Passed
Pull Request — master (#89)
by Romain
02:49
created

ThreadControl::create()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 1
cts 1
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 2
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Kerox\Messenger\Model;
6
7
use Kerox\Messenger\Helper\ValidatorTrait;
8
9
class ThreadControl implements \JsonSerializable
10
{
11
    use ValidatorTrait;
12
13
    /**
14
     * @var int
15
     */
16
    protected $recipientId;
17
18
    /**
19
     * @var null|int
20
     */
21
    protected $targetAppId;
22
23
    /**
24
     * @var null|string
25
     */
26
    protected $metadata;
27
28
    /**
29
     * PassThreadControl constructor.
30
     *
31
     * @param int      $recipientId
32 2
     * @param int|null $targetAppId
33
     */
34 2
    public function __construct(int $recipientId, ?int $targetAppId = null)
35 2
    {
36 2
        $this->recipientId = $recipientId;
37
        $this->targetAppId = $targetAppId;
38
    }
39
40
    /**
41 2
     * @param int      $recipientId
42
     * @param int|null $targetAppId
43 2
     *
44
     * @return \Kerox\Messenger\Model\ThreadControl
45 2
     */
46 2
    public static function create(int $recipientId, ?int $targetAppId = null): self
47
    {
48
        return new self($recipientId, $targetAppId);
49
    }
50
51 2
    /**
52
     * @param string $metadata
53
     *
54
     * @throws \Exception
55 2
     */
56
    public function setMetadata(string $metadata): void
57 2
    {
58 2
        $this->isValidString($metadata, 1000);
59
60
        $this->metadata = $metadata;
61 2
    }
62
63
    /**
64
     * @return array
65
     */
66
    public function toArray(): array
67
    {
68
        $array = [
69
            'recipient'     => [
70
                'id' => $this->recipientId,
71
            ],
72
            'target_app_id' => $this->targetAppId,
73
            'metadata'      => $this->metadata,
74
        ];
75
76
        return array_filter($array);
77
    }
78
79
    /**
80
     * @return array
81
     */
82
    public function jsonSerialize(): array
83
    {
84
        return $this->toArray();
85
    }
86
}
87