APNSMessage::getBinaryMessage()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 11
rs 9.4285
cc 1
eloc 8
nc 1
nop 0
1
<?php
2
3
namespace JWage\APNS;
4
5
/**
6
 * APNSMessage class represents an individual message
7
 * to a device token with a Payload to deliver.
8
 */
9
class APNSMessage
10
{
11
    /**
12
     * @var string
13
     */
14
    private $deviceToken;
15
16
    /**
17
     * @var \JWage\APNS\Payload
18
     */
19
    private $payload;
20
21
    /**
22
     * Construct.
23
     *
24
     * @param string $deviceToken
25
     * @param \JWage\APNS\Payload $payload
26
     */
27
    public function __construct($deviceToken, Payload $payload)
28
    {
29
        $this->deviceToken = $deviceToken;
30
        $this->payload = $payload;
31
    }
32
33
    /**
34
     * Returns a binary message that the Apple Push Notification Service understands.
35
     *
36
     * @return string $binaryMessage
37
     */
38
    public function getBinaryMessage()
39
    {
40
        $encodedPayload = $this->jsonEncode($this->payload->getPayload());
41
42
        return chr(0).
43
               chr(0).
44
               chr(32).
45
               pack('H*', $this->deviceToken).
46
               chr(0).chr(strlen($encodedPayload)).
47
               $encodedPayload;
48
    }
49
50
    /**
51
     * @param array $payload
52
     * @return string $payloadJson
53
     */
54
    private function jsonEncode(array $payload)
55
    {
56
        return json_encode($payload);
57
    }
58
}
59