Completed
Pull Request — master (#7)
by Ondrej
01:33
created

BitMaskConverter::fetchEnumClass()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 7
cts 7
cp 1
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 5
nc 4
nop 1
crap 5
1
<?php
2
namespace SpareParts\Enum\Set\Converter;
3
4
use SpareParts\Enum\Enum;
5
use SpareParts\Enum\Exception\EnumSetMustContainEnumsException;
6
use SpareParts\Enum\Exception\InvalidEnumClassException;
7
use SpareParts\Enum\Set\ISet;
8
use SpareParts\Enum\Set\ImmutableSet;
9
10
class BitMaskConverter implements IEnumSetConverter
11
{
12
    /**
13
     * @var string
14
     */
15
    private $enumClass;
16
17
    /**
18
     * @var array
19
     */
20
    private $mapping;
21
22
    /**
23
     * @param Enum[] $enumValuesMap
24
     */
25 14
    public function __construct($enumValuesMap)
26
    {
27 14
        if (!is_array($enumValuesMap) || !count($enumValuesMap)) {
28 2
            throw new EnumSetMustContainEnumsException('You have to provide converter mapping.');
29
        }
30 12
        $this->mapping = $this->prepareInnerMapping($enumValuesMap);
31 9
    }
32
33
    /**
34
     * @param int $values
35
     * @return ISet
36
     */
37 5 View Code Duplication
    public function convertToEnumSet($values)
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...
38
    {
39 5
        $bitValue = (int) $values;
40
41 5
        $set = [];
42 5
        foreach ($this->mapping as $value => $bit) {
43 5
            if ($bitValue & $bit) {
44 4
                $set[] = call_user_func([$this->enumClass, 'instance'], $value);
45 4
            }
46 5
        }
47 5
        return new ImmutableSet($this->enumClass, $set);
48
    }
49
50
    /**
51
     * @param ISet $enumSet
52
     * @return int
53
     */
54 4
    public function convertFromEnumSet(ISet $enumSet)
55
    {
56 4
        $bitValue = 0;
57
        /** @var Enum $value */
58 4
        foreach ($enumSet as $value) {
59 3
            $bitValue = $bitValue | $this->mapping[(string) $value];
60 4
        }
61 4
        return $bitValue;
62
    }
63
64
    /**
65
     * @param $enumValuesMap
66
     * @return array
67
     */
68 12
    private function prepareInnerMapping($enumValuesMap)
69
    {
70
        // translate enum values to bit values mapping
71 12
        $innerMapping = [];
72 12
        $shift = 0;
73 12
        foreach ($enumValuesMap as $value) {
74 12
            $this->fetchEnumClass($value);
75
76 11
            $innerMapping[(string)$value] = 1 << $shift;
77 11
            $shift++;
78 11
        }
79 9
        return $innerMapping;
80
    }
81
82
    /**
83
     * @param $value
84
     */
85 12
    private function fetchEnumClass($value)
86
    {
87
        // try to guess enum class
88 12
        if (is_null($this->enumClass)) {
89 12
            $this->setEnumClass($value);
90 11
        }
91 11
        if (!is_object($value) || !($value instanceof $this->enumClass)) {
92 2
            throw new EnumSetMustContainEnumsException(sprintf("Expected `%s`, got `%s`", $this->enumClass, is_object($value) ? get_class($value) : var_export($value, true)));
93
        }
94 11
    }
95
96
    /**
97
     * @param mixed $enum
98
     */
99 12
    private function setEnumClass($enum)
100
    {
101 12
        if (!is_object($enum) || !is_subclass_of($enum, Enum::class)) {
0 ignored issues
show
Bug introduced by
Due to PHP Bug #53727, is_subclass_of might return inconsistent results on some PHP versions if \SpareParts\Enum\Enum::class can be an interface. If so, you could instead use ReflectionClass::implementsInterface.
Loading history...
102 1
            throw new EnumSetMustContainEnumsException(sprintf("Class %s does not implement Enum (as it should).", is_object($enum) ? get_class($enum) : var_export($enum, true)));
103
        }
104 11
        $this->enumClass = get_class($enum);
105 11
    }
106
}
107