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

ParamCollector::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
c 0
b 0
f 0
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
1
<?php
2
declare(strict_types=1);
3
4
namespace Eps\Req2CmdBundle\Params\ParamCollector;
5
6
use Eps\Req2CmdBundle\Params\ParameterMapper\ParamMapperInterface;
7
use Symfony\Component\HttpFoundation\Request;
8
9
class ParamCollector implements ParamCollectorInterface
10
{
11
    /**
12
     * @var ParamMapperInterface[]
13
     */
14
    private $mappers;
15
16
    public function __construct(array $mappers)
17
    {
18
        $this->mappers = [];
19
        foreach ($mappers as $mapper) {
20
            $this->addMapper($mapper);
21
        }
22
    }
23
24
    public function collect(Request $request, array $propsMap): array
25
    {
26
        return array_reduce(
27
            $this->mappers,
28
            function (array $carry, ParamMapperInterface $mapper) use ($request, $propsMap) {
29
                $carry = array_merge($carry, $mapper->map($request, $propsMap));
30
                return $carry;
31
            },
32
            []
33
        );
34
    }
35
36
    private function addMapper(ParamMapperInterface $mapper): void
37
    {
38
        $this->mappers[] = $mapper;
39
    }
40
}
41