GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

VippsException::parsePhrase()   A
last analyzed

Complexity

Conditions 5
Paths 8

Size

Total Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 31
rs 9.1128
c 0
b 0
f 0
cc 5
nc 8
nop 2
1
<?php
2
3
/**
4
 * Vipps exception.
5
 *
6
 * Provides and handles vipps exception.
7
 */
8
9
namespace zaporylie\Vipps\Exceptions;
10
11
use JMS\Serializer\Serializer;
12
use Psr\Http\Message\ResponseInterface;
13
use zaporylie\Vipps\Model\Error\AuthorizationError;
14
use zaporylie\Vipps\Model\Error\ErrorInterface;
15
use zaporylie\Vipps\Model\Error\PaymentError;
16
17
/**
18
 * Class VippsException
19
 * @package Vipps\Exceptions
20
 */
21
class VippsException extends \Exception
22
{
23
24
    /**
25
     * @var \zaporylie\Vipps\Model\Error\ErrorInterface|null
26
     */
27
    protected $error;
28
29
    /**
30
     * VippsException constructor.
31
     *
32
     * @param string $message
33
     * @param int $code
34
     * @param \Exception|null $previous
35
     * @param \zaporylie\Vipps\Model\Error\ErrorInterface|null $error
36
     */
37
    public function __construct($message = '', $code = 0, \Exception $previous = null, ErrorInterface $error = null)
38
    {
39
        parent::__construct($message, $code, $previous);
40
        $this->error = $error;
41
    }
42
43
    /**
44
     * @param string $phrase
45
     * @param \JMS\Serializer\Serializer|null $serializer
46
     *
47
     * @return object|array|integer|double|string|boolean
48
     */
49
    protected static function parsePhrase($phrase, $serializer = null)
50
    {
51
        if (!($serializer instanceof Serializer)) {
52
            return $phrase;
53
        }
54
55
        try {
56
            $decoded = json_decode($phrase, true);
57
            // Match AuthorizationError.
58
            if (isset($decoded['error'])) {
59
                return $serializer->deserialize(
60
                    $phrase,
61
                    AuthorizationError::class,
62
                    'json'
63
                );
64
            }
65
            // Match PaymentError collection.
66
            if (isset($decoded[0]['errorGroup'])) {
67
                $phrase = $serializer->deserialize(
68
                    $phrase,
69
                    'array<' . PaymentError::class . '>',
70
                    'json'
71
                );
72
                return reset($phrase);
73
            }
74
        } catch (\Exception $exception) {
75
            // Mute exceptions.
76
        }
77
78
        return $phrase;
79
    }
80
81
    /**
82
     * @return \zaporylie\Vipps\Model\Error\ErrorInterface|null
83
     */
84
    public function getError()
85
    {
86
        return $this->error;
87
    }
88
89
    /**
90
     * Create new Exception from Response.
91
     *
92
     * @param ResponseInterface $response
93
     * @param \JMS\Serializer\Serializer|null $serializer
94
     * @param bool $force
95
     *
96
     * @return null|\zaporylie\Vipps\Exceptions\VippsException
97
     */
98
    public static function createFromResponse(
99
        ResponseInterface $response,
100
        $serializer = null,
101
        $force = true
102
    ) {
103
104
        // If error code tells us that something went wrong we must accept it.
105
        if (!$force && $response->getStatusCode() >= 400) {
106
            $force = true;
107
        }
108
109
        $phrase = $response->getBody()->getContents();
110
        $phrase = self::parsePhrase($phrase, $serializer);
111
112
        // If not an instance of ErrorInterface we must assume everything is ok.
113
        if (!$force && !($phrase instanceof ErrorInterface)) {
114
            // Rewind content pointer.
115
            $response->getBody()->rewind();
116
            return null;
117
        }
118
119
        // If Error can be parsed.
120
        if ($phrase instanceof ErrorInterface) {
121
            return new static(
122
                $phrase->getMessage(),
123
                $response->getStatusCode(),
124
                null,
125
                $phrase
126
            );
127
        }
128
129
        // If Error cannot be parsed.
130
        return new static(
131
            $phrase ?: $response->getReasonPhrase(),
132
            $response->getStatusCode()
133
        );
134
    }
135
}
136