Passed
Branch 3.0.0 (0ebb76)
by Pieter
02:27
created

ChainableFactory   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 29
c 1
b 0
f 0
dl 0
loc 87
rs 10
wmc 17

5 Methods

Rating   Name   Duplication   Size   Complexity  
A hasApiResourcePersisterInstance() 0 11 4
A hasApiResourceRetrieverInstance() 0 11 4
A getApiResourcePersisterInstance() 0 11 4
A getApiResourceRetrieverInstance() 0 11 4
A __construct() 0 3 1
1
<?php
2
3
4
namespace W2w\Lib\Apie\Core\ResourceFactories;
5
6
use W2w\Lib\Apie\Exceptions\CouldNotConstructApiResourceClassException;
7
use W2w\Lib\Apie\Interfaces\ApiResourceFactoryInterface;
8
use W2w\Lib\Apie\Interfaces\ApiResourcePersisterInterface;
9
use W2w\Lib\Apie\Interfaces\ApiResourceRetrieverInterface;
10
11
class ChainableFactory implements ApiResourceFactoryInterface
12
{
13
    /**
14
     * @var ApiResourceRetrieverInterface[]
15
     */
16
    private $instantiatedRetrievers = [];
17
18
    /**
19
     * @var ApiResourcePersisterInterface[]
20
     */
21
    private $instantiatedPersisters = [];
22
23
    /**
24
     * @var ApiResourceFactoryInterface[]
25
     */
26
    private $factories;
27
28
    /**
29
     * @var ApiResourceFactoryInterface[]
30
     */
31
    public function __construct(array $factories)
32
    {
33
        $this->factories = $factories;
34
    }
35
36
    /**
37
     * {@inheritDoc}
38
     */
39
    public function hasApiResourceRetrieverInstance(string $identifier): bool
40
    {
41
        if (isset($this->instantiatedRetrievers[$identifier])) {
42
            return true;
43
        }
44
        foreach ($this->factories as $factory) {
45
            if ($factory->hasApiResourceRetrieverInstance($identifier)) {
46
                return true;
47
            }
48
        }
49
        return false;
50
    }
51
52
    /**
53
     * {@inheritDoc}
54
     */
55
    public function getApiResourceRetrieverInstance(string $identifier): ApiResourceRetrieverInterface
56
    {
57
        if (isset($this->instantiatedRetrievers[$identifier])) {
58
            return $this->instantiatedRetrievers[$identifier];
59
        }
60
        foreach ($this->factories as $factory) {
61
            if ($factory->hasApiResourceRetrieverInstance($identifier)) {
62
                return $this->instantiatedRetrievers[$identifier] = $factory->getApiResourceRetrieverInstance($identifier);
63
            }
64
        }
65
        throw new CouldNotConstructApiResourceClassException($identifier);
66
    }
67
68
    /**
69
     * {@inheritDoc}
70
     */
71
    public function hasApiResourcePersisterInstance(string $identifier): bool
72
    {
73
        if (isset($this->instantiatedPersisters[$identifier])) {
74
            return true;
75
        }
76
        foreach ($this->factories as $factory) {
77
            if ($factory->hasApiResourcePersisterInstance($identifier)) {
78
                return true;
79
            }
80
        }
81
        return false;
82
    }
83
84
    /**
85
     * {@inheritDoc}
86
     */
87
    public function getApiResourcePersisterInstance(string $identifier): ApiResourcePersisterInterface
88
    {
89
        if (isset($this->instantiatedPersisters[$identifier])) {
90
            return $this->instantiatedPersisters[$identifier];
91
        }
92
        foreach ($this->factories as $factory) {
93
            if ($factory->hasApiResourcePersisterInstance($identifier)) {
94
                return $this->instantiatedPersisters[$identifier] = $factory->getApiResourcePersisterInstance($identifier);
95
            }
96
        }
97
        throw new CouldNotConstructApiResourceClassException($identifier);
98
    }
99
}
100