SocketClient::getSocketAddress()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace JWage\APNS;
4
5
use ErrorException;
6
7
class SocketClient
8
{
9
    const PAYLOAD_MAX_BYTES = 256;
10
11
    /**
12
     * @var \JWage\APNS\Certificate
13
     */
14
    private $certificate;
15
16
    /**
17
     * @var string
18
     */
19
    private $host;
20
21
    /**
22
     * @var int
23
     */
24
    private $port;
25
26
    /**
27
     * @var Resource
28
     */
29
    private $apnsResource;
30
31
    /**
32
     * @var integer
33
     */
34
    private $error;
35
36
    /**
37
     * @var string
38
     */
39
    private $errorString;
40
41
    /**
42
     * Construct.
43
     *
44
     * @param \JWage\APNS\Certificate $certificate
45
     * @param string $host
46
     * @param string $port
47
     */
48
    public function __construct(Certificate $certificate, $host, $port)
49
    {
50
        $this->certificate = $certificate;
51
        $this->host = $host;
52
        $this->port = $port;
0 ignored issues
show
Documentation Bug introduced by
The property $port was declared of type integer, but $port is of type string. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
53
    }
54
55
    public function __destruct()
56
    {
57
        if (is_resource($this->apnsResource)) {
58
            fclose($this->apnsResource);
59
        }
60
    }
61
62
    /**
63
     * Writes a binary message to apns.
64
     *
65
     * @param string $binaryMessage
66
     * @return integer Returns the number of bytes written, or FALSE on error.
67
     */
68
    public function write($binaryMessage)
69
    {
70
        if (strlen($binaryMessage) > self::PAYLOAD_MAX_BYTES) {
71
            throw new \InvalidArgumentException(
72
                sprintf('The maximum size allowed for a notification payload is %s bytes; Apple Push Notification Service refuses any notification that exceeds this limit.', self::PAYLOAD_MAX_BYTES)
73
            );
74
        }
75
76
        return fwrite($this->getApnsResource(), $binaryMessage);
77
    }
78
79
    /**
80
     * @return Resource
81
     */
82
    protected function getApnsResource()
83
    {
84
        if (!is_resource($this->apnsResource)) {
85
            $this->apnsResource = $this->createStreamClient();
86
        }
87
88
        return $this->apnsResource;
89
    }
90
91
    /**
92
     * @return Resource
93
     */
94
    protected function createStreamContext()
95
    {
96
        $streamContext = stream_context_create();
97
        stream_context_set_option($streamContext, 'ssl', 'local_cert', $this->certificate->writeToTmp());
98
99
        return $streamContext;
100
    }
101
102
    /**
103
     * @return Resource
104
     */
105
    protected function createStreamClient()
106
    {
107
        $address = $this->getSocketAddress();
108
109
        $client = @stream_socket_client(
110
            $address,
111
            $this->error,
112
            $this->errorString,
113
            2,
114
            STREAM_CLIENT_CONNECT,
115
            $this->createStreamContext()
116
        );
117
118
        if (!$client) {
119
            throw new ErrorException(
120
                sprintf('Failed to create stream socket client to "%s". %s', $address, $this->errorString), $this->error
121
            );
122
        }
123
124
        return $client;
125
    }
126
127
    /**
128
     * @return string $socketAddress
129
     */
130
    protected function getSocketAddress()
131
    {
132
        return sprintf('ssl://%s:%s', $this->host, $this->port);
133
    }
134
}
135