Completed
Pull Request — master (#7)
by Ondrej
05:55
created

StringSerializeConverter::convertFromEnumSet()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
crap 2
1
<?php
2
3
namespace SpareParts\Enum\Set\Converter;
4
5
6
use SpareParts\Enum\Enum;
7
use SpareParts\Enum\Exception\InvalidEnumClassException;
8
use SpareParts\Enum\Set\ISet;
9
use SpareParts\Enum\Set\ImmutableSet;
10
11
class StringSerializeConverter implements IEnumSetConverter
12
{
13
    /**
14
     * @var string
15
     */
16
    private $enumClass;
17
18
    /**
19
     * @param string $enumClass
20
     */
21 9 View Code Duplication
    public function __construct($enumClass)
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...
22
    {
23 9
        if (!is_subclass_of($enumClass, 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...
24 1
            throw new InvalidEnumClassException("Class ${enumClass} does not implement Enum (as it should).");
25
        }
26
        $this->enumClass = $enumClass;
27
    }
28
29
    /**
30
     * @param string $serialized
31
     * @return ImmutableSet
32
     */
33 View Code Duplication
    public function convertToEnumSet($serialized)
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...
34
    {
35 4
        $serialized = (string) $serialized;
36 4
        $stringList = unserialize($serialized);
37
38 4
        $set = [];
39 4
        foreach ($stringList as $value) {
40 3
            $set[] = call_user_func([$this->enumClass, 'instance'], $value);
41 4
        }
42 4
        return new ImmutableSet($this->enumClass, $set);
43
    }
44
45
    /**
46
     * @param ISet $enumSet
47
     * @return string
48
     */
49
    public function convertFromEnumSet(ISet $enumSet)
50
    {
51 4
        $stringList = [];
52 4
        foreach ($enumSet as $enum) {
53 3
            $stringList[] = (string) $enum;
54 4
        }
55 4
        return serialize($stringList);
56
    }
57
}
58