Passed
Push — master ( 4ca145...e39a6b )
by Jakub
02:24
created

PathParamsMapper::map()   D

Complexity

Conditions 10
Paths 10

Size

Total Lines 33
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 33
c 0
b 0
f 0
rs 4.8196
cc 10
eloc 19
nc 10
nop 2

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
declare(strict_types=1);
3
4
namespace Eps\Req2CmdBundle\Params\ParameterMapper;
5
6
use Eps\Req2CmdBundle\Exception\ParamMapperException;
7
use Symfony\Component\HttpFoundation\Request;
8
9
class PathParamsMapper implements ParamMapperInterface
10
{
11
    private const REQ_CHAR = '!';
12
13
    public function map(Request $request, array $propsMap): array
14
    {
15
        if (!array_key_exists('path', $propsMap)) {
16
            return [];
17
        }
18
19
        $pathProps = (array)$propsMap['path'];
20
        $result = [];
21
        foreach ($pathProps as $propName => $propValue) {
22
            $required = false;
23
            if (strpos($propName, self::REQ_CHAR) === 0) {
24
                $required = true;
25
                $propName = ltrim($propName, self::REQ_CHAR);
26
            }
27
28
            if ($required && $request->attributes->has($propName) && empty($request->attributes->get($propName))) {
29
                throw ParamMapperException::paramEmpty($propName);
30
            }
31
32
            if (!$request->attributes->has($propName)) {
33
                if ($required && $request->attributes->get($propName) === null) {
34
                    throw ParamMapperException::noParamFound($propName);
35
                }
36
37
                continue;
38
            }
39
40
            $finalPropName = $propValue ?? $propName;
41
            $result[$finalPropName] = $request->attributes->get($propName);
42
        }
43
44
        return $result;
45
    }
46
}
47