Passed
Push — master ( ee6060...4c211f )
by Brent
02:40
created

AdapterFactory   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 68
rs 10
c 0
b 0
f 0
wmc 4
lcom 1
cbo 2

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 23 1
A addAdapter() 0 3 1
A getByType() 0 7 2
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
    /**
18
     * @var ContainerInterface
19
     */
20
    private $container;
21
22
    private $adapters = [];
23
24
    /**
25
     * AdapterFactory constructor.
26
     *
27
     * @param ContainerInterface $container
28
     */
29
    public function __construct(ContainerInterface $container) {
30
        $this->container = $container;
31
32
        $this->addAdapter(self::COLLECTION_ADAPTER, function () {
33
            return $this->container->get('adapter.collection');
34
        });
35
36
        $this->addAdapter(self::PAGINATION_ADAPTER, function () {
37
            return $this->container->get('adapter.pagination');
38
        });
39
40
        $this->addAdapter(self::ORDER_ADAPTER, function () {
41
            return $this->container->get('adapter.order');
42
        });
43
44
        $this->addAdapter(self::FILTER_ADAPTER, function () {
45
            return $this->container->get('adapter.filter');
46
        });
47
48
        $this->addAdapter(self::LIMIT_ADAPTER, function () {
49
            return $this->container->get('adapter.limit');
50
        });
51
    }
52
53
    /**
54
     * @param string   $adapterName
55
     * @param callable $filter
56
     */
57
    public function addAdapter(string $adapterName, callable $filter) {
58
        $this->adapters[$adapterName] = $filter;
59
    }
60
61
    /**
62
     * @param $type
63
     *
64
     * @return mixed
65
     *
66
     * @throws UnknownAdapterException
67
     */
68
    public function getByType($type) : Adapter {
69
        if (!isset($this->adapters[$type])) {
70
            throw new UnknownAdapterException();
71
        }
72
73
        return $this->adapters[$type]($this->container);
74
    }
75
76
}
77