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
Pull Request — master (#24)
by Anatolii
03:27
created

Parser::setLastPage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php
2
3
  namespace Xparse\Parser;
4
5
  use GuzzleHttp\Client;
6
  use GuzzleHttp\ClientInterface;
7
  use GuzzleHttp\Psr7\Request;
8
  use GuzzleHttp\RequestOptions;
9
  use GuzzleHttp\TransferStats;
10
  use Psr\Http\Message\RequestInterface;
11
  use Psr\Http\Message\ResponseInterface;
12
  use Xparse\ElementFinder\ElementFinder;
13
  use Xparse\ParserInterface\ParserInterface;
14
15
  /**
16
   *
17
   * @package Xparse\Parser
18
   */
19
  class Parser implements ParserInterface {
20
21
    /**
22
     *
23
     * @var null|\Xparse\ElementFinder\ElementFinder
24
     */
25
    protected $lastPage = null;
26
27
    /**
28
     * @var ClientInterface
29
     */
30
    protected $client = null;
31
32
    /**
33
     * @var ElementFinderFactoryInterface
34
     */
35
    protected $elementFinderFactory = null;
36
37
    /**
38
     * @var null|ResponseInterface
39
     */
40
    protected $lastResponse = null;
41
42
43
    /**
44
     * @param ClientInterface|null $client
45
     * @param ElementFinderFactoryInterface|null $elementFinderFactory
46
     */
47 10
    public function __construct(ClientInterface $client = null, ElementFinderFactoryInterface $elementFinderFactory = null) {
48 10
      if ($client === null) {
49 3
        $client = new Client([
50 3
          RequestOptions::ALLOW_REDIRECTS => true,
51 3
          RequestOptions::COOKIES => new \GuzzleHttp\Cookie\CookieJar(),
52 3
        ]);
53 3
      }
54
55 10
      if ($elementFinderFactory === null) {
56 10
        $elementFinderFactory = new ElementFinderFactory();
57 10
      }
58
59 10
      $this->elementFinderFactory = $elementFinderFactory;
60
61 10
      $this->client = $client;
62 10
    }
63
64
65
    /**
66
     * @param string $url
67
     * @param array $options
68
     * @return ElementFinder
69
     * @throws \Exception
70
     * @throws \InvalidArgumentException
71
     */
72 5
    public function get($url, array $options = []) {
73 5
      if (empty($url) or !is_string($url)) {
74 1
        throw new \InvalidArgumentException('Url must be not empty and string.');
75
      }
76
77 4
      $request = new Request('GET', $url);
78 4
      return $this->send($request, $options);
79
    }
80
81
82
    /**
83
     * @param string $url
84
     * @param string|resource|\Psr\Http\Message\StreamInterface $body Message body.
85
     * @param array $options
86
     * @return ElementFinder
87
     * @throws \Exception
88
     * @throws \InvalidArgumentException
89
     */
90 2
    public function post($url, $body = null, array $options = []) {
91
92 2
      if (empty($url) or !is_string($url)) {
93 1
        throw new \InvalidArgumentException('Url must be not empty and string.');
94
      }
95
96 1
      $request = new Request('POST', $url, [], $body);
97
98 1
      return $this->send($request, $options);
99
    }
100
101
102
    /**
103
     * @return \Xparse\ElementFinder\ElementFinder
104
     */
105 2
    public function getLastPage() {
106 2
      return $this->lastPage;
107
    }
108
109
110
    /**
111
     * @param \Xparse\ElementFinder\ElementFinder $lastPage
112
     * @return $this
113
     */
114 5
    public function setLastPage($lastPage) {
115 5
      $this->lastPage = $lastPage;
116 5
      return $this;
117
    }
118
119
120
    /**
121
     * @return null|ResponseInterface
122
     */
123 2
    public function getLastResponse() {
124 2
      return $this->lastResponse;
125
    }
126
127
128
    /**
129
     * @return ClientInterface
130
     */
131 4
    public function getClient() {
132 4
      return $this->client;
133
    }
134
135
136
    /**
137
     * @param $request
138
     * @param array $options
139
     * @return \Xparse\ElementFinder\ElementFinder
140
     * @throws \Exception
141
     */
142 5
    public function send(RequestInterface $request, array $options = []) {
143
144 5
      $prevCallback = !empty($options['on_stats']) ? $options['on_stats'] : null;
145
146 5
      $effectiveUri = null;
147 5
      $options['on_stats'] = function (TransferStats $stats) use (&$effectiveUri, $prevCallback) {
148 5
        $effectiveUri = $stats->getEffectiveUri();
149 5
        if ($prevCallback !== null) {
150
          /** @var callable $prevCallback */
151
          $prevCallback($stats);
152
        }
153 5
      };
154
155 5
      $response = $this->client->send($request, $options);
156
157 5
      $guzzleEffectiveUrl = $response->getHeaderLine('X-GUZZLE-EFFECTIVE-URL');
158 5
      if (!empty($guzzleEffectiveUrl)) {
159 5
        $effectiveUri = $guzzleEffectiveUrl;
160 5
      }
161
162 5
      $page = $this->elementFinderFactory->create($response, $effectiveUri);
163
164 5
      $this->setLastPage($page);
165 5
      $this->lastResponse = $response;
166 5
      return $page;
167
    }
168
169
170
    /**
171
     * @return ElementFinderFactoryInterface
172
     */
173 1
    public function getElementFinderFactory() {
174 1
      return $this->elementFinderFactory;
175
    }
176
177
  }
178