GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

Imei   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 16
dl 0
loc 63
rs 10
c 0
b 0
f 0
wmc 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A createFromHex() 0 5 1
A __construct() 0 7 3
A __toString() 0 3 1
A isLuhn() 0 8 3
A getImei() 0 3 1
A jsonSerialize() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Uro\TeltonikaFmParser\Model;
6
7
use JsonSerializable;
8
use Uro\TeltonikaFmParser\Model\Exception\InvalidArgumentException;
9
10
class Imei implements Model, JsonSerializable
11
{
12
    const IMEI_LENGTH = 15;
13
14
    /**
15
     * @var string
16
     */
17
    private $imei;
18
19
    /**
20
     * @param string $imei
21
     *
22
     * @throws InvalidArgumentException
23
     */
24
    public function __construct(string $imei)
25
    {
26
        if (!$this->isLuhn($imei) || strlen($imei) !== self::IMEI_LENGTH) {
27
            throw new InvalidArgumentException("IMEI number is not valid.");
28
        }
29
30
        $this->imei = $imei;
31
    }
32
33
    /**
34
     * @return string
35
     */
36
    public function getImei(): string
37
    {
38
        return $this->imei;
39
    }
40
41
    public function jsonSerialize(): array
42
    {
43
        return [
44
            'imei' => $this->getImei()
45
        ];
46
    }
47
48
    public function __toString(): string
49
    {
50
        return $this->getImei();
51
    }
52
53
    /**
54
     * @param string $imei
55
     *
56
     * @return bool
57
     */
58
    private function isLuhn(string $imei): bool
59
    {
60
        $str = '';
61
        foreach (str_split(strrev((string)$imei)) as $i => $d) {
62
            $str .= $i % 2 !== 0 ? $d * 2 : $d;
63
        }
64
65
        return array_sum(str_split($str)) % 10 === 0;
66
    }
67
68
    public static function createFromHex(string $hexData): Imei
69
    {
70
        $imei = hex2bin($hexData);
71
72
        return new Imei($imei);
73
    }
74
}
75