Completed
Push — master ( 58620c...f23d9f )
by Vitaliy
02:20
created

ApnId::fromId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
/*
4
 * This file is part of the AppleApnPush package
5
 *
6
 * (c) Vitaliy Zhuk <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code
10
 */
11
12
namespace Apple\ApnPush\Model;
13
14
/**
15
 * UUID identifier for APN
16
 */
17
class ApnId
18
{
19
    /**
20
     * @var string
21
     */
22
    private $value;
23
24
    /**
25
     * Create ApnID from null
26
     *
27
     * @return ApnId
28
     */
29
    public static function fromNull() : ApnId
30
    {
31
        return new self('');
32
    }
33
34
    /**
35
     * Create from id
36
     *
37
     * @param string $id
38
     *
39
     * @return ApnId
40
     *
41
     * @throws \InvalidArgumentException
42
     */
43
    public static function fromId(string $id) : ApnId
44
    {
45
        self::validateId($id);
46
47
        return new self($id);
48
    }
49
50
    /**
51
     * Is null Apn ID?
52
     *
53
     * @return bool
54
     */
55
    public function isNull()
56
    {
57
        return $this->value === '';
58
    }
59
60
    /**
61
     * Get value
62
     *
63
     * @return string
64
     *
65
     * @throws \LogicException
66
     */
67
    public function getValue() : string
68
    {
69
        if ($this->isNull()) {
70
            throw new \LogicException('Can not get value from null Apn ID object.');
71
        }
72
73
        return (string) $this->value;
74
    }
75
76
    /**
77
     * Constructor.
78
     *
79
     * @param string $id
80
     */
81
    private function __construct($id)
82
    {
83
        $this->value = $id;
84
    }
85
86
    /**
87
     * Validate ID
88
     *
89
     * @param string $id
90
     *
91
     * @throws \InvalidArgumentException
92
     */
93
    private static function validateId(string $id)
94
    {
95
        if (!preg_match('/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/', $id)) {
96
            throw new \InvalidArgumentException(sprintf(
97
                'Invalid UUID identifier "%s".',
98
                $id
99
            ));
100
        }
101
    }
102
}
103