Completed
Push — master ( 6a1fa2...c405e7 )
by Dmitry
06:02
created

ClaimsProvider   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 0
dl 0
loc 35
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getClaims() 0 13 3
A claimBuilders() 0 18 1
1
<?php
2
3
namespace hiam\mrdp\providers;
4
5
use hiam\mrdp\models\Identity;
6
use hiam\providers\ClaimsProviderInterface;
7
use yii\helpers\ArrayHelper;
8
use yii\web\IdentityInterface;
9
10
class ClaimsProvider implements ClaimsProviderInterface
11
{
12
    public function getClaims(IdentityInterface $identity, string $claims): \stdClass
13
    {
14
        $claimed = explode(' ', trim($claims));
15
16
        $response = [];
17
        foreach ($this->claimBuilders() as $claim => $provider) {
18
            if (\in_array($claim, $claimed, true)) {
19
                $response = array_merge($response, $provider($identity));
20
            }
21
        }
22
23
        return (object)$response;
24
    }
25
26
    public function claimBuilders(): array
27
    {
28
        return [
29
            'profile' => function (Identity $identity) {
30
                return ArrayHelper::toArray($identity, [
31
                    Identity::class => [
32
                        'name' => function ($model) {
33
                            return "{$model->first_name} {$model->last_name}";
34
                        },
35
                        'username',
36
                    ]
37
                ]);
38
            },
39
            'email' => function (Identity $identity) {
40
                return ['email' => $identity->email];
41
            },
42
        ];
43
    }
44
}
45