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

DalliClient::getErrors()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
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