ActionType::getXML()   B
last analyzed

Complexity

Conditions 10
Paths 7

Size

Total Lines 32
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 10

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.6666
cc 10
nc 7
nop 0
crap 10

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
/**
19
 * @property array{
20
 *     type: string,
21
 *     filter?: iterable<string, mixed>,
22
 *     agenda?: string,
23
 * } $data
24
 */
25
class ActionType extends AbstractAgenda
26
{
27
    use SetNamespaceTrait;
28
29
    /**
30
     * {@inheritdoc}
31
     */
32 8
    public function getXML(): \SimpleXMLElement
33
    {
34 8
        if (is_null($this->namespace)) {
35 1
            throw new \LogicException('Namespace not set.');
36
        }
37
38 7
        $xml = $this->createXML()->addChild($this->namespace . ':actionType', '', $this->namespace($this->namespace));
39 7
        $action = $xml->addChild($this->namespace . ':' . ('add/update' == $this->data['type'] ? 'add' : $this->data['type']));
40
41 7
        if ('add/update' == $this->data['type']) {
42 1
            $action->addAttribute('update', 'true');
43
        }
44
45 7
        if (isset($this->data['filter'])) {
46 6
            $filter = $action->addChild('ftr:filter', '', $this->namespace('ftr'));
47
48 6
            if (!empty($this->data['agenda'])) {
49 1
                $filter->addAttribute('agenda', strval($this->data['agenda']));
50
            }
51
52 6
            foreach ($this->data['filter'] as $property => $value) {
53 6
                $ftr = $filter->addChild('ftr:' . $property, \is_array($value) ? null : strval($value));
54
55 6
                if (\is_array($value)) {
56 2
                    foreach ($value as $tProperty => $tValue) {
57 2
                        $ftr->addChild('typ:' . $tProperty, $tValue, $this->namespace('typ'));
58
                    }
59
                }
60
            }
61
        }
62
63 7
        return $xml;
64
    }
65
66
    /**
67
     * {@inheritdoc}
68
     */
69 1
    protected function configureOptions(OptionsResolver $resolver): void
70
    {
71
        // available options
72 1
        $resolver->setDefined(['type', 'filter', 'agenda']);
73
74
        // validate / format options
75 1
        $resolver->setRequired('type');
76 1
        $resolver->setAllowedValues('type', ['add', 'add/update', 'update', 'delete']);
77
    }
78
}
79