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\Helpers; |
13
|
|
|
|
14
|
|
|
use Zibios\WrikePhpLibrary\Enum\Api\ResourceMethodEnum; |
15
|
|
|
use Zibios\WrikePhpLibrary\Validator\IdValidator; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Request Path Processor. |
19
|
|
|
*/ |
20
|
|
|
class RequestPathProcessor |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* Combine ResourceMethodConfiguration, ResourceMethod and optional Id |
24
|
|
|
* to retrieve correct resource path for request. |
25
|
|
|
* |
26
|
|
|
* @param string $resourceMethod |
27
|
|
|
* @param string|array|null $id |
28
|
|
|
* @param array $resourceMethodConfiguration |
29
|
|
|
* |
30
|
|
|
* @SuppressWarnings(PHPMD.CyclomaticComplexity) |
31
|
|
|
* |
32
|
|
|
* @throws \InvalidArgumentException |
33
|
|
|
* |
34
|
|
|
* @return string |
35
|
|
|
*/ |
36
|
111 |
|
public function prepareRequestPathForResourceMethod( |
37
|
|
|
$resourceMethod, |
38
|
|
|
$id, |
39
|
|
|
array $resourceMethodConfiguration |
40
|
|
|
) { |
41
|
111 |
|
$requestPathFormat = $this->calculateRequestPathFormat($resourceMethod, $resourceMethodConfiguration); |
42
|
|
|
|
43
|
|
|
switch ($resourceMethod) { |
44
|
109 |
|
case ResourceMethodEnum::GET_ALL: |
45
|
13 |
|
IdValidator::assertIsNull($id); |
46
|
12 |
|
$path = $requestPathFormat; |
47
|
12 |
|
break; |
48
|
|
|
|
49
|
96 |
|
case ResourceMethodEnum::GET_BY_IDS: |
50
|
9 |
|
IdValidator::assertIsValidIdArray($id); |
51
|
8 |
|
$path = sprintf($requestPathFormat, implode(',', $id)); |
52
|
8 |
|
break; |
53
|
|
|
|
54
|
87 |
|
case ResourceMethodEnum::GET_ALL_FOR_ACCOUNT: |
55
|
75 |
|
case ResourceMethodEnum::GET_ALL_FOR_FOLDER: |
56
|
69 |
|
case ResourceMethodEnum::GET_ALL_FOR_TASK: |
57
|
64 |
|
case ResourceMethodEnum::GET_ALL_FOR_CONTACT: |
58
|
62 |
|
case ResourceMethodEnum::GET_ALL_FOR_TIMELOG_CATEGORY: |
59
|
60 |
|
case ResourceMethodEnum::CREATE_FOR_ACCOUNT: |
60
|
55 |
|
case ResourceMethodEnum::CREATE_FOR_FOLDER: |
61
|
51 |
|
case ResourceMethodEnum::CREATE_FOR_TASK: |
62
|
47 |
|
case ResourceMethodEnum::GET_BY_ID: |
63
|
35 |
|
case ResourceMethodEnum::UPDATE: |
64
|
21 |
|
case ResourceMethodEnum::DELETE: |
65
|
12 |
|
case ResourceMethodEnum::COPY: |
66
|
10 |
|
case ResourceMethodEnum::DOWNLOAD: |
67
|
8 |
|
case ResourceMethodEnum::DOWNLOAD_PREVIEW: |
68
|
6 |
|
case ResourceMethodEnum::GET_PUBLIC_URL: |
69
|
4 |
|
case ResourceMethodEnum::UPLOAD_FOR_FOLDER: |
70
|
2 |
|
case ResourceMethodEnum::UPLOAD_FOR_TASK: |
71
|
87 |
|
IdValidator::assertIsValidIdString($id); |
72
|
86 |
|
$path = sprintf($requestPathFormat, $id); |
73
|
86 |
|
break; |
74
|
|
|
|
75
|
|
|
default: |
76
|
|
|
throw new \InvalidArgumentException(sprintf('"%s" resource method not yet supported', $resourceMethod)); |
77
|
|
|
} |
78
|
|
|
|
79
|
106 |
|
return $path; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param string $resourceMethod |
84
|
|
|
* @param array $resourceMethodConfiguration |
85
|
|
|
* |
86
|
|
|
* @throws \InvalidArgumentException |
87
|
|
|
* |
88
|
|
|
* @return string |
89
|
|
|
*/ |
90
|
111 |
|
private function calculateRequestPathFormat($resourceMethod, array $resourceMethodConfiguration) |
91
|
|
|
{ |
92
|
111 |
|
if (false === array_key_exists($resourceMethod, $resourceMethodConfiguration)) { |
93
|
2 |
|
throw new \InvalidArgumentException( |
94
|
2 |
|
sprintf( |
95
|
2 |
|
'Resource "%s" Method not found in configuration keys [%s]', |
96
|
2 |
|
$resourceMethod, |
97
|
2 |
|
implode(', ', array_keys($resourceMethodConfiguration)) |
98
|
|
|
) |
99
|
|
|
); |
100
|
|
|
} |
101
|
|
|
|
102
|
109 |
|
return $resourceMethodConfiguration[$resourceMethod]; |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|