OpenGraphElement::toArray()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 4
cts 4
cp 1
rs 9.9666
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Kerox\Messenger\Model\Message\Attachment\Template\Element;
6
7
use Kerox\Messenger\Helper\ValidatorTrait;
8
use Kerox\Messenger\Model\Common\Button\AbstractButton;
9
10
class OpenGraphElement implements \JsonSerializable
11
{
12
    use ValidatorTrait;
13
14
    /**
15
     * @var string
16
     */
17
    protected $url;
18
19
    /**
20
     * @var \Kerox\Messenger\Model\Common\Button\AbstractButton[]
21
     */
22
    protected $buttons = [];
23
24
    /**
25
     * OpenGraphElement constructor.
26
     *
27
     * @throws \Kerox\Messenger\Exception\MessengerException
28
     */
29 2
    public function __construct(string $url)
30
    {
31 2
        $this->isValidUrl($url);
32
33 2
        $this->url = $url;
34 2
    }
35
36
    /**
37
     * @throws \Kerox\Messenger\Exception\MessengerException
38
     *
39
     * @return \Kerox\Messenger\Model\Message\Attachment\Template\Element\OpenGraphElement
40
     */
41 2
    public static function create(string $url): self
42
    {
43 2
        return new self($url);
44
    }
45
46
    /**
47
     * @param \Kerox\Messenger\Model\Common\Button\AbstractButton[] $buttons
48
     *
49
     * @throws \Kerox\Messenger\Exception\MessengerException
50
     *
51
     * @return \Kerox\Messenger\Model\Message\Attachment\Template\Element\OpenGraphElement
52
     */
53 2 View Code Duplication
    public function setButtons(array $buttons): self
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
54
    {
55 2
        $this->isValidArray($buttons, 1);
56 2
        $this->isValidButtons($buttons, $this->getAllowedButtonsType());
57
58 1
        $this->buttons = $buttons;
59
60 1
        return $this;
61
    }
62
63 2
    protected function getAllowedButtonsType(): array
64
    {
65
        return [
66 2
            AbstractButton::TYPE_WEB_URL,
67
        ];
68
    }
69
70 1
    public function toArray(): array
71
    {
72
        $array = [
73 1
            'url' => $this->url,
74 1
            'buttons' => $this->buttons,
75
        ];
76
77 1
        return array_filter($array);
78
    }
79
80 1
    public function jsonSerialize(): array
81
    {
82 1
        return $this->toArray();
83
    }
84
}
85