SmsRuMessage::withIp()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 7
rs 10
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Kafkiansky\SmsRu\Message;
6
7
final class SmsRuMessage
8
{
9
    /**
10
     * @var Recipient
11
     */
12
    private $recipient;
13
14
    /**
15
     * @var string|null
16
     */
17
    private $ip;
18
19
    /**
20
     * @var int|null
21
     */
22
    private $time;
23
24
    /**
25
     * @var int|null
26
     */
27
    private $ttl;
28
29
    /**
30
     * @var int|null
31
     */
32
    private $dayTime;
33
34
    /**
35
     * @var int|null
36
     */
37
    private $translit;
38
39
    public function __construct(Recipient $recipient)
40
    {
41
        $this->recipient = $recipient;
42
    }
43
44
    /**
45
     * @param Recipient $recipient
46
     *
47
     * @return SmsRuMessage
48
     */
49
    public static function fromRecipient(Recipient $recipient): self
50
    {
51
        return new self($recipient);
52
    }
53
54
    /**
55
     * @return Recipient
56
     */
57
    public function getRecipient(): Recipient
58
    {
59
        return $this->recipient;
60
    }
61
62
    /**
63
     * @param string $ip
64
     *
65
     * @return $this
66
     */
67
    public function withIp(string $ip): self
68
    {
69
        if (\filter_var($ip, \FILTER_VALIDATE_IP)) {
70
            $this->ip = $ip;
71
        }
72
73
        return $this;
74
    }
75
76
    /**
77
     * @param int $time
78
     *
79
     * @return $this
80
     */
81
    public function withTime(int $time): self
82
    {
83
        $this->time = $time;
84
85
        return $this;
86
    }
87
88
    /**
89
     * @param int $ttl
90
     *
91
     * @return $this
92
     */
93
    public function withTtl(int $ttl): self
94
    {
95
        $this->ttl = $ttl;
96
97
        return $this;
98
    }
99
100
    /**
101
     * @return $this
102
     */
103
    public function enableDaytime(): self
104
    {
105
        $this->dayTime = 1;
106
107
        return $this;
108
    }
109
110
    /**
111
     * @return $this
112
     */
113
    public function enableTranslit(): self
114
    {
115
        $this->translit = 1;
116
117
        return $this;
118
    }
119
120
    /**
121
     * @return array
122
     */
123
    public function toArray(): array
124
    {
125
        $payload = [
126
            'ip'       => $this->ip,
127
            'time'     => $this->time,
128
            'ttl'      => $this->ttl,
129
            'daytime'  => $this->dayTime,
130
            'translit' => $this->translit,
131
        ];
132
133
        return \array_filter(
134
            \array_merge($payload, $this->recipient->buildMessage())
135
        );
136
    }
137
}
138