Recipient::toIconv()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Kafkiansky\SmsRu\Message;
6
7
abstract class Recipient
8
{
9
    /**
10
     * @return array|string
11
     */
12
    abstract public function getTo();
13
14
    /**
15
     * @return string
16
     */
17
    abstract public function getMessage(): string;
18
19
    /**
20
     * @return bool
21
     */
22
    abstract public function useIconv(): bool;
23
24
    /**
25
     * @return bool
26
     */
27
    abstract public function asMulti(): bool;
28
29
    /**
30
     * @return array
31
     */
32
    public function buildMessage(): array
33
    {
34
        $options = [];
35
36
        if ($this->asMulti()) {
37
            /** @var To $to */
38
            foreach ($this->getTo() as $to) {
39
                $options['multi'][$to->getTo()] = $this->defineEncoding($to->getMessage());
40
            }
41
42
            return $options;
43
        }
44
45
        $options['to'] = $this->collapseRecipients($this->getTo());
46
        $options['msg'] = $this->defineEncoding($this->getMessage());
47
48
        return $options;
49
    }
50
51
    /**
52
     * @param string $message
53
     *
54
     * @return bool|false|string
55
     */
56
    private function defineEncoding(string $message)
57
    {
58
        return $this->useIconv() ? $this->toIconv($message) : $message;
59
    }
60
61
    /**
62
     * @param $tos
63
     *
64
     * @return string
65
     */
66
    private function collapseRecipients($tos): string
67
    {
68
        return \is_array($tos) ? \implode(',', $tos) : $tos;
69
    }
70
71
    /**
72
     * @param string $message
73
     *
74
     * @return bool|false|string
75
     */
76
    private function toIconv(string $message)
77
    {
78
        return \iconv('windows-1251', 'utf-8', $message);
79
    }
80
}
81