1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace NotificationChannels\Apn; |
4
|
|
|
|
5
|
|
|
use ZendService\Apple\Apns\Client\Feedback as Client; |
6
|
|
|
use NotificationChannels\Apn\Exception\ConnectionFailed; |
7
|
|
|
|
8
|
|
|
class FeedbackService |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* The feedback client instance. |
12
|
|
|
* |
13
|
|
|
* @var \ZendService\Apple\Apns\Client\Feedback |
14
|
|
|
*/ |
15
|
|
|
protected $client; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Create feedback service instance. |
19
|
|
|
* |
20
|
|
|
* @param \ZendService\Apple\Apns\Client\Feedback $client |
21
|
|
|
* @param string $environment |
22
|
|
|
* @param string $certificate |
23
|
|
|
* @param string|null $passPhrase |
24
|
|
|
* @return void |
|
|
|
|
25
|
|
|
*/ |
26
|
1 |
|
public function __construct(Client $client, $environment, $certificate, $passPhrase = null) |
27
|
|
|
{ |
28
|
1 |
|
$this->client = $client; |
29
|
1 |
|
$this->environment = $environment; |
|
|
|
|
30
|
1 |
|
$this->certificate = $certificate; |
|
|
|
|
31
|
1 |
|
$this->passPhrase = $passPhrase; |
|
|
|
|
32
|
1 |
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Get feedback from the Apple Feedback Service about failed deliveries. |
36
|
|
|
* |
37
|
|
|
* @return array|ApnFeedback[] |
38
|
|
|
* @throws Exceptions\ConnectionFailed |
39
|
|
|
*/ |
40
|
1 |
|
public function get() |
41
|
|
|
{ |
42
|
1 |
|
$this->openConnection(); |
43
|
|
|
|
44
|
1 |
|
$feedback = $this->fetchFeedback(); |
45
|
|
|
|
46
|
1 |
|
$this->client->close(); |
47
|
|
|
|
48
|
1 |
|
return $feedback; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Open the connection to the feedback service. |
53
|
|
|
* |
54
|
|
|
* @return void |
55
|
|
|
* @throws \NotificationChannels\Apn\Exception\ConnectionFailed |
56
|
|
|
*/ |
57
|
1 |
|
protected function openConnection() |
58
|
|
|
{ |
59
|
|
|
try { |
60
|
1 |
|
$this->client->open($this->environment, $this->certificate, $this->passPhrase); |
61
|
|
|
} catch (Exception $exception) { |
|
|
|
|
62
|
|
|
throw ConnectionFailed::create($exception); |
63
|
|
|
} |
64
|
1 |
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Fetch the feedback from APNS and collect our feedback object. |
68
|
|
|
* |
69
|
|
|
* @return array |
70
|
|
|
*/ |
71
|
1 |
|
protected function fetchFeedback() |
72
|
|
|
{ |
73
|
1 |
|
$feedback = []; |
74
|
|
|
|
75
|
|
|
/** @var FeedbackResponse $response */ |
76
|
1 |
|
foreach ($this->client->feedback() as $response) { |
77
|
1 |
|
$feedback[] = new ApnFeedback($response->getToken(), $response->getTime()); |
78
|
|
|
} |
79
|
|
|
|
80
|
1 |
|
return $feedback; |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.