AbstractRequest   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 138
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 22
dl 0
loc 138
rs 10
c 0
b 0
f 0
wmc 13

11 Methods

Rating   Name   Duplication   Size   Complexity  
A getBody() 0 3 1
A setClient() 0 5 1
A __construct() 0 2 1
A getDomain() 0 3 1
A create() 0 3 1
A getUrl() 0 3 1
A getClient() 0 7 2
A getMethod() 0 3 1
A getModel() 0 7 2
A send() 0 3 1
A getHeaders() 0 3 1
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace Korobovn\CloudPayments\Message\Request;
6
7
use Korobovn\CloudPayments\Message\Request\Exception\ClientCannotBeNull;
8
use Tarampampam\Wrappers\Json;
9
use Korobovn\CloudPayments\Client\ClientInterface;
10
use Korobovn\CloudPayments\Message\Response\ResponseInterface;
11
use Korobovn\CloudPayments\Message\Strategy\StrategyInterface;
12
use Korobovn\CloudPayments\Message\Request\Model\ModelInterface;
13
14
abstract class AbstractRequest implements RequestInterface
15
{
16
    protected const API_CLOUD_PAYMENT_DOMAIN = 'https://api.cloudpayments.ru/';
17
18
    /**
19
     * @var ModelInterface|null
20
     */
21
    protected $model;
22
23
    /**
24
     * @var string
25
     */
26
    protected $method = 'POST';
27
28
    /**
29
     * @var array
30
     */
31
    protected $headers = [
32
        'Content-Type' => 'application/json',
33
    ];
34
35
    /**
36
     * @var ClientInterface|null
37
     */
38
    protected $client;
39
40
    protected function __construct()
41
    {
42
    }
43
44
    /**
45
     * @return RequestInterface
46
     */
47
    public static function create(): RequestInterface
48
    {
49
        return new static;
50
    }
51
52
    /**
53
     * Returns relative url of request (without domain part)
54
     *
55
     * @return string
56
     */
57
    abstract protected function getRelativeUrl(): string;
58
59
    /**
60
     * {@inheritDoc}
61
     */
62
    public function getUrl(): string
63
    {
64
        return rtrim($this->getDomain(), '/') . '/' . ltrim($this->getRelativeUrl(), '/');
65
    }
66
67
    /**
68
     * Main domain name of cloud payments api server
69
     *
70
     * @return string
71
     */
72
    protected function getDomain(): string
73
    {
74
        return static::API_CLOUD_PAYMENT_DOMAIN;
75
    }
76
77
    /**
78
     * {@inheritDoc}
79
     */
80
    public function getModel(): ModelInterface
81
    {
82
        if ($this->model === null) {
83
            $this->model = $this->createModel();
84
        }
85
86
        return $this->model;
87
    }
88
89
    /**
90
     * @return ModelInterface
91
     */
92
    abstract protected function createModel(): ModelInterface;
93
94
    /**
95
     * {@inheritDoc}
96
     */
97
    abstract public function getStrategy(): StrategyInterface;
98
99
    /**
100
     * {@inheritDoc}
101
     */
102
    public function getBody(): ?string
103
    {
104
        return Json::encode($this->getModel()->toArray());
105
    }
106
107
    /**
108
     * {@inheritDoc}
109
     */
110
    public function getMethod(): string
111
    {
112
        return $this->method;
113
    }
114
115
    /**
116
     * {@inheritDoc}
117
     */
118
    public function getHeaders(): array
119
    {
120
        return $this->headers;
121
    }
122
123
    /**
124
     * {@inheritDoc}
125
     */
126
    public function setClient(ClientInterface $client): RequestInterface
127
    {
128
        $this->client = $client;
129
130
        return $this;
131
    }
132
133
    /**
134
     * @return ClientInterface
135
     * @throws \LogicException
136
     */
137
    protected function getClient(): ClientInterface
138
    {
139
        if ($this->client === null) {
140
            throw new ClientCannotBeNull('The client cannot be null');
141
        }
142
143
        return $this->client;
144
    }
145
146
    /**
147
     * {@inheritDoc}
148
     */
149
    public function send(): ResponseInterface
150
    {
151
        return $this->getClient()->send($this);
152
    }
153
}
154