Passed
Pull Request — master (#4)
by Igor
04:25
created

Client   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Test Coverage

Coverage 95.45%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 26
c 1
b 0
f 1
dl 0
loc 86
ccs 21
cts 22
cp 0.9545
rs 10
wmc 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A detail() 0 7 1
A __construct() 0 6 1
A list() 0 12 2
A handleRequest() 0 12 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SomeWork\Minjust;
6
7
use Psr\Http\Client\ClientExceptionInterface;
8
use Psr\Http\Client\ClientInterface;
9
use Psr\Http\Message\RequestFactoryInterface;
10
use Psr\Http\Message\RequestInterface;
11
use SomeWork\Minjust\Exception\HttpClientException;
12
use SomeWork\Minjust\Exception\WrongStatusCodeException;
13
14
/**
15
 * @see \SomeWork\Minjust\Tests\Unit\ClientTest
16
 */
17
class Client
18
{
19
    /**
20
     * @var string
21
     */
22
    private const SERVICE_URL = 'http://lawyers.minjust.ru';
23
24
    /**
25
     * @var string
26
     */
27
    private const LIST_URL = Client::SERVICE_URL . '/Lawyers';
28
29
    /**
30
     * @var ClientInterface
31
     */
32
    private $client;
33
34
    /**
35
     * @var RequestFactoryInterface
36
     */
37
    private $requestFactory;
38
39 2
    public function __construct(
40
        ClientInterface $client,
41
        RequestFactoryInterface $requestFactory
42
    ) {
43 2
        $this->client = $client;
44 2
        $this->requestFactory = $requestFactory;
45 2
    }
46
47
    /**
48
     * @param array $formData
49
     *
50
     * @return string
51
     * @throws WrongStatusCodeException
52
     * @throws HttpClientException
53
     */
54 3
    public function list(array $formData = []): string
55
    {
56 3
        $query = '';
57 3
        if ([] !== $formData) {
58 2
            $query .= '?' . http_build_query($formData);
59
        }
60
61
        $request = $this
62 3
                ->requestFactory
63 3
                ->createRequest('GET', static::LIST_URL . $query);
64
65 3
        return $this->handleRequest($request);
66
    }
67
68
    /**
69
     * @param string $url
70
     *
71
     * @return string
72
     * @throws WrongStatusCodeException
73
     * @throws HttpClientException
74
     */
75 2
    public function detail(string $url): string
76
    {
77
        $request = $this
78 2
            ->requestFactory
79 2
            ->createRequest('GET', static::SERVICE_URL . $url);
80
81 2
        return $this->handleRequest($request);
82
    }
83
84
    /**
85
     * @param RequestInterface $request
86
     *
87
     * @return string
88
     * @throws WrongStatusCodeException
89
     * @throws HttpClientException
90
     */
91 2
    public function handleRequest(RequestInterface $request):string
92
    {
93
        try {
94 2
            $response = $this->client->sendRequest($request);
95 1
        } catch (ClientExceptionInterface $e) {
96 1
            throw new HttpClientException($e->getMessage(), $e->getCode(), $e);
97
        }
98 1
        if ($response->getStatusCode() === 200) {
99
            return $response->getBody()->getContents();
100
        }
101
102 1
        throw new WrongStatusCodeException($response->getStatusCode());
103
    }
104
}
105