ActionType::getXML()   B
last analyzed

Complexity

Conditions 11
Paths 7

Size

Total Lines 32
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 11

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 16
c 1
b 0
f 0
dl 0
loc 32
ccs 17
cts 17
cp 1
rs 7.3166
cc 11
nc 7
nop 0
crap 11

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
 * This file is part of riesenia/pohoda package.
5
 *
6
 * Licensed under the MIT License
7
 * (c) RIESENIA.com
8
 */
9
10
declare(strict_types=1);
11
12
namespace Riesenia\Pohoda\Type;
13
14
use Riesenia\Pohoda\AbstractAgenda;
15
use Riesenia\Pohoda\Common\OptionsResolver;
16
use Riesenia\Pohoda\Common\SetNamespaceTrait;
17
18
class ActionType extends AbstractAgenda
19
{
20
    use SetNamespaceTrait;
21
22
    /**
23
     * {@inheritdoc}
24
     */
25 8
    public function getXML(): \SimpleXMLElement
26
    {
27 8
        if (is_null($this->namespace)) {
28 1
            throw new \LogicException('Namespace not set.');
29
        }
30
31 7
        $xml = $this->createXML()->addChild($this->namespace . ':actionType', '', $this->namespace($this->namespace));
32 7
        $action = $xml->addChild($this->namespace . ':' . ('add/update' == $this->data['type'] ? 'add' : $this->data['type']));
33
34 7
        if ('add/update' == $this->data['type']) {
35 1
            $action->addAttribute('update', 'true');
36
        }
37
38 7
        if (isset($this->data['filter']) && is_iterable($this->data['filter'])) {
39 6
            $filter = $action->addChild('ftr:filter', '', $this->namespace('ftr'));
40
41 6
            if ($this->data['agenda']) {
42 1
                $filter->addAttribute('agenda', strval($this->data['agenda']));
43
            }
44
45 6
            foreach ($this->data['filter'] as $property => $value) {
46 6
                $ftr = $filter->addChild('ftr:' . $property, \is_array($value) ? null : strval($value));
47
48 6
                if (\is_array($value)) {
49 2
                    foreach ($value as $tProperty => $tValue) {
50 2
                        $ftr->addChild('typ:' . $tProperty, $tValue, $this->namespace('typ'));
51
                    }
52
                }
53
            }
54
        }
55
56 7
        return $xml;
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62 1
    protected function configureOptions(OptionsResolver $resolver): void
63
    {
64
        // available options
65 1
        $resolver->setDefined(['type', 'filter', 'agenda']);
66
67
        // validate / format options
68 1
        $resolver->setRequired('type');
69 1
        $resolver->setAllowedValues('type', ['add', 'add/update', 'update', 'delete']);
70
    }
71
}
72