DeviceEntity::getPlatform()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
/**
3
 * Push notification server example (http://github.com/juliangut/tify_example)
4
 *
5
 * @link https://github.com/juliangut/tify_example for the canonical source repository
6
 *
7
 * @license https://github.com/juliangut/tify_example/blob/master/LICENSE
8
 */
9
10
namespace Jgut\Pusher\Entity;
11
12
use Doctrine\ORM\Mapping as ORM;
13
use Ramsey\Uuid\Uuid;
14
15
/**
16
 * Class Installation
17
 *
18
 * @ORM\Entity(repositoryClass="\Jgut\Pusher\Repository\DeviceRepository")
19
 * @ORM\Table(name="device")
20
 */
21
class DeviceEntity implements \JsonSerializable
22
{
23
    const PLATFORM_ANDROID = 'android';
24
    const PLATFORM_IOS = 'ios';
25
26
    const ANDROID_TOKEN_PATTERN = '/^[0-9a-zA-Z-_.]+$/';
27
    const IOS_TOKEN_PATTERN = '/^[\da-f]{64}$/';
28
29
    /**
30
     * @ORM\Id
31
     * @ORM\Column(type="uuid")
32
     * @ORM\GeneratedValue(strategy="CUSTOM")
33
     * @ORM\CustomIdGenerator(class="Ramsey\Uuid\Doctrine\UuidGenerator")
34
     *
35
     * @var \Ramsey\Uuid\Uuid
36
     */
37
    protected $uuid;
38
39
    /**
40
     * @ORM\Column(type="string", unique=true, length=255)
41
     *
42
     * @var string
43
     */
44
    private $token;
45
46
    /**
47
     * @ORM\Column(type="string", length=10)
48
     *
49
     * @var string
50
     */
51
    private $platform;
52
53
    /**
54
     * @return \Ramsey\Uuid\Uuid
55
     */
56
    public function getUuid()
57
    {
58
        return $this->uuid;
59
    }
60
61
    /**
62
     * @param Uuid $uuid
63
     *
64
     * @return $this
65
     */
66
    public function setUuid(Uuid $uuid)
67
    {
68
        $this->uuid = $uuid;
69
70
        return $this;
71
    }
72
73
    /**
74
     * @return string
75
     */
76
    public function getToken()
77
    {
78
        return $this->token;
79
    }
80
81
    /**
82
     * @param string $token
83
     *
84
     * @throws \InvalidArgumentException
85
     *
86
     * @return $this
87
     */
88
    public function setToken($token)
89
    {
90
        $token = trim($token);
91
92
        if (!preg_match(static::ANDROID_TOKEN_PATTERN, $token) && !preg_match(static::IOS_TOKEN_PATTERN, $token)) {
93
            throw new \InvalidArgumentException(sprintf('"%s" is not a supported device token', $token));
94
        }
95
96
        $this->token = $token;
97
98
        return $this;
99
    }
100
101
    /**
102
     * @return string
103
     */
104
    public function getPlatform()
105
    {
106
        return $this->platform;
107
    }
108
109
    /**
110
     * @param string $platform
111
     *
112
     * @throws \InvalidArgumentException
113
     *
114
     * @return $this
115
     */
116
    public function setPlatform($platform)
117
    {
118
        $validPlatforms = [self::PLATFORM_ANDROID, self::PLATFORM_IOS];
119
120
        $platform = strtolower(trim($platform));
121
        if (!in_array($platform, $validPlatforms, true)) {
122
            throw new \InvalidArgumentException(sprintf('"%s" is not a valid device platform', $platform));
123
        }
124
125
        $this->platform = $platform;
126
127
        return $this;
128
    }
129
130
    /**
131
     * {@inheritdoc}
132
     */
133
    public function jsonSerialize()
134
    {
135
        return [
136
            'uuid' => $this->uuid,
137
            'token' => $this->token,
138
            'platform' => $this->platform,
139
        ];
140
    }
141
}
142