Completed
Push — master ( c35835...32add1 )
by Zbigniew
02:35
created

AbstractResource::assertIsValidIdArray()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3.0416

Importance

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