Completed
Pull Request — master (#7)
by Stefan
03:14
created

Styler   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 104
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 4
Bugs 1 Features 1
Metric Value
wmc 11
c 4
b 1
f 1
lcom 1
cbo 4
dl 0
loc 104
ccs 41
cts 41
cp 1
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A addStyle() 0 9 2
A register() 0 13 2
A getStyleById() 0 4 2
A getStyleSheetXml() 0 21 4
1
<?php
2
3
namespace OneSheet\Style;
4
5
use OneSheet\Xml\StyleXml;
6
7
/**
8
 * Class Styler to keep track of registered styles.
9
 *
10
 * @package OneSheet
11
 */
12
class Styler
13
{
14
    /**
15
     * @var Style[]
16
     */
17
    private $styles = array();
18
19
    /**
20
     * @var Font[]
21
     */
22
    private $fonts = array();
23
24
    /**
25
     * @var Fill[]
26
     */
27
    private $fills = array();
28
29
    /**
30
     * @var array
31
     */
32
    private $hashes = array();
33
34
    /**
35
     * Styler constructor.
36
     */
37 4
    public function __construct()
38
    {
39 4
        $this->addStyle(new Style());
40 4
        $grey = new Style();
41 4
        $this->addStyle($grey->setFillPattern('grey125'));
42 4
    }
43
44
    /**
45
     * Add a new style, if it doesnt exists yet.
46
     *
47
     * @param Style $style
48
     */
49 4
    public function addStyle(Style $style)
50
    {
51 4
        if (null === $style->getId()) {
52 4
            $this->register($style->getFont(), $this->fonts);
53 4
            $this->register($style->getFill(), $this->fills);
54 4
            $this->register($style, $this->styles);
55 4
            $style->lock();
56 4
        }
57 4
    }
58
59
    /**
60
     * Register a new Style compoment its compoments to account
61
     * for reusable styles, fills, fonts, ...
62
     *
63
     * @param Component $component
64
     * @param array     $collection
65
     */
66 4
    private function register(Component $component, &$collection)
67
    {
68 4
        $hash = md5($component->asXml());
69 4
        $newId = count($collection);
70
71 4
        if (isset($this->hashes[$hash])) {
72 4
            $component->setId($this->hashes[$hash]);
73 4
        } else {
74 4
            $component->setId($newId);
75 4
            $collection[$newId] = $component;
76 4
            $this->hashes[$hash] = $newId;
77
        }
78 4
    }
79
80
    /**
81
     * @param int $id
82
     * @return Style
83
     */
84 1
    public function getStyleById($id)
85
    {
86 1
        return isset($this->styles[$id]) ? $this->styles[$id] : new Style();
87
    }
88
89
    /**
90
     * Return entire xml string for the style sheet.
91
     *
92
     * @return string
93
     */
94 2
    public function getStyleSheetXml()
95
    {
96 2
        $fontsXml = $fillsXml = $cellXfsXml = '';
97 2
        foreach ($this->styles as $style) {
98 2
            $cellXfsXml .= $style->asXml();
99 2
        }
100
101 2
        foreach ($this->fonts as $font) {
102 2
            $fontsXml .= $font->asXml();
103 2
        }
104
105 2
        foreach ($this->fills as $fill) {
106 2
            $fillsXml .= $fill->asXml();
107 2
        }
108
109 2
        return sprintf(StyleXml::STYLE_SHEET_XML,
110 2
            sprintf(StyleXml::FONTS_XML, count($this->fonts), $fontsXml),
111 2
            sprintf(StyleXml::FILLS_XML, count($this->fills), $fillsXml),
112 2
            sprintf(StyleXml::CELL_XFS_XML, count($this->styles), $cellXfsXml)
113 2
        );
114
    }
115
}
116