PagerComponent::jsonSerialize()   B
last analyzed

Complexity

Conditions 10
Paths 64

Size

Total Lines 33
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 10

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 10
eloc 17
nc 64
nop 0
dl 0
loc 33
ccs 19
cts 19
cp 1
crap 10
rs 7.6666
c 1
b 0
f 1

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
declare(strict_types=1);
4
5
namespace MaxBeckers\AmazonAlexa\Response\Directives\APL\Component;
6
7
use MaxBeckers\AmazonAlexa\Response\Directives\APL\Component\Traits\ActionableComponentTrait;
8
use MaxBeckers\AmazonAlexa\Response\Directives\APL\Component\Traits\MultiChildComponentTrait;
9
use MaxBeckers\AmazonAlexa\Response\Directives\APL\Document\APLComponentType;
10
use MaxBeckers\AmazonAlexa\Response\Directives\APL\Document\Navigation;
11
use MaxBeckers\AmazonAlexa\Response\Directives\APL\Document\PageDirection;
12
use MaxBeckers\AmazonAlexa\Response\Directives\APL\StandardCommand\AbstractStandardCommand;
13
14
class PagerComponent extends APLBaseComponent implements \JsonSerializable
15
{
16
    use ActionableComponentTrait;
17
    use MultiChildComponentTrait;
18
19
    public const TYPE = APLComponentType::PAGER;
20
21
    /**
22
     * @param array|null $handlePageMove Handlers to run when the Pager changes pages
23
     * @param int $initialPage The index of the starting page (0-based)
24
     * @param Navigation|null $navigation Specifies the allowed navigation direction
25
     * @param AbstractStandardCommand[]|null $onPageChanged Commands to run when the page changes
26
     * @param PageDirection|null $pageDirection The direction to move pages
27
     * @param string[]|null $preserve Properties to save when reinflating the document
28
     */
29 13
    public function __construct(
30
        public ?array $handlePageMove = null,
31
        public int $initialPage = 0,
32
        public ?Navigation $navigation = null,
33
        public ?array $onPageChanged = null,
34
        public ?PageDirection $pageDirection = null,
35
        ?array $preserve = null,
36
    ) {
37 13
        parent::__construct(type: self::TYPE, preserve: $preserve);
38
    }
39
40 7
    public function jsonSerialize(): array
41
    {
42 7
        $data = array_merge(
43 7
            parent::jsonSerialize(),
44 7
            $this->serializeActionableProperties(),
45 7
            $this->serializeMultiChildProperties()
46 7
        );
47
48 7
        if ($this->handlePageMove !== null && !empty($this->handlePageMove)) {
49 1
            $data['handlePageMove'] = $this->handlePageMove;
50
        }
51
52 7
        if ($this->initialPage !== 0) {
53 2
            $data['initialPage'] = $this->initialPage;
54
        }
55
56 7
        if ($this->navigation !== null) {
57 2
            $data['navigation'] = $this->navigation->value;
58
        }
59
60 7
        if ($this->onPageChanged !== null && !empty($this->onPageChanged)) {
61 1
            $data['onPageChanged'] = $this->onPageChanged;
62
        }
63
64 7
        if ($this->pageDirection !== null) {
65 2
            $data['pageDirection'] = $this->pageDirection->value;
66
        }
67
68 7
        if ($this->preserve !== null && !empty($this->preserve)) {
69 1
            $data['preserve'] = $this->preserve;
70
        }
71
72 7
        return $data;
73
    }
74
}
75