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.
Completed
Push — 1.x ( 8d9920...6b0768 )
by Jakub
01:44
created

ResourceBase::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 2
1
<?php
2
3
/**
4
 * Resource base class.
5
 *
6
 * Abstract resource base class.
7
 */
8
9
namespace Vipps\Resource;
10
11
use Doctrine\Common\Annotations\AnnotationRegistry;
12
use JMS\Serializer\SerializerBuilder;
13
use Psr\Http\Message\RequestInterface;
14
use Vipps\Exceptions\ViPPSErrorException;
15
use Vipps\Exceptions\VippsException;
16
use Vipps\VippsInterface;
17
18
/**
19
 * Class ResourceBase
20
 * @package Vipps\Resources
21
 */
22
abstract class ResourceBase implements ResourceInterface
23
{
24
25
    /**
26
     * @var VippsInterface
27
     */
28
    protected $app;
29
30
    /**
31
     * @var array
32
     */
33
    protected $headers = [];
34
35
    /**
36
     * @var string
37
     */
38
    protected $body = '';
39
40
    /**
41
     * @var string
42
     */
43
    protected $id;
44
45
    /**
46
     * @var string
47
     */
48
    protected $path;
49
50
    /**
51
     * @var \Vipps\Resource\HttpMethod
52
     */
53
    protected $method;
54
55
    /**
56
     * @var \JMS\Serializer\Serializer
57
     */
58
    protected $serializer;
59
60
    /**
61
     * AbstractResource constructor.
62
     *
63
     * @param \Vipps\VippsInterface $vipps
64
     * @param string $subscription_key
65
     */
66
    public function __construct(VippsInterface $vipps, $subscription_key)
67
    {
68
        $this->app = $vipps;
69
70
        $this->headers['Ocp-Apim-Subscription-Key'] = $subscription_key;
71
72
        // Initiate serializer.
73
        AnnotationRegistry::registerLoader('class_exists');
0 ignored issues
show
Deprecated Code introduced by
The method Doctrine\Common\Annotati...istry::registerLoader() has been deprecated with message: this method is deprecated and will be removed in doctrine/annotations 2.0 autoloading should be deferred to the globally registered autoloader by then. For now, use @example AnnotationRegistry::registerLoader('class_exists')

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
74
        $this->serializer = SerializerBuilder::create()
75
            ->build();
76
    }
77
78
    /**
79
     * Gets serializer value.
80
     *
81
     * @return \JMS\Serializer\Serializer
82
     */
83
    public function getSerializer()
84
    {
85
        return $this->serializer;
86
    }
87
88
    /**
89
     * {@inheritdoc}
90
     */
91
    public function getHeaders()
92
    {
93
        return $this->headers;
94
    }
95
96
    /**
97
     * {@inheritdoc}
98
     */
99
    public function getMethod()
100
    {
101
        if (!isset($this->method)) {
102
            throw new \LogicException('Missing HTTP method');
103
        }
104
        return $this->method;
105
    }
106
107
    /**
108
     * {@inheritdoc}
109
     *
110
     * All occurrences of {id} pattern will be replaced with $this->id
111
     */
112
    public function getPath()
113
    {
114
        if (!isset($this->path)) {
115
            throw new \LogicException('Missing resource path');
116
        }
117
        // Get local var.
118
        $path = $this->path;
119
        // If ID is set replace {id} pattern with model's ID.
120
        if (isset($this->id)) {
121
            $path = str_replace('{id}', $this->id, $path);
122
        }
123
        return $path;
124
    }
125
126
    /**
127
     * @return string
128
     */
129
    public function getBody()
130
    {
131
        return $this->body;
132
    }
133
134
    /**
135
     * @param $path
136
     *
137
     * @return \Psr\Http\Message\UriInterface
138
     */
139
    public function getUri($path)
140
    {
141
        return $this->app->getClient()->getEndpoint()->getUri()->withPath($path);
142
    }
143
144
    /**
145
     * @return \Psr\Http\Message\ResponseInterface
146
     *
147
     * @throws \Vipps\Exceptions\VippsException
148
     */
149
    public function makeCall()
150
    {
151
        $request = $this->app->getClient()->getMessageFactory()->createRequest(
152
            $this->getMethod(),
153
            $this->getUri($this->getPath()),
154
            $this->getHeaders(),
155
            $this->getBody()
156
        );
157
        $response = $this->app->getClient()->getHttpClient()->sendRequest($request);
0 ignored issues
show
Bug introduced by
The method sendRequest does only exist in Http\Client\HttpClient, but not in Http\Client\HttpAsyncClient.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
158
        // @todo: Handle response.
159
160
        if ($response->getStatusCode() >= 400 && $response->getStatusCode() < 500) {
161
            $error = $response->getBody()->getContents();
162
            throw new VippsException($error, $response->getStatusCode());
163
        } elseif ($response->getStatusCode() >= 500 && $response->getStatusCode() < 600) {
164
            throw new VippsException($response->getReasonPhrase(), $response->getStatusCode());
165
        }
166
167
        return $response;
168
    }
169
}
170