AppIdResourceOwner::toArray()   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 Jampire\OAuth2\Client\Provider;
4
5
use League\OAuth2\Client\Provider\ResourceOwnerInterface;
6
7
/**
8
 * Class AppIdResourceOwner
9
 *
10
 * Individual resource owners may introduce more attributes, as needed.
11
 *
12
 * @author  Dzianis Kotau <[email protected]>
13
 * @package Jampire\OAuth2\Client\Provider
14
 */
15
class AppIdResourceOwner implements ResourceOwnerInterface
16
{
17
    /**
18
     * Raw response
19
     *
20
     * @var array
21
     */
22
    protected $response;
23
24
    /** @var array */
25
    protected $attributes = [];
26
27
    /**
28
     * Creates new resource owner.
29
     *
30
     * @param array $response
31
     */
32 3
    public function __construct(array $response = [])
33
    {
34 3
        $this->response = $response;
35 3
        $this->attributes = $this->getAttributes();
36 3
    }
37
38
    /**
39
     * Parsed canonical Lotus Notes Id to human one.
40
     *
41
     * For example
42
     * "CN=Dzianis Kotau/OU=Org1/OU=Org2/O=IBM@IBMMail" is parsed to
43
     * "Dzianis Kotau/Org1/Org2/IBM"
44
     *
45
     * @param string $lotusNotesId Canonical Lotus Notes Id
46
     *
47
     * @return string Human Lotus Notes Id
48
     * @author Dzianis Kotau <[email protected]>
49
     */
50 2
    public static function parseLotusNotesId(string $lotusNotesId): string
51
    {
52 2
        $arr = explode('/', $lotusNotesId);
53 2
        $idArr = [];
54 2
        foreach ($arr as $value) {
55 2
            $value = explode('=', $value);
56 2
            $value = $value[1] ?? null;
57 2
            if ($value === null) {
58 1
                continue;
59
            }
60 2
            $idArr[] = $value;
61
        }
62
63 2
        $id = implode('/', $idArr);
64
65 2
        if (($pos = strpos($id, '@')) !== false) {
66 2
            $id = substr($id, 0, $pos);
67
        }
68
69 2
        return $id;
70
    }
71
72
    /**
73
     * Returns the identifier of the authorized resource owner.
74
     *
75
     * @return string
76
     * @author Dzianis Kotau <[email protected]>
77
     */
78 1
    public function getId(): string
79
    {
80 1
        return $this->response['sub'] ?? '';
81
    }
82
83
    /**
84
     * Get resource owner email
85
     *
86
     * @return string
87
     * @author Dzianis Kotau <[email protected]>
88
     */
89 1
    public function getEmail(): string
90
    {
91 1
        return strtolower($this->response['email']) ?? '';
92
    }
93
94
    /**
95
     * Return all of the owner details available as an array.
96
     *
97
     * @return array
98
     */
99 2
    public function toArray(): array
100
    {
101 2
        return $this->response;
102
    }
103
104
    /**
105
     * @author Dzianis Kotau <[email protected]>
106
     * @return string
107
     */
108 1
    public function getFullName(): string
109
    {
110 1
        return $this->response['name'] ?? '';
111
    }
112
113
    /**
114
     * @author Dzianis Kotau <[email protected]>
115
     * @return string
116
     */
117 1
    public function getCnum(): string
118
    {
119 1
        return $this->attributes['cnum'] ?? '';
120
    }
121
122
    /**
123
     * @author Dzianis Kotau <[email protected]>
124
     * @return string
125
     */
126 3
    public function getLotusNotesId(): string
127
    {
128 3
        $lotusNotesId = $this->attributes['lotusnotesid'] ?? '';
129 3
        if (empty($lotusNotesId)) {
130 1
            return $lotusNotesId;
131
        }
132
133 2
        return self::parseLotusNotesId($lotusNotesId);
134
    }
135
136
    /**
137
     * @author Dzianis Kotau <[email protected]>
138
     * @return array
139
     */
140 1
    public function getIbmInfo(): array
141
    {
142 1
        return $this->attributes['ibminfo'] ?? [];
143
    }
144
145
    /**
146
     * @author Dzianis Kotau <[email protected]>
147
     * @return string
148
     */
149 1
    public function getLocation(): string
150
    {
151 1
        return $this->attributes['locate'] ?? '';
152
    }
153
154
    /**
155
     * @return string
156
     * @author Dzianis Kotau <[email protected]>
157
     */
158 1
    public function getUid(): string
159
    {
160 1
        return $this->attributes['uid'] ?? '';
161
    }
162
163
    /**
164
     * @return array
165
     * @author Dzianis Kotau <[email protected]>
166
     */
167 3
    private function getAttributes(): array
168
    {
169 3
        $response = $this->response;
170 3
        if (empty($response['identities'])
171 2
                  || empty($response['identities'][0])
172 2
                  || empty($response['identities'][0]['idpUserInfo'])
173 3
                  || empty($response['identities'][0]['idpUserInfo']['attributes'])) {
174 1
            return [];
175
        }
176
177 2
        return $response['identities'][0]['idpUserInfo']['attributes'];
178
    }
179
}
180