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.

MeasureConverter::applyReversedOperation()   B
last analyzed

Complexity

Conditions 6
Paths 6

Size

Total Lines 25
Code Lines 19

Duplication

Lines 25
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 25
loc 25
rs 8.439
cc 6
eloc 19
nc 6
nop 3
1
<?php
2
3
namespace Akeneo\Bundle\MeasureBundle\Convert;
4
5
use Akeneo\Bundle\MeasureBundle\Exception\UnknownFamilyMeasureException;
6
use Akeneo\Bundle\MeasureBundle\Exception\UnknownMeasureException;
7
use Akeneo\Bundle\MeasureBundle\Exception\UnknownOperatorException;
8
9
/**
10
 * Aims to convert measures
11
 *
12
 * @author    Romain Monceau <[email protected]>
13
 * @copyright 2012 Akeneo SAS (http://www.akeneo.com)
14
 * @license   http://opensource.org/licenses/MIT MIT
15
 */
16
class MeasureConverter
17
{
18
    /**
19
     * @var array
20
     */
21
    protected $config;
22
23
    /**
24
     * @var string
25
     */
26
    protected $family;
27
28
    /**
29
     * Constructor
30
     *
31
     * @param array $config Configuration parameters
32
     */
33
    public function __construct($config = [])
34
    {
35
        $this->config = $config['measures_config'];
36
    }
37
38
    /**
39
     * Set a family for the converter
40
     * @param string $family
41
     *
42
     * @throws UnknownFamilyMeasureException
43
     * @return MeasureConverter
44
     *
45
     */
46 View Code Duplication
    public function setFamily($family)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
47
    {
48
        if (!isset($this->config[$family])) {
49
            throw new UnknownFamilyMeasureException();
50
        }
51
52
        $this->family = $family;
53
54
        return $this;
55
    }
56
57
    /**
58
     * Convert a value from a base measure to a final measure
59
     * @param string $baseUnit  Base unit for value
60
     * @param string $finalUnit Result unit for value
61
     * @param double $value     Value to convert
62
     *
63
     * @return double
64
     */
65
    public function convert($baseUnit, $finalUnit, $value)
66
    {
67
        $standardValue = $this->convertBaseToStandard($baseUnit, $value);
68
69
        $result = $this->convertStandardToResult($finalUnit, $standardValue);
70
71
        return $result;
72
    }
73
74
    /**
75
     * Convert a value in a base unit to the standard unit
76
     * @param string $baseUnit Base unit for value
77
     * @param double $value    Value to convert
78
     *
79
     * @throws UnknownOperatorException
80
     * @throws UnknownMeasureException
81
     * @return double
82
     *
83
     */
84 View Code Duplication
    public function convertBaseToStandard($baseUnit, $value)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
85
    {
86
        if (!isset($this->config[$this->family]['units'][$baseUnit])) {
87
            throw new UnknownMeasureException(
88
                sprintf(
89
                    'Could not find metric unit "%s" in family "%s"',
90
                    $baseUnit,
91
                    $this->family
92
                )
93
            );
94
        }
95
        $conversionConfig = $this->config[$this->family]['units'][$baseUnit]['convert'];
96
        $convertedValue = $value;
97
98
        foreach ($conversionConfig as $operation) {
99
            foreach ($operation as $operator => $operand) {
100
                $convertedValue = $this->applyOperation($convertedValue, $operator, $operand);
101
            }
102
        }
103
104
        return $convertedValue;
105
    }
106
107
    /**
108
     * Apply operation between value and operand by using operator
109
     *
110
     * @param double $value    Value to convert
111
     * @param string $operator Operator to apply
112
     * @param double $operand  Operand to use
113
     *
114
     * @return double
115
     */
116 View Code Duplication
    protected function applyOperation($value, $operator, $operand)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
117
    {
118
        $processedValue = $value;
119
120
        switch ($operator) {
121
            case "div":
122
                if ($operand !== 0) {
123
                    $processedValue = $processedValue / $operand;
124
                }
125
                break;
126
            case "mul":
127
                $processedValue = $processedValue * $operand;
128
                break;
129
            case "add":
130
                $processedValue = $processedValue + $operand;
131
                break;
132
            case "sub":
133
                $processedValue = $processedValue - $operand;
134
                break;
135
            default:
136
                throw new UnknownOperatorException();
137
        }
138
139
        return $processedValue;
140
    }
141
142
    /**
143
     * Convert a value in a standard unit to a final unit
144
     * @param string $finalUnit Final unit for value
145
     * @param double $value     Value to convert
146
     *
147
     * @throws UnknownOperatorException
148
     * @throws UnknownMeasureException
149
     * @return double
150
     *
151
     */
152 View Code Duplication
    public function convertStandardToResult($finalUnit, $value)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
153
    {
154
        if (!isset($this->config[$this->family]['units'][$finalUnit])) {
155
            throw new UnknownMeasureException(
156
                sprintf(
157
                    'Could not find metric unit "%s" in family "%s"',
158
                    $finalUnit,
159
                    $this->family
160
                )
161
            );
162
        }
163
        $conversionConfig = $this->config[$this->family]['units'][$finalUnit]['convert'];
164
        $convertedValue = $value;
165
166
        // calculate result with conversion config (calculs must be reversed and operation inversed)
167
        foreach (array_reverse($conversionConfig) as $operation) {
168
            foreach ($operation as $operator => $operand) {
169
                $convertedValue = $this->applyReversedOperation($convertedValue, $operator, $operand);
170
            }
171
        }
172
173
        return $convertedValue;
174
    }
175
176
    /**
177
     * Apply reversed operation between value and operand by using operator
178
     *
179
     * @param double $value    Value to convert
180
     * @param string $operator Operator to apply
181
     * @param double $operand  Operand to use
182
     *
183
     * @return double
184
     */
185 View Code Duplication
    protected function applyReversedOperation($value, $operator, $operand)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
186
    {
187
        $processedValue = $value;
188
189
        switch ($operator) {
190
            case "div":
191
                $processedValue = $processedValue * $operand;
192
                break;
193
            case "mul":
194
                if ($operand !== 0) {
195
                    $processedValue = $processedValue / $operand;
196
                }
197
                break;
198
            case "add":
199
                $processedValue = $processedValue - $operand;
200
                break;
201
            case "sub":
202
                $processedValue = $processedValue + $operand;
203
                break;
204
            default:
205
                throw new UnknownOperatorException();
206
        }
207
208
        return $processedValue;
209
    }
210
}
211