ActionType   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 11
eloc 21
c 1
b 0
f 0
dl 0
loc 52
ccs 21
cts 21
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
B getXML() 0 32 10
A configureOptions() 0 8 1
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