Completed
Push — master ( 04f203...b3922d )
by Gennady
02:26
created

GuzzleHandler::factoryException()   C

Complexity

Conditions 7
Paths 8

Size

Total Lines 48
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 48
rs 6.7272
cc 7
eloc 32
nc 8
nop 1
1
<?php
2
/**
3
 * PHP APNS.
4
 *
5
 * @author Gennady Telegin <[email protected]>
6
 *
7
 * This source file is subject to the license that is bundled
8
 * with this source code in the file LICENSE.
9
 */
10
11
namespace Apns\Handler;
12
13
use Apns\Client;
14
use Apns\Exception\ApnsException;
15
use Apns\Exception\CertificateException;
16
use Apns\Exception\InactiveDeviceTokenException;
17
use Apns\Message;
18
use GuzzleHttp\Client as HttpClient;
19
use GuzzleHttp\Exception\RequestException;
20
21
if (!defined('CURL_HTTP_VERSION_2_0')) {
22
    define('CURL_HTTP_VERSION_2_0', 3);
23
}
24
25
/**
26
 * Class GuzzleHandler.
27
 */
28
class GuzzleHandler
29
{
30
    /**
31
     * @var Client
32
     */
33
    private $httpClient;
34
35
    /**
36
     * GuzzleHandler constructor.
37
     */
38
    public function __construct()
39
    {
40
        $this->httpClient = new HttpClient();
0 ignored issues
show
Documentation Bug introduced by
It seems like new \GuzzleHttp\Client() of type object<GuzzleHttp\Client> is incompatible with the declared type object<Apns\Client> of property $httpClient.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
41
    }
42
43
    /**
44
     * @param Client  $apns
45
     * @param Message $message
46
     *
47
     * @return bool
48
     *
49
     * @throws ApnsException
50
     */
51
    public function __invoke(Client $apns, Message $message)
52
    {
53
        try {
54
            $response = $this->httpClient->request(
0 ignored issues
show
Bug introduced by
The method request() does not seem to exist on object<Apns\Client>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
55
                'POST',
56
                $apns->getPushURI($message),
57
                [
58
                    'json' => $message->getMessageBody(),
59
                    'cert' => $apns->getSslCert(),
60
                    'headers' => $message->getMessageHeaders(),
61
                    'curl' => [
62
                        CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_2_0,
63
                    ],
64
                ]
65
            );
66
67
            return 200 === $response->getStatusCode();
68
        } catch (RequestException $e) {
69
            throw self::factoryException($e);
70
        }
71
    }
72
73
    /**
74
     * @param RequestException $exception
75
     *
76
     * @return ApnsException
77
     */
78
    private static function factoryException(RequestException $exception)
79
    {
80
        $response = $exception->getResponse();
81
82
        if (null === $response) {
83
            return new ApnsException(
84
                'Unknown network error',
85
                0,
86
                $exception
87
            );
88
        }
89
90
        try {
91
            $data = json_decode($response->getBody()->getContents(), true);
92
        } catch (\Exception $e) {
93
            return new ApnsException(
94
                'Unknown network error',
95
                0,
96
                $e
97
            );
98
        }
99
100
        $reason = isset($data['reason']) ? (string) $data['reason'] : '';
101
102
        switch ($response->getStatusCode()) {
103
            case 403:
104
                return new CertificateException(
105
                    $reason,
106
                    $response->getStatusCode(),
107
                    $exception
108
                );
109
110
            case 410:
111
                return new InactiveDeviceTokenException(
112
                    $reason,
113
                    isset($data['timestamp']) ? $data['timestamp'] : 0,
114
                    $response->getStatusCode(),
115
                    $exception
116
                );
117
118
            default:
119
                return new ApnsException(
120
                    $reason,
121
                    $response->getStatusCode(),
122
                    $exception
123
                );
124
        }
125
    }
126
}
127