Completed
Push — master ( 91f756...eb84ec )
by Adrien
13:07
created

hasSerializedStyleAlreadyBeenRegistered()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
c 0
b 0
f 0
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Box\Spout\Writer\Common\Manager\Style;
4
5
use Box\Spout\Common\Entity\Style\Style;
6
7
/**
8
 * Class StyleRegistry
9
 * Registry for all used styles
10
 */
11
class StyleRegistry
12
{
13
    /** @var array [SERIALIZED_STYLE] => [STYLE_ID] mapping table, keeping track of the registered styles */
14
    protected $serializedStyleToStyleIdMappingTable = [];
15
16
    /** @var array [STYLE_ID] => [STYLE] mapping table, keeping track of the registered styles */
17
    protected $styleIdToStyleMappingTable = [];
18
19
    /**
20
     * @param Style $defaultStyle
21
     */
22 91
    public function __construct(Style $defaultStyle)
23
    {
24
        // This ensures that the default style is the first one to be registered
25 91
        $this->registerStyle($defaultStyle);
26 91
    }
27
28
    /**
29
     * Registers the given style as a used style.
30
     * Duplicate styles won't be registered more than once.
31
     *
32
     * @param Style $style The style to be registered
33
     * @return Style The registered style, updated with an internal ID.
34
     */
35 91
    public function registerStyle(Style $style)
36
    {
37 91
        $serializedStyle = $this->serialize($style);
38
39 91
        if (!$this->hasSerializedStyleAlreadyBeenRegistered($serializedStyle)) {
40 91
            $nextStyleId = \count($this->serializedStyleToStyleIdMappingTable);
41 91
            $style->markAsRegistered($nextStyleId);
42
43 91
            $this->serializedStyleToStyleIdMappingTable[$serializedStyle] = $nextStyleId;
44 91
            $this->styleIdToStyleMappingTable[$nextStyleId] = $style;
45
        }
46
47 91
        return $this->getStyleFromSerializedStyle($serializedStyle);
48
    }
49
50
    /**
51
     * Returns whether the serialized style has already been registered.
52
     *
53
     * @param string $serializedStyle The serialized style
54
     * @return bool
55
     */
56 91
    protected function hasSerializedStyleAlreadyBeenRegistered(string $serializedStyle)
57
    {
58 91
        // Using isset here because it is way faster than array_key_exists...
59
        return isset($this->serializedStyleToStyleIdMappingTable[$serializedStyle]);
60
    }
61 91
62
    /**
63
     * Returns the registered style associated to the given serialization.
64
     *
65
     * @param string $serializedStyle The serialized style from which the actual style should be fetched from
66
     * @return Style
67
     */
68
    protected function getStyleFromSerializedStyle($serializedStyle)
69
    {
70 91
        $styleId = $this->serializedStyleToStyleIdMappingTable[$serializedStyle];
71
72 91
        return $this->styleIdToStyleMappingTable[$styleId];
73
    }
74 91
75
    /**
76
     * @return Style[] List of registered styles
77
     */
78
    public function getRegisteredStyles()
79
    {
80 73
        return \array_values($this->styleIdToStyleMappingTable);
81
    }
82 73
83
    /**
84
     * @param int $styleId
85
     * @return Style
86
     */
87
    public function getStyleFromStyleId($styleId)
88
    {
89 7
        return $this->styleIdToStyleMappingTable[$styleId];
90
    }
91 7
92
    /**
93
     * Serializes the style for future comparison with other styles.
94
     * The ID is excluded from the comparison, as we only care about
95
     * actual style properties.
96
     *
97
     * @param Style $style
98
     * @return string The serialized style
99
     */
100
    public function serialize(Style $style)
101
    {
102 91
        // In order to be able to properly compare style, set static ID value and reset registration
103
        $currentId = $style->getId();
104
        $style->unmarkAsRegistered();
105 91
106 91
        $serializedStyle = \serialize($style);
107
108 91
        $style->markAsRegistered($currentId);
109
110 91
        return $serializedStyle;
111
    }
112
}
113