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 — master ( 0bf85f...94d3f3 )
by Anatolii
18:54 queued 18:54
created

Parser   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 193
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 11

Test Coverage

Coverage 73.32%

Importance

Changes 13
Bugs 0 Features 4
Metric Value
wmc 21
c 13
b 0
f 4
lcom 1
cbo 11
dl 0
loc 193
ccs 44
cts 60
cp 0.7332
rs 10

12 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 2
A get() 0 8 3
A post() 0 12 3
A getLastPage() 0 3 1
A setLastPage() 0 4 1
A getLastResponse() 0 3 1
A getClient() 0 3 1
A getConvertRelativeLinksState() 0 3 1
A setConvertRelativeLinksState() 0 4 1
A getConvertEncodingState() 0 3 1
A setConvertEncodingState() 0 4 1
B send() 0 36 5
1
<?php
2
3
  namespace Xparse\Parser;
4
5
  use GuzzleHttp\ClientInterface;
6
  use GuzzleHttp\HandlerStack;
7
  use GuzzleHttp\Psr7\Request;
8
  use Psr\Http\Message\RequestInterface;
9
  use Psr\Http\Message\ResponseInterface;
10
  use Xparse\ElementFinder\ElementFinder;
11
  use Xparse\ElementFinder\Helper;
12
  use Xparse\Parser\Helper\LinkConverter;
13
14
  /**
15
   *
16
   * @package Xparse\Parser
17
   */
18
  class Parser implements \Xparse\ParserInterface\ParserInterface {
19
20
    /**
21
     * Set true if we need to automatically convert relative links to absolute
22
     */
23
    protected $convertRelativeLinksState = true;
24
25
    /**
26
     * Set true if we need automatically convert encoding to utf-8
27
     */
28
    protected $convertEncodingState = true;
29
30
    /**
31
     *
32
     * @var null|\Xparse\ElementFinder\ElementFinder
33
     */
34
    protected $lastPage = null;
35
36
    /**
37
     * @var ClientInterface
38
     */
39
    protected $client = null;
40
41
    /**
42
     * @var null|ResponseInterface
43
     */
44
    protected $lastResponse = null;
45
46
47
    /**
48
     *
49
     * @param ClientInterface|null $client
50
     */
51 6
    public function __construct(ClientInterface $client = null) {
52 6
      if (empty($client)) {
53 1
        $client = new \GuzzleHttp\Client([
54 1
          \GuzzleHttp\RequestOptions::ALLOW_REDIRECTS => true,
55 1
        ]);
56
57 1
      }
58
59 6
      $this->client = $client;
60 6
    }
61
62
63
    /**
64
     * @param string $url
65
     * @return \Xparse\ElementFinder\ElementFinder
66
     */
67 3
    public function get($url) {
68 3
      if (empty($url) or !is_string($url)) {
69 1
        throw new \InvalidArgumentException("Url must be not empty and string.");
70
      }
71
72 2
      $request = new \GuzzleHttp\Psr7\Request('GET', $url);
73 2
      return $this->send($request);
74
    }
75
76
77
    /**
78
     * @param string $url
79
     * @param array $data
80
     * @return \Xparse\ElementFinder\ElementFinder
81
     */
82 2
    public function post($url, $data) {
83
84 2
      if (empty($url) or !is_string($url)) {
85 1
        throw new \InvalidArgumentException("Url must be not empty and string.");
86
      }
87
88 1
      $request = new Request('POST', $url, [
89 1
        'body' => $data,
90 1
      ]);
91
92 1
      return $this->send($request);
93
    }
94
95
96
    /**
97
     * @return \Xparse\ElementFinder\ElementFinder
98
     */
99 2
    public function getLastPage() {
100 2
      return $this->lastPage;
101
    }
102
103
104
    /**
105
     * @param \Xparse\ElementFinder\ElementFinder $lastPage
106
     * @return $this
107
     */
108 3
    public function setLastPage($lastPage) {
109 3
      $this->lastPage = $lastPage;
110 3
      return $this;
111
    }
112
113
114
    /**
115
     * @return null|ResponseInterface
116
     */
117 1
    public function getLastResponse() {
118 1
      return $this->lastResponse;
119
    }
120
121
122
    /**
123
     * @return ClientInterface
124
     */
125 3
    public function getClient() {
126 3
      return $this->client;
127
    }
128
129
130
    /**
131
     *
132
     * @return boolean
133
     */
134
    public function getConvertRelativeLinksState() {
135
      return $this->convertRelativeLinksState;
136
    }
137
138
139
    /**
140
     * @param boolean $convertRelativeLinksState
141
     * @return $this
142
     */
143
    public function setConvertRelativeLinksState($convertRelativeLinksState) {
144
      $this->convertRelativeLinksState = $convertRelativeLinksState;
145
      return $this;
146
    }
147
148
149
    /**
150
     * @return boolean
151
     */
152
    public function getConvertEncodingState() {
153
      return $this->convertEncodingState;
154
    }
155
156
157
    /**
158
     * @param boolean $convertEncodingState
159
     * @return $this
160
     */
161
    public function setConvertEncodingState($convertEncodingState) {
162
      $this->convertEncodingState = $convertEncodingState;
163
      return $this;
164
    }
165
166
167
    /**
168
     * @param $request
169
     * @param array $options
170
     * @return \Xparse\ElementFinder\ElementFinder
171
     * @throws \Exception
172
     */
173 3
    public function send(RequestInterface $request, $options = []) {
174
      /** @var RequestInterface $lastRequest */
175 3
      $lastRequest = null;
176
177
178
      /** @var HandlerStack $handler */
179 3
      $stack = $this->client->getConfig('handler');
180
181 3
      if (!empty($stack) and $stack instanceof HandlerStack) {
182
        $stack->remove('last_request');
183
        $stack->push(\GuzzleHttp\Middleware::mapRequest(function (RequestInterface $request) use (&$lastRequest) {
184
          $lastRequest = $request;
185
          return $request;
186
        }), 'last_request');
187
188
      }
189 3
      $response = $this->client->send($request, $options);
190
191
192 3
      $htmlCode = Helper::safeEncodeStr((string) $response->getBody());
193 3
      $htmlCode = mb_convert_encoding($htmlCode, 'HTML-ENTITIES', "UTF-8");
194
195
      //@todo convert encoding
196
197 3
      $page = new ElementFinder((string) $htmlCode);
198
199 3
      if ($this->convertRelativeLinksState) {
200 3
        $url = (!empty($lastRequest)) ? $lastRequest->getUri()->__toString() : $request->getUri()->__toString();
201 3
        LinkConverter::convertUrlsToAbsolute($page, $url);
202 3
      }
203
204
205 3
      $this->setLastPage($page);
206 3
      $this->lastResponse = $response;
207 3
      return $page;
208
    }
209
210
  }
211