1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace NotificationChannels\Apn; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use ZendService\Apple\Apns\Client\Feedback as Client; |
7
|
|
|
use NotificationChannels\Apn\Exception\ConnectionFailed; |
8
|
|
|
|
9
|
|
|
class FeedbackService |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* The feedback client instance. |
13
|
|
|
* |
14
|
|
|
* @var \ZendService\Apple\Apns\Client\Feedback |
15
|
|
|
*/ |
16
|
|
|
protected $client; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* The connection environment. |
20
|
|
|
* |
21
|
|
|
* @var int |
22
|
|
|
*/ |
23
|
|
|
protected $environment; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* The connection certificate. |
27
|
|
|
* |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
protected $certificate; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* The connection pass phrase. |
34
|
|
|
* |
35
|
|
|
* @var string |
36
|
|
|
*/ |
37
|
|
|
protected $passPhrase; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Create feedback service instance. |
41
|
|
|
* |
42
|
|
|
* @param \ZendService\Apple\Apns\Client\Feedback $client |
43
|
|
|
* @param int $environment |
44
|
|
|
* @param string $certificate |
45
|
|
|
* @param string|null $passPhrase |
46
|
|
|
*/ |
47
|
1 |
|
public function __construct(Client $client, $environment, $certificate, $passPhrase = null) |
48
|
|
|
{ |
49
|
1 |
|
$this->client = $client; |
50
|
1 |
|
$this->environment = $environment; |
51
|
1 |
|
$this->certificate = $certificate; |
52
|
1 |
|
$this->passPhrase = $passPhrase; |
53
|
1 |
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Get feedback from the Apple Feedback Service about failed deliveries. |
57
|
|
|
* |
58
|
|
|
* @return array|ApnFeedback[] |
59
|
|
|
* @throws Exceptions\ConnectionFailed |
60
|
|
|
*/ |
61
|
1 |
|
public function get() |
62
|
|
|
{ |
63
|
1 |
|
$this->openConnection(); |
64
|
|
|
|
65
|
1 |
|
$feedback = $this->fetchFeedback(); |
66
|
|
|
|
67
|
1 |
|
$this->client->close(); |
68
|
|
|
|
69
|
1 |
|
return $feedback; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Open the connection to the feedback service. |
74
|
|
|
* |
75
|
|
|
* @return void |
76
|
|
|
* @throws \NotificationChannels\Apn\Exception\ConnectionFailed |
77
|
|
|
*/ |
78
|
1 |
View Code Duplication |
protected function openConnection() |
|
|
|
|
79
|
|
|
{ |
80
|
|
|
try { |
81
|
1 |
|
$this->client->open($this->environment, $this->certificate, $this->passPhrase); |
82
|
|
|
} catch (Exception $exception) { |
83
|
|
|
throw ConnectionFailed::create($exception); |
84
|
|
|
} |
85
|
1 |
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Fetch the feedback from APNS and collect our feedback object. |
89
|
|
|
* |
90
|
|
|
* @return array |
91
|
|
|
*/ |
92
|
1 |
|
protected function fetchFeedback() |
93
|
|
|
{ |
94
|
1 |
|
$feedback = []; |
95
|
|
|
|
96
|
|
|
/** @var FeedbackResponse $response */ |
97
|
1 |
|
foreach ($this->client->feedback() as $response) { |
98
|
1 |
|
$feedback[] = new ApnFeedback($response->getToken(), $response->getTime()); |
99
|
|
|
} |
100
|
|
|
|
101
|
1 |
|
return $feedback; |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.