ExceptionFactory::createByReason()   D
last analyzed

Complexity

Conditions 30
Paths 30

Size

Total Lines 97

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 97
rs 4.1666
c 0
b 0
f 0
cc 30
nc 30
nop 2

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types = 1);
4
5
/*
6
 * This file is part of the AppleApnPush package
7
 *
8
 * (c) Vitaliy Zhuk <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code
12
 */
13
14
namespace Apple\ApnPush\Protocol\Http\ExceptionFactory;
15
16
use Apple\ApnPush\Exception\SendNotification\BadCertificateEnvironmentException;
17
use Apple\ApnPush\Exception\SendNotification\BadCertificateException;
18
use Apple\ApnPush\Exception\SendNotification\BadCollapseIdException;
19
use Apple\ApnPush\Exception\SendNotification\BadDeviceTokenException;
20
use Apple\ApnPush\Exception\SendNotification\BadExpirationDateException;
21
use Apple\ApnPush\Exception\SendNotification\BadMessageIdException;
22
use Apple\ApnPush\Exception\SendNotification\BadPathException;
23
use Apple\ApnPush\Exception\SendNotification\BadPriorityException;
24
use Apple\ApnPush\Exception\SendNotification\BadTopicException;
25
use Apple\ApnPush\Exception\SendNotification\DeviceTokenNotForTopicException;
26
use Apple\ApnPush\Exception\SendNotification\DuplicateHeadersException;
27
use Apple\ApnPush\Exception\SendNotification\ExpiredProviderTokenException;
28
use Apple\ApnPush\Exception\SendNotification\ForbiddenException;
29
use Apple\ApnPush\Exception\SendNotification\IdleTimeoutException;
30
use Apple\ApnPush\Exception\SendNotification\InternalServerErrorException;
31
use Apple\ApnPush\Exception\SendNotification\InvalidProviderTokenException;
32
use Apple\ApnPush\Exception\SendNotification\InvalidResponseException;
33
use Apple\ApnPush\Exception\SendNotification\MethodNotAllowedException;
34
use Apple\ApnPush\Exception\SendNotification\MissingContentInResponseException;
35
use Apple\ApnPush\Exception\SendNotification\MissingDeviceTokenException;
36
use Apple\ApnPush\Exception\SendNotification\MissingErrorReasonInResponseException;
37
use Apple\ApnPush\Exception\SendNotification\MissingProviderTokenException;
38
use Apple\ApnPush\Exception\SendNotification\MissingTopicException;
39
use Apple\ApnPush\Exception\SendNotification\PayloadEmptyException;
40
use Apple\ApnPush\Exception\SendNotification\PayloadTooLargeException;
41
use Apple\ApnPush\Exception\SendNotification\SendNotificationException;
42
use Apple\ApnPush\Exception\SendNotification\ServiceUnavailableException;
43
use Apple\ApnPush\Exception\SendNotification\ShutdownException;
44
use Apple\ApnPush\Exception\SendNotification\TooManyProviderTokenUpdatesException;
45
use Apple\ApnPush\Exception\SendNotification\TooManyRequestsException;
46
use Apple\ApnPush\Exception\SendNotification\TopicDisallowedException;
47
use Apple\ApnPush\Exception\SendNotification\UndefinedErrorException;
48
use Apple\ApnPush\Exception\SendNotification\UnregisteredException;
49
use Apple\ApnPush\Protocol\Http\Response;
50
51
/**
52
 * Default exception factory for create exception via response.
53
 * For all error codes, please see: https://developer.apple.com/library/prerelease/content/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/CommunicatingwithAPNs.html#//apple_ref/doc/uid/TP40008194-CH11-SW17
54
 */
55
class ExceptionFactory implements ExceptionFactoryInterface
56
{
57
    /**
58
     * {@inheritdoc}
59
     */
60
    public function create(Response $response): SendNotificationException
61
    {
62
        $content = $response->getContent();
63
64
        if (!$content) {
65
            return new MissingContentInResponseException();
66
        }
67
68
        $json = \json_decode($content, true);
69
70
        if (null === $json) {
71
            return new InvalidResponseException(\sprintf(
72
                'Can not parse JSON in response. Error: %d - %s',
73
                \json_last_error(),
74
                \json_last_error_msg()
75
            ));
76
        }
77
78
        if (!\array_key_exists('reason', $json)) {
79
            return new MissingErrorReasonInResponseException();
80
        }
81
82
        $reason = $json['reason'];
83
84
        return $this->createByReason($reason, $json);
85
    }
86
87
    /**
88
     * Create exception by reason
89
     *
90
     * @param string $reason
91
     * @param array  $json
92
     *
93
     * @return SendNotificationException
94
     */
95
    private function createByReason(string $reason, array $json): SendNotificationException
96
    {
97
        $reason = \strtolower($reason);
98
99
        switch ($reason) {
100
            case 'badcollapseid':
101
                return new BadCollapseIdException();
102
103
            case 'baddevicetoken':
104
                return new BadDeviceTokenException();
105
106
            case 'badexpirationdate':
107
                return new BadExpirationDateException();
108
109
            case 'badmessageid':
110
                return new BadMessageIdException();
111
112
            case 'badpriority':
113
                return new BadPriorityException();
114
115
            case 'badtopic':
116
                return new BadTopicException();
117
118
            case 'devicetokennotfortopic':
119
                return new DeviceTokenNotForTopicException();
120
121
            case 'duplicateheaders':
122
                return new DuplicateHeadersException();
123
124
            case 'idletimeout':
125
                return new IdleTimeoutException();
126
127
            case 'missingdevicetoken':
128
                return new MissingDeviceTokenException();
129
130
            case 'missingtopic':
131
                return new MissingTopicException();
132
133
            case 'payloadempty':
134
                return new PayloadEmptyException();
135
136
            case 'topicdisallowed':
137
                return new TopicDisallowedException();
138
139
            case 'badcertificate':
140
                return new BadCertificateException();
141
142
            case 'badcertificateenvironment':
143
                return new BadCertificateEnvironmentException();
144
145
            case 'expiredprovidertoken':
146
                return new ExpiredProviderTokenException();
147
148
            case 'forbidden':
149
                return new ForbiddenException();
150
151
            case 'invalidprovidertoken':
152
                return new InvalidProviderTokenException();
153
154
            case 'missingprovidertoken':
155
                return new MissingProviderTokenException();
156
157
            case 'badpath':
158
                return new BadPathException();
159
160
            case 'methodnotallowed':
161
                return new MethodNotAllowedException();
162
163
            case 'unregistered':
164
                $timestamp = \array_key_exists('timestamp', $json) ? $json['timestamp'] : 0;
165
                $lastConfirmed = new \DateTime('now', new \DateTimeZone('UTC'));
166
                $lastConfirmed->setTimestamp($timestamp);
167
168
                return new UnregisteredException($lastConfirmed);
169
170
            case 'payloadtoolarge':
171
                return new PayloadTooLargeException();
172
173
            case 'toomanyprovidertokenupdates':
174
                return new TooManyProviderTokenUpdatesException();
175
176
            case 'toomanyrequests':
177
                return new TooManyRequestsException();
178
179
            case 'internalservererror':
180
                return new InternalServerErrorException();
181
182
            case 'serviceunavailable':
183
                return new ServiceUnavailableException();
184
185
            case 'shutdown':
186
                return new ShutdownException();
187
188
            default:
189
                return new UndefinedErrorException();
190
        }
191
    }
192
}
193