Passed
Push — master ( 023315...1fcf08 )
by Evgenii
14:57
created

DalliClient   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 17
c 1
b 0
f 0
dl 0
loc 55
ccs 17
cts 17
cp 1
rs 10
wmc 5

4 Methods

Rating   Name   Duplication   Size   Complexity  
A sendApiRequest() 0 6 1
A __construct() 0 4 1
A parseErrors() 0 8 2
A getErrors() 0 3 1
1
<?php
2
3
namespace floor12\DalliApi;
4
5
use GuzzleHttp\ClientInterface;
6
use GuzzleHttp\Exception\GuzzleException;
7
use GuzzleHttp\Psr7\Request;
8
use Psr\Http\Message\ResponseInterface;
9
10
class DalliClient
11
{
12
    /**
13
     * @var ClientInterface
14
     */
15
    private $client;
16
    /**
17
     * @var string
18
     */
19
    private $dalliEndpoint;
20
    /** @var array */
21
    private $errors = [];
22
    /** @var ResponseInterface */
23
    private $response;
24
25
    /**
26
     * DalliClient constructor.
27
     * @param string $dalliEndpoint
28
     * @param ClientInterface $client
29
     */
30 2
    public function __construct(string $dalliEndpoint, ClientInterface $client)
31
    {
32 2
        $this->client = $client;
33 2
        $this->dalliEndpoint = $dalliEndpoint;
34 2
    }
35
36
    /**
37
     * @return bool
38
     */
39 2
    private function parseErrors(): bool
40
    {
41 2
        $pattern = '/errorMessage=\'(.+)\'/';
42 2
        if (preg_match_all($pattern, $this->response->getBody()->getContents(), $matches)) {
43 1
            $this->errors = $matches[1];
44 1
            return false;
45
        }
46 1
        return true;
47
    }
48
49
    /**
50
     * @param string $body Body as XML string
51
     * @return boolean
52
     * @throws GuzzleException
53
     */
54 2
    public function sendApiRequest(string $body): bool
55
    {
56 2
        $headers = ['Content-type' => 'application/xml'];
57 2
        $request = new Request('POST', $this->dalliEndpoint, $headers, $body);
58 2
        $this->response = $this->client->send($request);
59 2
        return $this->parseErrors();
60
    }
61
62 1
    public function getErrors()
63
    {
64 1
        return $this->errors;
65
    }
66
}
67