Skip to content

Commit 831ccd2

Browse files
committed
Intergrated Config Implemented
* Users can use Config class to set both Android & APNs config setting * Basic functionality implemented. Need to improve on error-proofness
1 parent aa2e84f commit 831ccd2

File tree

3 files changed

+139
-2
lines changed

3 files changed

+139
-2
lines changed

src/Config.php

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: lkaybob
5+
* Date: 05/04/2018
6+
* Time: 20:12
7+
*/
8+
9+
namespace phpFCMv1;
10+
11+
use phpFCMv1\Config\AndroidConfig;
12+
use phpFCMv1\Config\APNsConfig;
13+
use phpFCMv1\Config\CommonConfig;
14+
15+
class Config implements CommonConfig {
16+
const PRIORITY_HIGH = 1;
17+
const PRIORITY_NORMAL = 2;
18+
private $androidConfig;
19+
private $apnsConfig;
20+
21+
public function __construct() {
22+
$this -> androidConfig = new AndroidConfig();
23+
$this -> apnsConfig = new APNsConfig();
24+
}
25+
26+
/**
27+
*
28+
* @param $key
29+
* @return mixed
30+
*/
31+
function setCollapseKey($key) {
32+
$this -> androidConfig -> setCollapseKey($key);
33+
$this -> apnsConfig -> setCollapseKey($key);
34+
35+
return null;
36+
}
37+
38+
/**
39+
* @param $priority
40+
* @return mixed
41+
*/
42+
function setPriority($priority) {
43+
switch ($priority) {
44+
case self::PRIORITY_HIGH:
45+
$this -> androidConfig -> setPriority(AndroidConfig::PRIORITY_HIGH);
46+
$this -> apnsConfig -> setPriority(APNsConfig::PRIORITY_HIGH);
47+
break;
48+
case self::PRIORITY_NORMAL:
49+
$this -> androidConfig -> setPriority(AndroidConfig::PRIORITY_NORMAL);
50+
$this -> apnsConfig -> setPriority(APNsConfig::PRIORITY_NORMAL);
51+
break;
52+
default:
53+
throw new \InvalidArgumentException("Priority option not proper");
54+
break;
55+
}
56+
57+
return null;
58+
}
59+
60+
/**
61+
* @param $time : seconds
62+
* @return mixed
63+
*/
64+
function setTimeToLive($time) {
65+
try {
66+
$this -> androidConfig -> setTimeToLive($time);
67+
$this -> apnsConfig -> setTimeToLive($time);
68+
} catch (\Exception $e) {
69+
70+
}
71+
72+
return null;
73+
}
74+
75+
/**
76+
* @return mixed
77+
*/
78+
function getPayload() {
79+
$androidConfig = $this -> androidConfig -> getPayload();
80+
$apnsConfig = $this -> apnsConfig -> getPayload();
81+
82+
return array_merge($androidConfig, $apnsConfig);
83+
}
84+
85+
function __invoke() {
86+
return $this -> getPayload();
87+
}
88+
}

src/Config/APNsConfig.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,13 @@ function setPriority($priority) {
4343
}
4444

4545
/**
46-
* @param $time : Time for notification to live in days
46+
* @param $time : Time for notification to live in seconds
4747
* @return mixed : Expiration option using UNIX epoch date
4848
* @throws \Exception
4949
*/
5050
function setTimeToLive($time) {
5151
$expiration = new \DateTime('now');
52-
$expiration -> add(new \DateInterval('P' . $time . 'D'));
52+
$expiration -> add(new \DateInterval('PT' . $time . 'S'));
5353
$expValue = $expiration -> format('U');
5454

5555
$payload = array_merge($this -> payload, array('apns-expiration' => $expValue));

tests/ConfigTest.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: lkaybob
5+
* Date: 21/04/2018
6+
* Time: 16:31
7+
*/
8+
9+
namespace phpFCMv1\tests;
10+
11+
use phpFCMv1\Config;
12+
use \PHPUnit\Framework\TestCase;
13+
14+
class ConfigTest extends TestCase {
15+
16+
public function testSetCollapseKey() {
17+
$this -> markTestSkipped("Skipping passed test");
18+
$fcmTest = new FCMTest();
19+
$config = new Config();
20+
21+
$config -> setCollapseKey('test');
22+
$result = $fcmTest -> fireWithConfig($config);
23+
24+
$this -> assertTrue($result);
25+
}
26+
27+
public function testSetTimeToLive() {
28+
$this -> markTestSkipped("Implemented");
29+
$fcmTest = new FCMTest();
30+
$config = new Config();
31+
32+
$config -> setTimeToLive(10);
33+
$result = $fcmTest -> fireWithConfig($config);
34+
35+
$this -> assertTrue($result);
36+
}
37+
38+
39+
public function testSetPriority() {
40+
// $this -> markTestSkipped("Not Implemented");
41+
$fcmTest = new FCMTest();
42+
$config = new Config();
43+
44+
$config -> setPriority(Config::PRIORITY_HIGH);
45+
$result = $fcmTest -> fireWithConfig($config);
46+
47+
$this -> assertTrue($result);
48+
}
49+
}

0 commit comments

Comments
 (0)