Completed
Push — master ( 8d719e...0f62ec )
by Arthur
08:07
created

Client::push()   C

Complexity

Conditions 8
Paths 12

Size

Total Lines 46
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 46
rs 5.5555
c 0
b 0
f 0
cc 8
eloc 29
nc 12
nop 0
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 ApnsResponseInterface[]
57
     */
58
    public function push(): array
59
    {
60
        $mh = curl_multi_init();
61
62
        curl_multi_setopt($mh, CURLMOPT_PIPELINING, CURLPIPE_MULTIPLEX);
63
64
        $handles = [];
65
        foreach ($this->notifications as $k => $notification) {
66
            $request = new Request($notification, $this->isProductionEnv);
67
            $handles[] = $ch = curl_init();
68
69
            $this->authProvider->authenticateClient($ch);
0 ignored issues
show
Documentation introduced by
$ch is of type resource, but the function expects a object<Pushok\Request>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
70
71
            curl_setopt_array($ch, $request->getOptions());
72
            curl_setopt($ch, CURLOPT_HTTPHEADER, $request->getDecoratedHeaders());
73
74
            curl_multi_add_handle($mh, $ch);
75
        }
76
77
        $active = null;
78
        do {
79
            $mrc = curl_multi_exec($mh, $active);
80
        } while ($mrc == CURLM_CALL_MULTI_PERFORM);
81
82
        while ($active && $mrc == CURLM_OK) {
83
            if (curl_multi_select($mh) != -1) {
84
                do {
85
                    $mrc = curl_multi_exec($mh, $active);
86
                } while ($mrc == CURLM_CALL_MULTI_PERFORM);
87
            }
88
        }
89
90
        $responseCollection = [];
91
        foreach ($handles as $handle) {
92
            curl_multi_remove_handle($mh, $handle);
93
            $result = curl_multi_getcontent($handle);
94
95
            list($headers, $body) = explode("\r\n\r\n", $result, 2);
96
            $statusCode = curl_getinfo($handle, CURLINFO_HTTP_CODE);
97
            $responseCollection[] = new Response($statusCode, $headers, $body);
98
        }
99
100
        curl_multi_close($mh);
101
102
        return $responseCollection;
103
    }
104
105
    /**
106
     * Add notification in queue for sending.
107
     *
108
     * @param Notification $notification
109
     */
110
    public function addNotification(Notification $notification)
111
    {
112
        $this->notifications[] = $notification;
113
    }
114
115
    /**
116
     * Add several notifications in queue for sending.
117
     *
118
     * @param Notification[] $notifications
119
     */
120
    public function addNotifications(array $notifications)
121
    {
122
        $this->notifications = array_merge($this->notifications, $notifications);
123
    }
124
125
    /**
126
     * Get already added notifications.
127
     *
128
     * @return Notification[]
129
     */
130
    public function getNotifications()
131
    {
132
        return $this->notifications;
133
    }
134
}
135