GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

ToggleSerializer   A
last analyzed

Complexity

Total Complexity 27

Size/Duplication

Total Lines 139
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 25.33%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 27
c 1
b 0
f 1
lcom 1
cbo 5
dl 0
loc 139
ccs 19
cts 75
cp 0.2533
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A serialize() 0 9 1
A deserialize() 0 20 4
A serializeConditions() 0 12 3
A deserializeConditions() 0 9 2
A serializeStatus() 0 11 4
A deserializeStatus() 0 18 4
A serializeStrategy() 0 11 4
A deserializeStrategy() 0 12 4
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