FeaturetoggleRegistry   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 43
wmc 5
lcom 1
cbo 0
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A retrieve() 0 15 3
A push() 0 5 1
1
<?php
2
3
namespace Jcowie\FeatureToggle\Model;
4
5
use Magento\Framework\Exception\NoSuchEntityException;
6
use Jcowie\FeatureToggle\Api\Data\FeaturetoggleInterfaceFactory;
7
use Jcowie\FeatureToggle\Model\Data\Featuretoggle;
8
9
class FeaturetoggleRegistry
10
{
11
    /**
12
     * @var FeaturetoggleInterfaceFactory
13
     */
14
    private $featuretoggleFactory;
15
16
    private $featuretoggleRegistryById;
17
18
    public function __construct(FeaturetoggleInterfaceFactory $featuretoggleInterfaceFactory)
19
    {
20
        $this->featuretoggleFactory = $featuretoggleInterfaceFactory;
21
    }
22
23
24
    public function retrieve($featuretoggleId)
25
    {
26
        if (isset($this->featuretoggleRegistryById[$featuretoggleId])) {
27
            return $this->featuretoggleRegistryById[$featuretoggleId];
28
        }
29
        /** @var Warehouse $warehouse */
30
        $featureToggle = $this->featuretoggleFactory->create()->load($featuretoggleId);
31
        if (!$featureToggle->getFeaturetoggleId()) {
32
            // featureToggle does not exist
33
            throw NoSuchEntityException::singleField('featuretoggleId', $featuretoggleId);
34
        } else {
35
            $this->featuretoggleRegistryById[$featuretoggleId] = $featureToggle;
36
            return $featureToggle;
37
        }
38
    }
39
    /**
40
     * Replace existing feature toggle model with a new one.
41
     *
42
     * @param Featuretoggle $featuretoggle
43
     *
44
     * @return $this
45
     */
46
    public function push(Featuretoggle $featuretoggle)
47
    {
48
        $this->featuretoggleRegistryById[$featuretoggle->getId()] = $featuretoggle;
49
        return $this;
50
    }
51
}
52