Passed
Push — master ( 8bdcbb...696730 )
by Zbigniew
03:20
created

prepareRequestPathForResourceMethod()   D

Complexity

Conditions 17
Paths 17

Size

Total Lines 39
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 28
CRAP Score 17.0118

Importance

Changes 0
Metric Value
dl 0
loc 39
ccs 28
cts 29
cp 0.9655
rs 4.9807
c 0
b 0
f 0
cc 17
eloc 31
nc 17
nop 3
crap 17.0118

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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