prepareRequestPathForResourceMethod()   D
last analyzed

Complexity

Conditions 20
Paths 20

Size

Total Lines 45

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 31
CRAP Score 20.0121

Importance

Changes 0
Metric Value
dl 0
loc 45
ccs 31
cts 32
cp 0.9688
rs 4.1666
c 0
b 0
f 0
cc 20
nc 20
nop 3
crap 20.0121

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