1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Copyright (c) 2016, HelloFresh GmbH. |
4
|
|
|
* All rights reserved. |
5
|
|
|
* |
6
|
|
|
* This source code is licensed under the MIT license found in the |
7
|
|
|
* LICENSE file in the root directory of this source tree. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace HelloFresh\FeatureToggle; |
11
|
|
|
|
12
|
|
|
use Collections\Dictionary; |
13
|
|
|
use Collections\MapInterface; |
14
|
|
|
use HelloFresh\FeatureToggle\Exception\FeatureAlreadyExistsException; |
15
|
|
|
use HelloFresh\FeatureToggle\Exception\FeatureNotFoundException; |
16
|
|
|
|
17
|
|
|
class FeatureManager |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var MapInterface |
21
|
|
|
*/ |
22
|
|
|
protected $features; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* FeatureManager constructor. |
26
|
|
|
* @param MapInterface $features |
27
|
|
|
*/ |
28
|
6 |
|
public function __construct(MapInterface $features = null) |
29
|
|
|
{ |
30
|
6 |
|
$this->features = $features ? $features : new Dictionary(); |
31
|
6 |
|
} |
32
|
|
|
|
33
|
5 |
|
public function addFeature(FeatureInterface $feature) |
34
|
|
|
{ |
35
|
5 |
|
if ($this->has($feature->getName())) { |
36
|
1 |
|
throw new FeatureAlreadyExistsException(sprintf('The feature %s already exists', $feature->getName())); |
37
|
|
|
} |
38
|
|
|
|
39
|
5 |
|
$this->features->add($feature->getName(), $feature); |
40
|
|
|
|
41
|
5 |
|
return $this; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function removeFeature(FeatureInterface $feature) |
45
|
|
|
{ |
46
|
|
|
$this->features->removeKey($feature->getName()); |
47
|
|
|
|
48
|
|
|
return $this; |
49
|
|
|
} |
50
|
|
|
|
51
|
6 |
|
public function has($name) |
52
|
|
|
{ |
53
|
6 |
|
return $this->features->containsKey($name); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Gets a feature toggle |
58
|
|
|
* @param $name - The name of the feature |
59
|
|
|
* @return FeatureInterface |
60
|
|
|
*/ |
61
|
5 |
|
public function get($name) |
62
|
|
|
{ |
63
|
5 |
|
if (!$this->has($name)) { |
64
|
1 |
|
throw new FeatureNotFoundException(sprintf('The feature %s was not found', $name)); |
65
|
|
|
} |
66
|
|
|
|
67
|
4 |
|
return $this->features->get($name); |
68
|
|
|
} |
69
|
|
|
|
70
|
3 |
|
public function isActive($name, Context $context) |
71
|
|
|
{ |
72
|
3 |
|
return $this->get($name)->activeFor($context); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|