Completed
Push — master ( 8f139f...a8687a )
by Joachim
04:58
created

Message::chunkCount()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 0
cts 10
cp 0
rs 9.2
c 0
b 0
f 0
cc 4
eloc 7
nc 6
nop 0
crap 20
1
<?php declare(strict_types = 1);
2
namespace Loevgaard\Linkmobility\ValueObject;
3
4
use Loevgaard\Linkmobility\GSM7\GSM7;
5
6
class Message extends StringValueObject
7
{
8
    /**
9
     * @var bool
10
     */
11
    private $gsm7;
12
13
    public function chunkCount() : int
14
    {
15
        $length = $this->length();
16
        $lengthThreshold = $this->isGsm7() ? 160 : 70;
17
18
        if ($length > $lengthThreshold) {
19
            $chunkDivisor = $this->isGsm7() ? 153 : 67;
20
            return (int)ceil($length / $chunkDivisor);
21
        }
22
23
        return 1;
24
    }
25
26
    public function length() : int
27
    {
28
        $length = mb_strlen($this->value);
29
30
        if (!$this->isGsm7()) {
31
            return $length;
32
        }
33
34
        $doubles = GSM7::DOUBLES;
35
36
        for ($i = 0; $i < $length; $i++) {
37
            if (in_array($this->value[$i], $doubles)) {
38
                $length++;
39
            }
40
        }
41
42
        return $length;
43
    }
44
45
    public function isGsm7() : bool
46
    {
47
        if (is_null($this->gsm7)) {
48
            $this->gsm7 = GSM7::isGSM7($this->value);
49
        }
50
51
        return $this->gsm7;
52
    }
53
}
54