VectorGraphicComponent::jsonSerialize()   B
last analyzed

Complexity

Conditions 10
Paths 64

Size

Total Lines 29
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 10

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 10
eloc 14
nc 64
nop 0
dl 0
loc 29
ccs 15
cts 15
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\Document\APLComponentType;
8
use MaxBeckers\AmazonAlexa\Response\Directives\APL\Document\ImageAlign;
9
use MaxBeckers\AmazonAlexa\Response\Directives\APL\Document\Scale;
10
use MaxBeckers\AmazonAlexa\Response\Directives\APL\StandardCommand\AbstractStandardCommand;
11
12
class VectorGraphicComponent extends TouchableComponent implements \JsonSerializable
13
{
14
    public const TYPE = APLComponentType::VECTOR_GRAPHIC;
15
16
    /**
17
     * @param ImageAlign|null $align Alignment of the vector graphic within the component
18
     * @param AbstractStandardCommand[]|null $onFail Commands to run when the source fails to load
19
     * @param AbstractStandardCommand[]|null $onLoad Commands to run after the source loads
20
     * @param array|null $parameters Optional map of parameters to pass to the vector graphic
21
     * @param Scale|null $scale How the vector graphic scales up or down within the component
22
     * @param string|null $source The URL or direct reference to a vector graphic
23
     */
24 10
    public function __construct(
25
        public ?ImageAlign $align = null,
26
        public ?array $onFail = null,
27
        public ?array $onLoad = null,
28
        public ?array $parameters = null,
29
        public ?Scale $scale = null,
30
        public ?string $source = null,
31
    ) {
32 10
        parent::__construct(self::TYPE);
33
    }
34
35 6
    public function jsonSerialize(): array
36
    {
37 6
        $data = parent::jsonSerialize();
38
39 6
        if ($this->align !== null) {
40 3
            $data['align'] = $this->align->value;
41
        }
42
43 6
        if ($this->onFail !== null && !empty($this->onFail)) {
44 1
            $data['onFail'] = $this->onFail;
45
        }
46
47 6
        if ($this->onLoad !== null && !empty($this->onLoad)) {
48 1
            $data['onLoad'] = $this->onLoad;
49
        }
50
51 6
        if ($this->parameters !== null && !empty($this->parameters)) {
52 1
            $data['parameters'] = $this->parameters;
53
        }
54
55 6
        if ($this->scale !== null) {
56 2
            $data['scale'] = $this->scale->value;
57
        }
58
59 6
        if ($this->source !== null) {
60 2
            $data['source'] = $this->source;
61
        }
62
63 6
        return $data;
64
    }
65
}
66