Passed
Push — develop ( 63565e...e47965 )
by Brent
07:18
created

AdapterFactory::addAdapter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Brendt\Stitcher\Factory;
4
5
use Brendt\Stitcher\Adapter\Adapter;
6
use Brendt\Stitcher\Exception\UnknownAdapterException;
7
use Symfony\Component\DependencyInjection\ContainerInterface;
8
9
class AdapterFactory
10
{
11
    const COLLECTION_ADAPTER = 'collection';
12
    const PAGINATION_ADAPTER = 'pagination';
13
    const ORDER_ADAPTER = 'order';
14
    const FILTER_ADAPTER = 'filter';
15
    const LIMIT_ADAPTER = 'limit';
16
17
    private $container;
18
    private $adapters = [];
19
20 57
    public function __construct(ContainerInterface $container)
21
    {
22 57
        $this->container = $container;
23
24
        $this->addAdapter(self::COLLECTION_ADAPTER, function () {
25 13
            return $this->container->get('adapter.collection');
26 57
        });
27
28
        $this->addAdapter(self::PAGINATION_ADAPTER, function () {
29
            return $this->container->get('adapter.pagination');
30 57
        });
31
32
        $this->addAdapter(self::ORDER_ADAPTER, function () {
33 1
            return $this->container->get('adapter.order');
34 57
        });
35
36
        $this->addAdapter(self::FILTER_ADAPTER, function () {
37 1
            return $this->container->get('adapter.filter');
38 57
        });
39
40 57
        $this->addAdapter(self::LIMIT_ADAPTER, function () {
41
            return $this->container->get('adapter.limit');
42 57
        });
43 57
    }
44
45 57
    public function addAdapter(string $adapterName, callable $filter)
46
    {
47 57
        $this->adapters[$adapterName] = $filter;
48 57
    }
49
50 14
    public function getByType($type) : Adapter
51
    {
52 14
        if (!isset($this->adapters[$type])) {
53
            throw new UnknownAdapterException();
54
        }
55
56 14
        return $this->adapters[$type]($this->container);
57
    }
58
}
59