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 ( f149bc...b3a1b8 )
by Shcherbak
03:19
created

Parser::post()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 9
Bugs 0 Features 1
Metric Value
c 9
b 0
f 1
dl 0
loc 12
ccs 7
cts 7
cp 1
rs 9.4285
cc 3
eloc 6
nc 2
nop 3
crap 3
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 7
    public function __construct(ClientInterface $client = null, ElementFinderFactoryInterface $elementFinderFactory = null) {
49 7
      if ($client === null) {
50 1
        $client = new Client([
51 1
          RequestOptions::ALLOW_REDIRECTS => true,
52 1
        ]);
53
54 1
      }
55
56 7
      if ($elementFinderFactory === null) {
57 7
        $elementFinderFactory = new ElementFinderFactory();
58 7
      }
59
60 7
      $this->elementFinderFactory = $elementFinderFactory;
61
62 7
      $this->client = $client;
63 7
    }
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 array $data
86
     * @param array $options
87
     * @return ElementFinder
88
     * @throws \Exception
89
     * @throws \InvalidArgumentException
90
     */
91 2
    public function post($url, $data, 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, [
98 1
        'body' => $data,
99 1
      ]);
100
101 1
      return $this->send($request, $options);
102
    }
103
104
105
    /**
106
     * @return \Xparse\ElementFinder\ElementFinder
107
     */
108 2
    public function getLastPage() {
109 2
      return $this->lastPage;
110
    }
111
112
113
    /**
114
     * @param \Xparse\ElementFinder\ElementFinder $lastPage
115
     * @return $this
116
     */
117 4
    public function setLastPage($lastPage) {
118 4
      $this->lastPage = $lastPage;
119 4
      return $this;
120
    }
121
122
123
    /**
124
     * @return null|ResponseInterface
125
     */
126 1
    public function getLastResponse() {
127 1
      return $this->lastResponse;
128
    }
129
130
131
    /**
132
     * @return ClientInterface
133
     */
134 3
    public function getClient() {
135 3
      return $this->client;
136
    }
137
138
139
    /**
140
     * @param $request
141
     * @param array $options
142
     * @return \Xparse\ElementFinder\ElementFinder
143
     * @throws \Exception
144
     */
145 4
    public function send(RequestInterface $request, array $options = []) {
146
147 4
      $prevCallback = !empty($options['on_stats']) ? $options['on_stats'] : null;
148
149 4
      $effectiveUri = null;
150 4
      $options['on_stats'] = function (TransferStats $stats) use (&$effectiveUri, $prevCallback) {
151 4
        $effectiveUri = $stats->getEffectiveUri();
152 4
        if ($prevCallback !== null) {
153
          /** @var callable $prevCallback */
154
          $prevCallback($stats);
155
        }
156 4
      };
157
158 4
      $response = $this->client->send($request, $options);
159 4
      $page = $this->elementFinderFactory->create($response, $effectiveUri);
160
161 4
      $this->setLastPage($page);
162 4
      $this->lastResponse = $response;
163 4
      return $page;
164
    }
165
166
  }
167