Passed
Push — master ( 3948f8...d3c224 )
by Dāvis
07:09
created

InboxUser::getFirstName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Sludio\HelperBundle\Openid\Client;
4
5
use Sludio\HelperBundle\Oauth\Component\SocialUserInterface;
6
7
class InboxUser implements SocialUserInterface
8
{
9
    /**
10
     * @var array
11
     */
12
    protected $response;
13
14
    /**
15
     * @var integer
16
     */
17
    protected $originalId;
18
19
    /**
20
     * @var integer
21
     */
22
    protected $id;
23
24
    /**
25
     * @var string
26
     */
27
    protected $email;
28
29
    /**
30
     * @var string
31
     */
32
    protected $firstName;
33
34
    /**
35
     * @var string
36
     */
37
    protected $lastName;
38
39
    /**
40
     * @var string
41
     */
42
    protected $username;
43
44
    /**
45
     * @var bool
46
     */
47
    protected $returnsEmail = true;
48
49
    /**
50
     * @return bool
51
     */
52
    public function returnsEmail()
53
    {
54
        return $this->returnsEmail;
55
    }
56
57
    /**
58
     * @param  array       $response
59
     * @param integer|null $id
60
     */
61
    public function __construct(array $response, $id = null)
62
    {
63
        $this->response = $response;
64
        $this->originalId = $id;
65
66
        $this->id = $this->getField('openid_sreg_email');
67
68
        $this->email = $this->getField('openid_sreg_email');
69
70
        $name = $this->getField('openid_sreg_fullname');
71
        $data = explode(' ', $name, 2);
72
73
        $this->firstName = $data[0];
74
75
        $this->lastName = $data[1];
76
77
        $username = explode('@', $this->originalId ?: $this->email);
78
        $username = preg_replace('/[^a-z\d]/i', '', $username[0]);
79
        $this->username = $username;
80
    }
81
82
    /**
83
     * Returns a field from the Graph node data.
84
     *
85
     * @param string $key
86
     *
87
     * @return mixed|null
88
     */
89
    private function getField($key)
90
    {
91
        return isset($this->response[$key]) ? $this->response[$key] : null;
92
    }
93
94
    /**
95
     * Get the value of Original Id
96
     *
97
     * @return integer
98
     */
99
    public function getOriginalId()
100
    {
101
        return $this->originalId;
102
    }
103
104
    /**
105
     * Get the value of Id
106
     *
107
     * @return integer
108
     */
109
    public function getId()
110
    {
111
        return $this->id;
112
    }
113
114
    /**
115
     * Get the value of Email
116
     *
117
     * @return string
118
     */
119
    public function getEmail()
120
    {
121
        return $this->email;
122
    }
123
124
    /**
125
     * Get the value of First Name
126
     *
127
     * @return string
128
     */
129
    public function getFirstName()
130
    {
131
        return $this->firstName;
132
    }
133
134
    /**
135
     * Get the value of Last Name
136
     *
137
     * @return string
138
     */
139
    public function getLastName()
140
    {
141
        return $this->lastName;
142
    }
143
144
    /**
145
     * Get the value of Username
146
     *
147
     * @return string
148
     */
149
    public function getUsername()
150
    {
151
        return $this->username;
152
    }
153
}
154