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 ( 18d695...0e4f84 )
by Shcherbak
03:15
created

Parser   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 153
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 95.83%

Importance

Changes 23
Bugs 2 Features 5
Metric Value
wmc 17
c 23
b 2
f 5
lcom 1
cbo 7
dl 0
loc 153
ccs 46
cts 48
cp 0.9583
rs 10

9 Methods

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