Completed
Push — master ( 9b760e...8a0b19 )
by Arthur
01:59
created

Client::addNotification()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
/*
4
 * This file is part of the Pushok package.
5
 *
6
 * (c) Arthur Edamov <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Pushok;
13
14
/**
15
 * Class Client
16
 * @package Pushok
17
 */
18
class Client
19
{
20
    /**
21
     * Array of notifications.
22
     *
23
     * @var Notification[]
24
     */
25
    private $notifications = [];
26
27
    /**
28
     * Authentication provider.
29
     *
30
     * @var AuthProviderInterface
31
     */
32
    private $authProvider;
33
34
    /**
35
     * Production or sandbox environment.
36
     *
37
     * @var bool
38
     */
39
    private $isProductionEnv;
40
41
    /**
42
     * Client constructor.
43
     *
44
     * @param AuthProviderInterface $authProvider
45
     * @param bool $isProductionEnv
46
     */
47
    public function __construct(AuthProviderInterface $authProvider, bool $isProductionEnv = false)
48
    {
49
        $this->authProvider = $authProvider;
50
        $this->isProductionEnv = $isProductionEnv;
51
    }
52
53
    /**
54
     * Push notifications to APNs.
55
     *
56
     * @return array
57
     */
58
    public function push(): array
59
    {
60
        $curlHandle = curl_init();
61
62
        $responseCollection = [];
63
        foreach ($this->notifications as $notification) {
64
            $request = new Request($notification, $this->isProductionEnv);
65
66
            $this->authProvider->authenticateClient($request);
67
68
            $result = $this->send($curlHandle, $request);
69
70
            list($headers, $body) = explode("\r\n\r\n", $result, 2);
71
72
            $statusCode = curl_getinfo($curlHandle, CURLINFO_HTTP_CODE);
73
74
            $responseCollection[] = new Response($statusCode, $headers, $body);
75
        }
76
77
        curl_close($curlHandle);
78
79
        return $responseCollection;
80
    }
81
82
    /**
83
     * Send request.
84
     *
85
     * @param $curlHandle
86
     * @param Request $request
87
     *
88
     * @return mixed Return the result on success, false on failure
89
     */
90
    private function send($curlHandle, Request $request)
91
    {
92
        curl_setopt_array($curlHandle, $request->getOptions());
93
        curl_setopt($curlHandle, CURLOPT_HTTPHEADER, $request->getHeaders());
94
95
        return curl_exec($curlHandle);
96
    }
97
98
    /**
99
     * Add notification in queue for sending.
100
     *
101
     * @param Notification $notification
102
     */
103
    public function addNotification(Notification $notification)
104
    {
105
        $this->notifications[] = $notification;
106
    }
107
108
    /**
109
     * Add several notifications in queue for sending.
110
     *
111
     * @param Notification[] $notifications
112
     */
113
    public function addNotifications(array $notifications)
114
    {
115
        $this->notifications = array_merge($this->notifications, $notifications);
116
    }
117
118
    /**
119
     * Get already added notifications.
120
     *
121
     * @return Notification[]
122
     */
123
    public function getNotifications()
124
    {
125
        return $this->notifications;
126
    }
127
}
128