Passed
Pull Request — master (#97)
by Maximilian
04:02
created

ImageComponent::jsonSerialize()   F

Complexity

Conditions 18
Paths 2048

Size

Total Lines 49
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 24
CRAP Score 18.0207

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 18
eloc 24
nc 2048
nop 0
dl 0
loc 49
ccs 24
cts 25
cp 0.96
crap 18.0207
rs 0.7
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 ImageComponent extends APLBaseComponent implements \JsonSerializable
13
{
14
    public const TYPE = APLComponentType::IMAGE;
15
16
    /**
17
     * @param ImageAlign|null $align Alignment of the image within the containing box
18
     * @param string $borderRadius Clipping radius for the image
19
     * @param array|null $filter Single filter to apply to the image
20
     * @param array|null $filters Array of filters to apply to the image
21
     * @param AbstractStandardCommand[]|null $onFail Commands to run when any source fails to load
22
     * @param AbstractStandardCommand[]|null $onLoad Commands to run after all sources loaded successfully
23
     * @param string $overlayColor Theme-appropriate scrim overlaid on the image
24
     * @param array|null $overlayGradient Colored gradient that overlays the image
25
     * @param Scale|null $scale How to resize the image to fit in the bounding box
26
     * @param string|null $source Single URL to download the image from
27
     * @param string[]|null $sources Array of URLs to download the image from
28
     */
29 10
    public function __construct(
30
        public ?ImageAlign $align = null,
31
        public string $borderRadius = '0',
32
        public ?array $filter = null,
33
        public ?array $filters = null,
34
        public ?array $onFail = null,
35
        public ?array $onLoad = null,
36
        public string $overlayColor = 'none',
37
        public ?array $overlayGradient = null,
38
        public ?Scale $scale = null,
39
        public ?string $source = null,
40
        public ?array $sources = null,
41
    ) {
42 10
        parent::__construct(self::TYPE);
43
    }
44
45 6
    public function jsonSerialize(): array
46
    {
47 6
        $data = parent::jsonSerialize();
48
49 6
        if ($this->align !== null) {
50 1
            $data['align'] = $this->align->value;
51
        }
52
53 6
        if ($this->borderRadius !== '0') {
54 2
            $data['borderRadius'] = $this->borderRadius;
55
        }
56
57 6
        if ($this->filter !== null && !empty($this->filter)) {
58 1
            $data['filter'] = $this->filter;
59
        }
60
61 6
        if ($this->filters !== null && !empty($this->filters)) {
62 1
            $data['filters'] = $this->filters;
63
        }
64
65 6
        if ($this->onFail !== null && !empty($this->onFail)) {
66 1
            $data['onFail'] = $this->onFail;
67
        }
68
69 6
        if ($this->onLoad !== null && !empty($this->onLoad)) {
70 1
            $data['onLoad'] = $this->onLoad;
71
        }
72
73 6
        if ($this->overlayColor !== 'none') {
74 2
            $data['overlayColor'] = $this->overlayColor;
75
        }
76
77 6
        if ($this->overlayGradient !== null && !empty($this->overlayGradient)) {
78 1
            $data['overlayGradient'] = $this->overlayGradient;
79
        }
80
81 6
        if ($this->scale !== null) {
82 1
            $data['scale'] = $this->scale->value;
83
        }
84
85 6
        if ($this->source !== null) {
86 1
            $data['source'] = $this->source;
87
        }
88
89 6
        if ($this->sources !== null && !empty($this->sources)) {
90
            $data['sources'] = $this->sources;
91
        }
92
93 6
        return $data;
94
    }
95
}
96