GitHub::fullNameNormalizer()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
rs 10
cc 2
eloc 2
nc 2
nop 1
1
<?php
2
3
/*
4
 * This file is part of the Oryzone PHPoAuthUserData package <https://github.com/Oryzone/PHPoAuthUserData>.
5
 *
6
 * (c) Oryzone, developed by Luciano Mammino <[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 OAuth\UserData\Extractor;
13
14
use OAuth\UserData\Utils\ArrayUtils;
15
16
/**
17
 * Class GitHub
18
 * @package OAuth\UserData\Extractor
19
 */
20
class GitHub extends LazyExtractor
21
{
22
    const REQUEST_PROFILE = '/user';
23
    const REQUEST_EMAIL = '/user/emails';
24
25
    public function __construct()
26
    {
27
        parent::__construct(
28
            self::getLoadersMap(),
29
            self::getDefaultNormalizersMap(),
30
            self::getSupportedFields()
31
        );
32
    }
33
34
    protected function profileLoader()
35
    {
36
        return json_decode($this->service->request(self::REQUEST_PROFILE), true);
37
    }
38
39
    protected function emailLoader()
40
    {
41
        return json_decode(
42
            $this->service->request(
43
                self::REQUEST_EMAIL,
44
                'GET',
45
                array(),
46
                array('Accept' => 'application/vnd.github.v3')
47
            ),
48
            true
49
        );
50
    }
51
52
    protected static function getSupportedFields()
53
    {
54
        return array(
55
            self::FIELD_UNIQUE_ID,
56
            self::FIELD_USERNAME,
57
            self::FIELD_FIRST_NAME,
58
            self::FIELD_LAST_NAME,
59
            self::FIELD_FULL_NAME,
60
            self::FIELD_EMAIL,
61
            self::FIELD_LOCATION,
62
            self::FIELD_DESCRIPTION,
63
            self::FIELD_IMAGE_URL,
64
            self::FIELD_PROFILE_URL,
65
            self::FIELD_VERIFIED_EMAIL,
66
            self::FIELD_EXTRA
67
        );
68
    }
69
70
    protected static function getLoadersMap()
71
    {
72
        return array_merge(self::getDefaultLoadersMap(), array(
73
            self::FIELD_EMAIL          => 'email',
74
            self::FIELD_VERIFIED_EMAIL => 'email'
75
        ));
76
    }
77
78
    protected function uniqueIdNormalizer($data)
79
    {
80
        return isset($data['id']) ? $data['id'] : null;
81
    }
82
83
    protected function usernameNormalizer($data)
84
    {
85
        return isset($data['login']) ? $data['login'] : null;
86
    }
87
88
    protected function firstNameNormalizer()
89
    {
90
        $fullName = $this->getField(self::FIELD_FULL_NAME);
91
        if ($fullName) {
92
            $names = explode(' ', $fullName);
93
94
            return $names[0];
95
        }
96
97
        return null;
98
    }
99
100
    protected function lastNameNormalizer()
101
    {
102
        $fullName = $this->getField(self::FIELD_FULL_NAME);
103
        if ($fullName) {
104
            $names = explode(' ', $fullName);
105
106
            return $names[sizeof($names) - 1];
107
        }
108
109
        return null;
110
    }
111
112
    protected function fullNameNormalizer($data)
113
    {
114
        return isset($data['name']) ? $data['name'] : null;
115
    }
116
117
    protected function emailNormalizer($emails)
118
    {
119
        $email = $this->getEmailObject($emails);
120
121
        return $email['email'];
122
    }
123
124
    protected function locationNormalizer($data)
125
    {
126
        return isset($data['location']) ? $data['location'] : null;
127
    }
128
129
    protected function descriptionNormalizer($data)
130
    {
131
        return isset($data['bio']) ? $data['bio'] : null;
132
    }
133
134
    protected function imageUrlNormalizer($data)
135
    {
136
        return isset($data['avatar_url']) ? $data['avatar_url'] : null;
137
    }
138
139
    protected function profileUrlNormalizer($data)
140
    {
141
        return isset($data['html_url']) ? $data['html_url'] : null;
142
    }
143
144
    protected function verifiedEmailNormalizer($emails)
145
    {
146
        $email = $this->getEmailObject($emails);
147
148
        return $email['verified'];
149
    }
150
151
    protected function extraNormalizer($data)
152
    {
153
        return ArrayUtils::removeKeys($data, array(
154
            'id',
155
            'login',
156
            'name',
157
            'location',
158
            'bio',
159
            'avatar_url',
160
            'html_url'
161
        ));
162
    }
163
164
    /**
165
     * Get the right email address from the one's the user provides.
166
     *
167
     * @param array $emails The array of email array objects provided by GitHub.
168
     *
169
     * @return array The email array object.
170
     */
171
    private function getEmailObject($emails)
172
    {
173
        // Try to find an email address which is primary and verified.
174
        foreach ($emails as $email) {
175
            if ($email['primary'] && $email['verified']) {
176
                return $email;
177
            }
178
        }
179
180
        // Try to find an email address which is primary.
181
        foreach ($emails as $email) {
182
            if ($email['primary']) {
183
                return $email;
184
            }
185
        }
186
187
        return $emails[0];
188
    }
189
}
190