Passed
Push — master ( 26e0e4...4a85b9 )
by Peter
10:37
created

Section::__construct()   C

Complexity

Conditions 12
Paths 64

Size

Total Lines 28
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 12
eloc 18
nc 64
nop 5
dl 0
loc 28
rs 6.9666
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
4
/**
5
 * The MIT License
6
 *
7
 * Copyright 2018  Peter Gee <https://github.com/pgee70>.
8
 *
9
 * Permission is hereby granted, free of charge, to any person obtaining a copy
10
 * of this software and associated documentation files (the "Software"), to deal
11
 * in the Software without restriction, including without limitation the rights
12
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13
 * copies of the Software, and to permit persons to whom the Software is
14
 * furnished to do so, subject to the following conditions:
15
 *
16
 * The above copyright notice and this permission notice shall be included in
17
 * all copies or substantial portions of the Software.
18
 *
19
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
 * FITNESS FOR A PARTICULAR PURPOSE AND NON INFRINGEMENT. IN NO EVENT SHALL THE
22
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25
 * THE SOFTWARE.
26
 */
27
28
namespace i3Soft\CDA\Component\SingleComponent;
29
30
/**
31
 *
32
 * @package     i3Soft\CDA
33
 * @author      Peter Gee <https://github.com/pgee70>
34
 * @link        https://github.com/pgee70/cda
35
 *
36
 */
37
38
use i3Soft\CDA\Elements\AbstractElement;
39
use i3Soft\CDA\Elements\Code;
40
use i3Soft\CDA\Elements\Entry;
41
use i3Soft\CDA\Elements\Html\Text;
42
use i3Soft\CDA\Elements\Html\Title;
43
use i3Soft\CDA\Elements\Id;
44
use i3Soft\CDA\Interfaces\ClassCodeInterface;
45
use i3Soft\CDA\Interfaces\MoodCodeInterface;
46
use i3Soft\CDA\Traits\AuthorTrait;
47
use i3Soft\CDA\Traits\ClassCodeTrait;
48
use i3Soft\CDA\Traits\CodeTrait;
49
use i3Soft\CDA\Traits\ConfidentialityCodeTrait;
50
use i3Soft\CDA\Traits\EntriesTrait;
51
use i3Soft\CDA\Traits\ExtCoverage2Trait;
52
use i3Soft\CDA\Traits\IdTrait;
53
use i3Soft\CDA\Traits\InformantsTrait;
54
use i3Soft\CDA\Traits\LanguageCodeTrait;
55
use i3Soft\CDA\Traits\MoodCodeTrait;
56
use i3Soft\CDA\Traits\SingleComponentTrait;
57
use i3Soft\CDA\Traits\SubjectTrait;
58
use i3Soft\CDA\Traits\TextTrait;
59
use i3Soft\CDA\Traits\TitleTrait;
60
61
/**
62
 * Class Section
63
 *
64
 * @package i3Soft\CDA\Component\SingleComponent
65
 */
66
class Section extends AbstractElement implements ClassCodeInterface, MoodCodeInterface
67
{
68
    use AuthorTrait;
69
    use CodeTrait;
70
    use ConfidentialityCodeTrait;
71
    use EntriesTrait;
72
    use IdTrait;
73
    use LanguageCodeTrait;
74
    use SingleComponentTrait;
75
    use SubjectTrait;
76
    use TextTrait;
77
    use TitleTrait;
78
    use InformantsTrait;
79
    use ExtCoverage2Trait;
80
    use ClassCodeTrait;
81
    use MoodCodeTrait;
82
83
    /** @noinspection ArrayTypeOfParameterByDefaultValueInspection */
84
85
    /**
86
     * Section constructor.
87
     *
88
     * @param Id    $id
89
     * @param Code  $code
90
     * @param Title $title
91
     * @param Text  $text
92
     * @param       $entry
93
     */
94
    public function __construct($id = null, $code = null, $title = null, $text = null, $entry = [])
95
    {
96
        $this->setAcceptableClassCodes(ClassCodeInterface::ActClass)
97
          ->setAcceptableMoodCodes(MoodCodeInterface::ActMood)
98
          ->setClassCode(ClassCodeInterface::DOCUMENT_SECTION)
99
          ->setMoodCode(MoodCodeInterface::EVENT);
100
101
        $this->entries = [];
102
103
        if ($id && $id instanceof Id) {
104
            $this->setId($id);
105
        }
106
        if ($code && $code instanceof Code) {
107
            $this->setCode($code);
108
        }
109
110
        if ($title && $title instanceof Title) {
111
            $this->setTitle($title);
112
        }
113
114
        if ($text && $text instanceof Text) {
115
            $this->setText($text);
116
        }
117
        if ($entry) {
118
            if (\is_array($entry)) {
119
                $this->setEntries($entry);
120
            } elseif ($entry instanceof Entry) {
121
                $this->addEntry($entry);
122
            }
123
        }
124
    }
125
126
127
    /**
128
     * @return self
129
     */
130
    public function clearEntries(): self
131
    {
132
        $this->entries = array();
133
        return $this;
134
    }
135
136
    /**
137
     * create an entry, which is already bound to the current section
138
     *
139
     * @return Entry
140
     */
141
    public function createEntry(): Entry
142
    {
143
        $entry = new Entry();
144
        $this->addEntry($entry);
145
        return $entry;
146
    }
147
148
149
    /**
150
     * @param \DOMDocument $doc
151
     *
152
     * @return \DOMElement
153
     */
154
    public function toDOMElement(\DOMDocument $doc): \DOMElement
155
    {
156
        $el = $this->createElement($doc);
157
        $this->renderId($el, $doc)
158
          ->renderCode($el, $doc)
159
          ->renderTitle($el, $doc)
160
          ->renderText($el, $doc)
161
          ->renderConfidentialityCode($el, $doc)
162
          ->renderLanguageCode($el, $doc)
163
          ->renderSubject($el, $doc)
164
          ->renderAuthor($el, $doc)
165
          ->renderInformants($el, $doc)
166
          ->renderEntries($el, $doc)
167
          ->renderComponents($el, $doc)
168
          ->renderExtCoverage2($el, $doc);
169
        return $el;
170
    }
171
172
173
    /**
174
     * @return string
175
     */
176
    protected function getElementTag(): string
177
    {
178
        return 'section';
179
    }
180
181
}