Completed
Branch master (431168)
by Gennady
09:40
created

ExceptionFactory   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 6
c 2
b 0
f 0
lcom 0
cbo 3
dl 0
loc 42
ccs 0
cts 25
cp 0
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B factoryException() 0 30 6
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;
12
13
use Apns\Exception\ApnsException;
14
use Apns\Exception\CertificateException;
15
use Apns\Exception\InactiveDeviceTokenException;
16
17
/**
18
 * Class ExceptionFactory.
19
 */
20
class ExceptionFactory
21
{
22
    /**
23
     * Factory concrete exception for response from APN service.
24
     *
25
     * @param int             $statusCode
26
     * @param string          $responseContent
27
     * @param \Exception|null $previous
28
     *
29
     * @return ApnsException
30
     */
31
    public static function factoryException($statusCode, $responseContent, \Exception $previous = null)
32
    {
33
        $data = json_decode($responseContent, true);
34
35
        if (!is_array($data)) {
36
            return new ApnsException(
37
                sprintf('Unable to parse json `%s`: %s', json_last_error_msg(), $responseContent),
38
                0,
39
                $previous
40
            );
41
        }
42
43
        $reason = isset($data['reason']) ? (string) $data['reason'] : '';
44
45
        switch ($statusCode) {
46
            case 403:
47
                return new CertificateException($reason, $statusCode, $previous);
48
49
            case 410:
50
                return new InactiveDeviceTokenException(
51
                    $reason,
52
                    isset($data['timestamp']) ? $data['timestamp'] : 0,
53
                    $statusCode,
54
                    $previous
55
                );
56
57
            default:
58
                return new ApnsException($reason, $statusCode, $previous);
59
        }
60
    }
61
}
62