Completed
Push — master ( 7dc19a...8d6915 )
by Markus
04:32 queued 02:59
created

Component::setComponents()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
/*
4
 * This file is part of the eluceo/iCal package.
5
 *
6
 * (c) Markus Poerschke <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace Eluceo\iCal;
13
14
use Eluceo\iCal\Util\ComponentUtil;
15
16
/**
17
 * Abstract Calender Component.
18
 */
19
abstract class Component
20
{
21
    /**
22
     * Array of Components.
23
     *
24
     * @var Component[]
25
     */
26
    protected $components = [];
27
28
    /**
29
     * The order in which the components will be rendered during build.
30
     *
31
     * Not defined components will be appended at the end.
32
     *
33
     * @var array
34
     */
35
    private $componentsBuildOrder = ['VTIMEZONE', 'DAYLIGHT', 'STANDARD'];
36
37
    /**
38
     * The type of the concrete Component.
39
     *
40
     * @abstract
41
     *
42
     * @return string
43
     */
44
    abstract public function getType();
45
46
    /**
47
     * Building the PropertyBag.
48
     *
49
     * @abstract
50
     *
51
     * @return PropertyBag
52
     */
53
    abstract public function buildPropertyBag();
54
55
    /**
56
     * Adds a Component.
57
     *
58
     * If $key is given, the component at $key will be replaced else the component will be append.
59
     *
60
     * @param Component $component The Component that will be added
61
     * @param null      $key       The key of the Component
62
     */
63 9
    public function addComponent(self $component, $key = null)
64
    {
65 9
        if (null == $key) {
66 8
            $this->components[] = $component;
67
        } else {
68 1
            $this->components[$key] = $component;
69
        }
70 9
    }
71
72
    /**
73
     * Set all Components.
74
     *
75
     * @param Component[] $components The array of Component that will be set
76
     * @param null        $key        The key of the Component
0 ignored issues
show
Bug introduced by
There is no parameter named $key. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
77
     */
78 1
    public function setComponents(array $components)
79
    {
80 1
        $this->components = $components;
81
82 1
        return $this;
83
    }
84
85
    /**
86
     * Renders an array containing the lines of the iCal file.
87
     *
88
     * @return array
89
     */
90 18
    public function build()
91
    {
92 18
        $lines = [];
93
94 18
        $lines[] = sprintf('BEGIN:%s', $this->getType());
95
96
        /** @var $property Property */
97 18
        foreach ($this->buildPropertyBag() as $property) {
98 18
            foreach ($property->toLines() as $l) {
99 18
                $lines[] = $l;
100
            }
101
        }
102
103 18
        $this->buildComponents($lines);
104
105 18
        $lines[] = sprintf('END:%s', $this->getType());
106
107 18
        $ret = [];
108
109 18
        foreach ($lines as $line) {
110 18
            foreach (ComponentUtil::fold($line) as $l) {
111 18
                $ret[] = $l;
112
            }
113
        }
114
115 18
        return $ret;
116
    }
117
118
    /**
119
     * Renders the output.
120
     *
121
     * @return string
122
     */
123 18
    public function render()
124
    {
125 18
        return implode("\r\n", $this->build());
126
    }
127
128
    /**
129
     * Renders the output when treating the class as a string.
130
     *
131
     * @return string
132
     */
133 1
    public function __toString()
134
    {
135 1
        return $this->render();
136
    }
137
138
    /**
139
     * @param $lines
140
     *
141
     * @return array
142
     */
143 18
    private function buildComponents(array &$lines)
144
    {
145 18
        $componentsByType = [];
146
147
        /** @var $component Component */
148 18
        foreach ($this->components as $component) {
149 9
            $type = $component->getType();
150 9
            if (!isset($componentsByType[$type])) {
151 9
                $componentsByType[$type] = [];
152
            }
153 9
            $componentsByType[$type][] = $component;
154
        }
155
156
        // render ordered components
157 18
        foreach ($this->componentsBuildOrder as $type) {
158 18
            if (!isset($componentsByType[$type])) {
159 18
                continue;
160
            }
161 2
            foreach ($componentsByType[$type] as $component) {
162 2
                $this->addComponentLines($lines, $component);
163
            }
164 2
            unset($componentsByType[$type]);
165
        }
166
167
        // render all other
168 18
        foreach ($componentsByType as $components) {
169 7
            foreach ($components as $component) {
170 7
                $this->addComponentLines($lines, $component);
171
            }
172
        }
173 18
    }
174
175
    /**
176
     * @param array     $lines
177
     * @param Component $component
178
     */
179 9
    private function addComponentLines(array &$lines, self $component)
180
    {
181 9
        foreach ($component->build() as $l) {
182 9
            $lines[] = $l;
183
        }
184 9
    }
185
}
186