Skip to content

Commit 14a598c

Browse files
author
manojkumarlinux
committed
Class and Object via modification
1 parent 8fe0678 commit 14a598c

File tree

5 files changed

+67
-22
lines changed

5 files changed

+67
-22
lines changed

src/Apns.php

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,9 @@
99

1010
class Apns {
1111

12+
1213
public static function send($config, $message, $deviceToken) {
13-
$data = [];
14-
$setConfig = (new Config)->setKeyPath($config['keyPath'])->setSecretKey($config['secretKey'])->setBuildId($config['buildId'])->setEnv($config['environment']);
15-
$data['config'] = $setConfig->getConfig();
16-
$data['headers'] = $setConfig->getHaders();
17-
$setMessage = (new Message)->setTitle($message['title'])->setBody($message['body'])->setSound(isset($message['sound']) ? $message['sound'] : null);
18-
$data['message'] = $setMessage->getMessage();
19-
$setDeviceToken = (new DeviceToken)->setDeviceToken($deviceToken);
20-
$data['deviceToken'] = $setDeviceToken->getDeviceToken();
21-
return (new Request)->curlRequest($data);
22-
}
14+
return Request::curlRequest(new Config($config), new Message($message), new DeviceToken($deviceToken));
15+
}
2316

2417
}

src/Lib/Config.php

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,26 +19,48 @@ class Config {
1919

2020
private $deviceToken;
2121

22+
public function __construct($config) {
23+
$this->setKeyPath($config['keyPath'])
24+
->setSecretKey($config['secretKey'])
25+
->setBuildId($config['buildId'])
26+
->setURL($config['environment']);
27+
}
2228

23-
public function setEnv(bool $mode) {
29+
public function setURL(bool $mode) {
2430
$this->url = $mode ? $this->urlProduction : $this->urlDevelopment;
2531
return $this;
2632
}
2733

34+
public function getURL() {
35+
return $this->url;
36+
}
37+
2838
public function setKeyPath(string $path) {
2939
$this->keyPath = file_exists($path) ? $path : '';
3040
return $this;
3141
}
3242

43+
public function getKeyPath() {
44+
return $this->keyPath;
45+
}
46+
3347
public function setSecretKey(string $key) {
3448
$this->secretKey = $key;
3549
return $this;
3650
}
3751

52+
public function getSecretKey() {
53+
return $this->secretKey;
54+
}
55+
3856
public function setBuildId(string $id) {
3957
$this->buildId = $id;
4058
return $this;
4159
}
60+
61+
public function getBuildId(string $id) {
62+
return $this->buildId;
63+
}
4264

4365
public function getConfig() {
4466
return [
@@ -48,7 +70,7 @@ public function getConfig() {
4870
'keyPath' => $this->keyPath,
4971
];
5072
}
51-
73+
5274
public function getHaders() {
5375
return [
5476
'apns-topic: '.$this->buildId,

src/Lib/DeviceToken.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
class DeviceToken {
66
private $deviceToken;
77

8+
public function __construct(string $token) {
9+
$this->setDeviceToken($token);
10+
}
11+
812
public function setDeviceToken(string $token) {
913
$this->deviceToken = $token;
1014
return $this;

src/Lib/Message.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,41 @@ class Message {
1010

1111
private $sound = 'default';
1212

13+
public function __construct(Array $message) {
14+
return $this->setTitle($message['title'])
15+
->setBody($message['body'])
16+
->setSound(isset($message['sound']) ? $message['sound'] : null);
17+
}
18+
1319
public function setTitle(string $title) {
1420
$this->title = $title;
1521
return $this;
1622
}
1723

24+
public function getTitle(string $title) {
25+
return $this->title;
26+
}
27+
1828
public function setBody(string $body) {
1929
$this->body = $body;
2030
return $this;
2131
}
2232

33+
public function getBody() {
34+
return $this->body;
35+
}
36+
2337
public function setSound(string $sound = null) {
2438
if(!is_null($sound))
2539
$this->sound = $sound;
2640
return $this;
2741
}
2842

43+
public function getSound(string $sound = null) {
44+
return $this->sound;
45+
}
46+
47+
2948
public function getMessage() {
3049
return [
3150
'aps' => [

src/Lib/Request.php

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@
22

33
namespace SimpleApns\Lib;
44

5+
use SimpleApns\Lib\Config;
6+
use SimpleApns\Lib\Message;
7+
use SimpleApns\Lib\DeviceToken;
8+
59
class Request {
610

7-
public static $code = [
11+
public static $response = [
812
'0' => 'Config issues.',
913

1014
'200' => 'Success.',
@@ -27,26 +31,29 @@ class Request {
2731

2832
'503' => 'The server is shutting down and unavailable.',
2933
];
34+
35+
public static function getResponse($httpcode) {
36+
$httpcode = isset(self::$responses[$httpcode]) ? $httpcode : 0;
37+
return [ 'response' => self::$response[$httpcode] , 'code' => $httpcode ];
38+
}
3039

31-
public static function curlRequest(array $data) {
40+
public static function curlRequest(Config $config, Message $message, DeviceToken $deviceToken) {
3241

33-
$url = $data['config']['url'].$data['deviceToken'];
42+
$url = $config->getURL().$deviceToken->getDeviceToken();
3443

3544
$ch = curl_init($url);
36-
curl_setopt($ch, CURLOPT_HTTPHEADER, $data['headers']);
37-
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data['message']));
45+
curl_setopt($ch, CURLOPT_HTTPHEADER, $config->getHaders());
46+
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($message->getMessage()));
3847
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2_0);
3948

40-
curl_setopt($ch, CURLOPT_SSLCERT, $data['config']['keyPath']);
41-
curl_setopt($ch, CURLOPT_SSLCERTPASSWD, $data['config']['secretKey']);
49+
curl_setopt($ch, CURLOPT_SSLCERT, $config->getKeyPath());
50+
curl_setopt($ch, CURLOPT_SSLCERTPASSWD, $config->getsecretKey());
4251

4352
curl_setopt($ch, CURLOPT_FAILONERROR, true);
4453
curl_exec($ch);
4554
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
4655
curl_close($ch);
4756

48-
$httpcode = isset(self::$responses[$httpcode]) ? $httpcode : 0;
49-
50-
return [ 'response' => self::$code[$httpcode], 'code' => $httpcode];
57+
return self::getResponse($httpcode);
5158
}
5259
}

0 commit comments

Comments
 (0)