|
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\Serializer; |
|
11
|
|
|
|
|
12
|
|
|
use Collections\ArrayList; |
|
13
|
|
|
use Collections\MapInterface; |
|
14
|
|
|
use Collections\VectorInterface; |
|
15
|
|
|
use HelloFresh\FeatureToggle\Feature; |
|
16
|
|
|
use HelloFresh\FeatureToggle\FeatureInterface; |
|
17
|
|
|
use HelloFresh\FeatureToggle\OperatorCondition; |
|
18
|
|
|
use HelloFresh\FeatureToggle\ToggleStatus; |
|
19
|
|
|
use HelloFresh\FeatureToggle\ToggleStrategy; |
|
20
|
|
|
|
|
21
|
|
|
class ToggleSerializer |
|
22
|
|
|
{ |
|
23
|
|
|
private $operatorConditionSerializer; |
|
24
|
|
|
|
|
25
|
1 |
|
public function __construct(OperatorConditionSerializer $operatorConditionSerializer) |
|
26
|
|
|
{ |
|
27
|
1 |
|
$this->operatorConditionSerializer = $operatorConditionSerializer; |
|
28
|
1 |
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @param FeatureInterface $feature |
|
32
|
|
|
* |
|
33
|
|
|
* @return string |
|
34
|
|
|
*/ |
|
35
|
|
|
public function serialize(FeatureInterface $feature) |
|
36
|
|
|
{ |
|
37
|
|
|
return [ |
|
38
|
|
|
'name' => $feature->getName(), |
|
39
|
|
|
'conditions' => $this->serializeConditions($feature->getConditions()), |
|
40
|
|
|
'status' => $this->serializeStatus($feature), |
|
41
|
|
|
'strategy' => $this->serializeStrategy($feature) |
|
42
|
|
|
]; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @param MapInterface $data |
|
47
|
|
|
* |
|
48
|
|
|
* @return FeatureInterface |
|
49
|
|
|
*/ |
|
50
|
1 |
|
public function deserialize(MapInterface $data) |
|
51
|
|
|
{ |
|
52
|
|
|
/** @var MapInterface $conditions */ |
|
53
|
1 |
|
$conditions = $data->get('conditions'); |
|
54
|
|
|
|
|
55
|
1 |
|
if (!$conditions instanceof MapInterface) { |
|
56
|
|
|
throw new \RuntimeException('Key "conditions" should be an MapInterface.'); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
1 |
|
$toggle = new Feature( |
|
60
|
1 |
|
$data->get('name'), |
|
61
|
1 |
|
$this->deserializeConditions($conditions), |
|
62
|
1 |
|
$data->containsKey('strategy') ? $this->deserializeStrategy($data->get('strategy')) : ToggleStrategy::AFFIRMATIVE |
|
63
|
1 |
|
); |
|
64
|
1 |
|
if ($data->containsKey('status')) { |
|
65
|
|
|
$this->deserializeStatus($toggle, $data->get('status')); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
1 |
|
return $toggle; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
private function serializeConditions(VectorInterface $conditions) |
|
72
|
|
|
{ |
|
73
|
|
|
$serialized = array(); |
|
74
|
|
|
foreach ($conditions as $condition) { |
|
75
|
|
|
if (!$condition instanceof OperatorCondition) { |
|
76
|
|
|
throw new \RuntimeException(sprintf('Unable to serialize %s.', get_class($condition))); |
|
77
|
|
|
} |
|
78
|
|
|
$serialized[] = $this->operatorConditionSerializer->serialize($condition); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
return $serialized; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
1 |
|
private function deserializeConditions(MapInterface $conditions) |
|
85
|
|
|
{ |
|
86
|
1 |
|
$deserialized = new ArrayList(); |
|
87
|
1 |
|
foreach ($conditions as $condition) { |
|
88
|
1 |
|
$deserialized->add($this->operatorConditionSerializer->deserialize($condition)); |
|
89
|
1 |
|
} |
|
90
|
|
|
|
|
91
|
1 |
|
return $deserialized; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
private function serializeStatus(FeatureInterface $feature) |
|
95
|
|
|
{ |
|
96
|
|
|
switch ($feature->getStatus()) { |
|
97
|
|
|
case ToggleStatus::ALWAYS_ACTIVE: |
|
98
|
|
|
return 'always-active'; |
|
99
|
|
|
case ToggleStatus::INACTIVE: |
|
100
|
|
|
return 'inactive'; |
|
101
|
|
|
case ToggleStatus::CONDITIONALLY_ACTIVE: |
|
102
|
|
|
return 'conditionally-active'; |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
private function deserializeStatus(FeatureInterface $feature, $status) |
|
107
|
|
|
{ |
|
108
|
|
|
switch ($feature) { |
|
109
|
|
|
case 'always-active': |
|
110
|
|
|
$feature->activate(ToggleStatus::ALWAYS_ACTIVE); |
|
111
|
|
|
|
|
112
|
|
|
return; |
|
113
|
|
|
case 'inactive': |
|
114
|
|
|
$feature->deactivate(); |
|
115
|
|
|
|
|
116
|
|
|
return; |
|
117
|
|
|
case 'conditionally-active': |
|
118
|
|
|
$feature->activate(ToggleStatus::CONDITIONALLY_ACTIVE); |
|
119
|
|
|
|
|
120
|
|
|
return; |
|
121
|
|
|
} |
|
122
|
|
|
throw new \RuntimeException(sprintf('Unknown toggle status "%s".', $status)); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* @param FeatureInterface $feature |
|
127
|
|
|
* |
|
128
|
|
|
* @return string |
|
129
|
|
|
*/ |
|
130
|
|
|
private function serializeStrategy(FeatureInterface $feature) |
|
131
|
|
|
{ |
|
132
|
|
|
switch ($feature->getStrategy()) { |
|
133
|
|
|
case ToggleStrategy::AFFIRMATIVE: |
|
134
|
|
|
return 'affirmative'; |
|
135
|
|
|
case ToggleStrategy::MAJORITY: |
|
136
|
|
|
return 'majority'; |
|
137
|
|
|
case ToggleStrategy::UNANIMOUS: |
|
138
|
|
|
return 'unanimous'; |
|
139
|
|
|
} |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* @param string $strategy |
|
144
|
|
|
* |
|
145
|
|
|
* @return int |
|
146
|
|
|
*/ |
|
147
|
|
|
private function deserializeStrategy($strategy) |
|
148
|
|
|
{ |
|
149
|
|
|
switch ($strategy) { |
|
150
|
|
|
case 'affirmative': |
|
151
|
|
|
return ToggleStrategy::AFFIRMATIVE; |
|
152
|
|
|
case 'majority': |
|
153
|
|
|
return ToggleStrategy::MAJORITY; |
|
154
|
|
|
case 'unanimous': |
|
155
|
|
|
return ToggleStrategy::UNANIMOUS; |
|
156
|
|
|
} |
|
157
|
|
|
throw new \RuntimeException(sprintf('Unknown toggle strategy "%s".', $strategy)); |
|
158
|
|
|
} |
|
159
|
|
|
} |
|
160
|
|
|
|