User::email()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
namespace ZoiloMora\ElasticAPM\Events\Common;
4
5
use ZoiloMora\ElasticAPM\Helper\Encoding;
6
7
class User implements \JsonSerializable
8
{
9
    /**
10
     * Identifier of the logged in user, e.g. the primary key of the user
11
     *
12
     * @var string|int|null
13
     */
14
    private $id;
15
16
    /**
17
     * Email of the logged in user
18
     *
19
     * @var string|null
20
     */
21
    private $email;
22
23
    /**
24
     * The username of the logged in user
25
     *
26
     * @var string|null
27
     */
28
    private $username;
29
30
    /**
31
     * @param int|string|null $id
32
     * @param string|null $email
33
     * @param string|null $username
34
     */
35 6
    public function __construct($id = null, $email = null, $username = null)
36
    {
37 6
        $this->assertId($id);
38 5
        $this->assertEmail($email);
39 4
        $this->assertUsername($username);
40
41 3
        $this->id = $id;
42 3
        $this->email = $email;
43 3
        $this->username = $username;
44 3
    }
45
46
    /**
47
     * @param mixed $id
48
     *
49
     * @return void
50
     *
51
     * @throws \InvalidArgumentException
52
     */
53 6
    private function assertId($id)
54
    {
55 6
        if (null !== $id && false === is_int($id) && false === is_string($id)) {
56 1
            throw new \InvalidArgumentException('[id] must be one of these types: integer, string or null.');
57
        }
58 5
    }
59
60
    /**
61
     * @param mixed $email
62
     *
63
     * @return void
64
     *
65
     * @throws \InvalidArgumentException
66
     */
67 5
    private function assertEmail($email)
68
    {
69 5
        if (null !== $email && false === is_string($email)) {
70 1
            throw new \InvalidArgumentException('[email] must be one of these types: string or null.');
71
        }
72 4
    }
73
74
    /**
75
     * @param mixed $username
76
     *
77
     * @return void
78
     *
79
     * @throws \InvalidArgumentException
80
     */
81 4
    private function assertUsername($username)
82
    {
83 4
        if (null !== $username && false === is_string($username)) {
84 1
            throw new \InvalidArgumentException('[username] must be one of these types: string or null.');
85
        }
86 3
    }
87
88
    /**
89
     * @return int|string|null
90
     */
91 1
    public function id()
92
    {
93 1
        return $this->id;
94
    }
95
96
    /**
97
     * @return string|null
98
     */
99 1
    public function email()
100
    {
101 1
        return $this->email;
102
    }
103
104
    /**
105
     * @return string|null
106
     */
107 1
    public function username()
108
    {
109 1
        return $this->username;
110
    }
111
112
    /**
113
     * @return array
114
     */
115 1
    public function jsonSerialize()
116
    {
117
        return [
118 1
            'id' => Encoding::keywordField($this->id),
119 1
            'email' => Encoding::keywordField($this->email),
120 1
            'username' => Encoding::keywordField($this->username),
121 1
        ];
122
    }
123
}
124