Completed
Push — master ( e7cd46...d02ce0 )
by Zbigniew
04:25
created

RequestPathProcessor::calculateRequestPathFormat()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 6
cts 6
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 8
nc 2
nop 2
crap 2
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 107
    public function prepareRequestPathForResourceMethod($resourceMethod, $id, array $resourceMethodConfiguration)
37
    {
38 107
        $requestPathFormat = $this->calculateRequestPathFormat($resourceMethod, $resourceMethodConfiguration);
39
40
        switch ($resourceMethod) {
41 105
            case ResourceMethodEnum::GET_ALL:
42 12
                IdValidator::assertIsNull($id);
43 11
                $path = sprintf($requestPathFormat, $id);
44 11
                break;
45
46 93
            case ResourceMethodEnum::GET_BY_IDS:
47 8
                IdValidator::assertIsValidIdArray($id);
48 7
                $path = sprintf($requestPathFormat, implode(',', $id));
49 7
                break;
50
51 85
            case ResourceMethodEnum::GET_ALL_FOR_ACCOUNT:
52 73
            case ResourceMethodEnum::GET_ALL_FOR_FOLDER:
53 67
            case ResourceMethodEnum::GET_ALL_FOR_TASK:
54 62
            case ResourceMethodEnum::GET_ALL_FOR_CONTACT:
55 60
            case ResourceMethodEnum::CREATE_FOR_ACCOUNT:
56 55
            case ResourceMethodEnum::CREATE_FOR_FOLDER:
57 51
            case ResourceMethodEnum::CREATE_FOR_TASK:
58 47
            case ResourceMethodEnum::GET_BY_ID:
59 35
            case ResourceMethodEnum::UPDATE:
60 21
            case ResourceMethodEnum::DELETE:
61 12
            case ResourceMethodEnum::COPY:
62 10
            case ResourceMethodEnum::DOWNLOAD:
63 8
            case ResourceMethodEnum::DOWNLOAD_PREVIEW:
64 6
            case ResourceMethodEnum::GET_PUBLIC_URL:
65 4
            case ResourceMethodEnum::UPLOAD_FOR_FOLDER:
66 2
            case ResourceMethodEnum::UPLOAD_FOR_TASK:
67 85
                IdValidator::assertIsValidIdString($id);
68 84
                $path = sprintf($requestPathFormat, $id);
69 84
                break;
70
71
            default:
72
                throw new \InvalidArgumentException(sprintf('"%s" resource method not yet supported', $resourceMethod));
73
        }
74
75 102
        return $path;
76
    }
77
78
    /**
79
     * @param string $resourceMethod
80
     * @param array  $resourceMethodConfiguration
81
     *
82
     * @throws \InvalidArgumentException
83
     *
84
     * @return string
85
     */
86 107
    private function calculateRequestPathFormat($resourceMethod, array $resourceMethodConfiguration)
87
    {
88 107
        if (array_key_exists($resourceMethod, $resourceMethodConfiguration) === false) {
89 2
            throw new \InvalidArgumentException(
90
                sprintf(
91 2
                    'Resource "%s" Method not found in configuration keys [%s]',
92
                    $resourceMethod,
93 2
                    implode(', ', array_keys($resourceMethodConfiguration))
94
                )
95
            );
96
        }
97
98 105
        return $resourceMethodConfiguration[$resourceMethod];
99
    }
100
}
101