Receipt::json()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 5
nc 2
nop 1
dl 0
loc 11
rs 10
c 1
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace Shoman4eg\Nalog\Api;
5
6
use Psr\Http\Client\ClientExceptionInterface;
7
use Psr\Http\Client\ClientInterface;
8
use Psr\Http\Message\ResponseInterface;
9
use Shoman4eg\Nalog\ErrorHandler;
10
use Shoman4eg\Nalog\Exception;
11
use Shoman4eg\Nalog\Model\User\UserType;
12
use Shoman4eg\Nalog\RequestBuilder;
13
use Webmozart\Assert\Assert;
14
15
/**
16
 * @author Artem Dubinin <[email protected]>
17
 */
18
final class Receipt extends BaseHttpApi
19
{
20
    private UserType $profile;
21
    private string $endpoint;
22
23
    public function __construct(
24
        ClientInterface $httpClient,
25
        RequestBuilder $requestBuilder,
26
        UserType $profile,
27
        string $endpoint
28
    ) {
29
        parent::__construct($httpClient, $requestBuilder);
30
        $this->profile = $profile;
31
        $this->endpoint = $endpoint;
32
    }
33
34
    public function printUrl(string $receiptUuid): string
35
    {
36
        return sprintf('%s%s', $this->endpoint, $this->composePrintUrl($receiptUuid));
37
    }
38
39
    /**
40
     * @throws ClientExceptionInterface
41
     * @throws Exception\DomainException
42
     *
43
     * @deprecated
44
     */
45
    public function print(string $receiptUuid): ResponseInterface
46
    {
47
        Assert::notEmpty($receiptUuid);
48
49
        $response = $this->httpGet($this->composePrintUrl($receiptUuid));
50
51
        if ($response->getStatusCode() >= 400) {
52
            (new ErrorHandler())->handleResponse($response);
53
        }
54
55
        return $response;
56
    }
57
58
    /**
59
     * @throws ClientExceptionInterface
60
     * @throws Exception\DomainException
61
     */
62
    public function json(string $receiptUuid): string
63
    {
64
        Assert::notEmpty($receiptUuid);
65
66
        $response = $this->httpGet(sprintf('/receipt/%s/%s/json', $this->profile->getInn(), $receiptUuid));
67
68
        if ($response->getStatusCode() >= 400) {
69
            (new ErrorHandler())->handleResponse($response);
70
        }
71
72
        return $response->getBody()->__toString();
73
    }
74
75
    private function composePrintUrl(string $receiptUuid): string
76
    {
77
        Assert::notEmpty($receiptUuid);
78
79
        return sprintf('/receipt/%s/%s/print', $this->profile->getInn(), $receiptUuid);
80
    }
81
}
82