Completed
Push — master ( 1f7cb9...c35835 )
by Zbigniew
09:09
created

AbstractResource   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 163
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 87.5%

Importance

Changes 0
Metric Value
wmc 22
lcom 1
cbo 4
dl 0
loc 163
ccs 49
cts 56
cp 0.875
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
getResourceMethodConfiguration() 0 1 ?
A executeRequest() 0 17 2
A calculateRequestPathFormat() 0 9 2
A assertIsNull() 0 6 2
A assertIsValidIdString() 0 6 4
C prepareRequestPathForResourceMethod() 0 30 8
A assertIsValidIdArray() 0 11 3
1
<?php
2
/**
3
 * This file is part of the WrikePhpLibrary package.
4
 *
5
 * (c) Zbigniew Ślązak
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Zibios\WrikePhpLibrary\Resource;
12
13
use Zibios\WrikePhpLibrary\Client\ClientInterface;
14
use Zibios\WrikePhpLibrary\Enum\Api\RequestMethodEnum;
15
use Zibios\WrikePhpLibrary\Enum\Api\ResourceMethodEnum;
16
use Zibios\WrikePhpLibrary\Transformer\ResponseTransformerInterface;
17
18
/**
19
 * Resource Abstract
20
 *
21
 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
22
 */
23
abstract class AbstractResource implements ResourceInterface
24
{
25
    /**
26
     * @var ClientInterface
27
     */
28
    protected $client;
29
30
    /**
31
     * @var ResponseTransformerInterface
32
     */
33
    protected $responseTransformer;
34
35
    /**
36
     * @param ClientInterface $client
37
     * @param ResponseTransformerInterface $responseTransformer
38
     */
39 40
    public function __construct(
40
        ClientInterface $client,
41
        ResponseTransformerInterface $responseTransformer
42
    ) {
43 40
        $this->client = $client;
44 40
        $this->responseTransformer = $responseTransformer;
45 40
    }
46
47
    /**
48
     * @return array
49
     */
50
    abstract protected function getResourceMethodConfiguration();
51
52
    /**
53
     * @param string $requestMethod
54
     * @param string $resourceMethod
55
     * @param array $params
56
     * @param string|array|null $id
57
     *
58
     * @return mixed
59
     * @throws \RuntimeException
60
     * @throws \LogicException
61
     * @throws \Exception
62
     * @throws \InvalidArgumentException
63
     * @throws \Zibios\WrikePhpLibrary\Exception\Api\ApiException
64
     * @throws \Zibios\WrikePhpLibrary\Exception\Api\AccessForbiddenException
65
     * @throws \Zibios\WrikePhpLibrary\Exception\Api\InvalidParameterException
66
     * @throws \Zibios\WrikePhpLibrary\Exception\Api\InvalidRequestException
67
     * @throws \Zibios\WrikePhpLibrary\Exception\Api\NotAllowedException
68
     * @throws \Zibios\WrikePhpLibrary\Exception\Api\NotAuthorizedException
69
     * @throws \Zibios\WrikePhpLibrary\Exception\Api\ParameterRequiredException
70
     * @throws \Zibios\WrikePhpLibrary\Exception\Api\ResourceNotFoundException
71
     * @throws \Zibios\WrikePhpLibrary\Exception\Api\ServerErrorException
72
     */
73 32
    protected function executeRequest($requestMethod, $resourceMethod, array $params, $id)
74
    {
75 32
        RequestMethodEnum::assertIsValidValue($requestMethod);
76 32
        ResourceMethodEnum::assertIsValidValue($resourceMethod);
77
78
        try {
79 32
            $response = $this->client->executeRequestForParams(
80 32
                $requestMethod,
81 32
                $this->prepareRequestPathForResourceMethod($resourceMethod, $id),
82
                $params
83 32
            );
84 32
        } catch (\Exception $e) {
85 16
            throw $this->client->transformApiException($e);
86
        }
87
88 16
        return $this->responseTransformer->transform($response, static::class);
89
    }
90
91
    /**
92
     * @param string $resourceMethod
93
     * @param string|array|null $id
94
     *
95
     * @return string
96
     * @throws \InvalidArgumentException
97
     */
98 32
    private function prepareRequestPathForResourceMethod($resourceMethod, $id)
99
    {
100 32
        $requestPathFormat = $this->calculateRequestPathFormat($resourceMethod);
101
102
        switch ($resourceMethod) {
103 32
            case ResourceMethodEnum::GET_ALL:
104 2
                $this->assertIsNull($id);
105 2
                $path = sprintf($requestPathFormat, $id);
106 2
                break;
107
108 30
            case ResourceMethodEnum::GET_BY_IDS:
109 2
                $this->assertIsValidIdArray($id);
110 2
                $path = sprintf($requestPathFormat, implode(',', $id));
111 2
                break;
112
113 28
            case ResourceMethodEnum::GET_ALL_IN_ACCOUNT:
114 28
            case ResourceMethodEnum::CREATE_IN_ACCOUNT:
115 28
            case ResourceMethodEnum::GET_BY_ID:
116 28
            case ResourceMethodEnum::UPDATE:
117 28
            case ResourceMethodEnum::DELETE:
118 28
                $this->assertIsValidIdString($id);
119 28
                $path = sprintf($requestPathFormat, $id);
120 28
                break;
121
122
            default:
123
                throw new \InvalidArgumentException(sprintf('"%s" resource method not yet supported', $resourceMethod));
124
        }
125
126 32
        return $path;
127
    }
128
129
    /**
130
     * @param string $resourceMethod
131
     *
132
     * @return string
133
     * @throws \InvalidArgumentException
134
     */
135 32
    private function calculateRequestPathFormat($resourceMethod)
136
    {
137 32
        $resourceMethodConfiguration = $this->getResourceMethodConfiguration();
138 32
        if (array_key_exists($resourceMethod, $resourceMethodConfiguration) === false) {
139
            throw new \InvalidArgumentException();
140
        }
141
142 32
        return $resourceMethodConfiguration[$resourceMethod];
143
    }
144
145
    /**
146
     * @param mixed $value
147
     *
148
     * @throws \InvalidArgumentException
149
     */
150 2
    private function assertIsNull($value)
151
    {
152 2
        if ($value !== null) {
153
            throw new \InvalidArgumentException();
154
        }
155 2
    }
156
157
    /**
158
     * @param mixed $value
159
     *
160
     * @throws \InvalidArgumentException
161
     */
162 30
    private function assertIsValidIdString($value)
163
    {
164 30
        if (is_string($value) === false || trim($value) === '' || strlen($value) > 16) {
165
            throw new \InvalidArgumentException(sprintf('Invalid Id, should be not empty string!'));
166
        }
167 30
    }
168
169
    /**
170
     * @param mixed $value
171
     *
172
     * @throws \InvalidArgumentException
173
     */
174 2
    private function assertIsValidIdArray($value)
175
    {
176 2
        if (is_array($value) === false) {
177
            throw new \InvalidArgumentException();
178
        }
179
180
        /** @var array $value */
181 2
        foreach ($value as $id) {
182 2
            $this->assertIsValidIdString($id);
183 2
        }
184 2
    }
185
}
186