AttributeMap::getAttributeMap()   A
last analyzed

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
declare(strict_types=1);
4
5
/**
6
 * Micro
7
 *
8
 * @copyright   Copryright (c) 2015-2018 gyselroth GmbH (https://gyselroth.com)
9
 * @license     MIT https://opensource.org/licenses/MIT
10
 */
11
12
namespace Micro\Auth;
13
14
use Closure;
15
use Psr\Log\LoggerInterface as Logger;
16
17
class AttributeMap implements AttributeMapInterface
18
{
19
    /**
20
     * Attribute map.
21
     *
22
     * @var iterable
23
     */
24
    protected $map = [];
25
26
    /**
27
     * Logger.
28
     *
29
     * @var Logger
30
     */
31
    protected $logger;
32
33
    /**
34
     * Custom mapper.
35
     *
36
     * @var array
37
     */
38
    protected $mapper = [];
39
40
    /**
41
     * Initialize.
42
     *
43
     * @param iterable $map
44
     * @param Logger   $logger
45
     */
46
    public function __construct(Iterable $map, Logger $logger)
47
    {
48
        $this->logger = $logger;
49
        $this->map = $map;
50
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55
    public function getAttributeMap(): Iterable
56
    {
57
        return $this->map;
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63
    public function addMapper(string $type, Closure $closure): AttributeMapInterface
64
    {
65
        $this->mapper[$type] = $closure;
66
67
        return $this;
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73
    public function map(array $data): array
74
    {
75
        $attrs = [];
76
        foreach ($this->map as $attr => $value) {
77
            if (array_key_exists($value['attr'], $data)) {
78
                $this->logger->info('found attribute mapping ['.$attr.'] => [('.$value['type'].') '.$value['attr'].']', [
79
                    'category' => get_class($this),
80
                ]);
81
82
                if ('array' === $value['type']) {
83
                    $store = $data[$value['attr']];
84
                } else {
85
                    $store = $data[$value['attr']];
86
                    if (is_array($store)) {
87
                        $store = $store[0];
88
                    }
89
                }
90
91
                if (isset($this->mapper[$value['type']])) {
92
                    $attrs[$attr] = $this->mapper[$value['type']]->call($this, $store);
93
                }
94
95
                switch ($value['type']) {
96
                    case 'array':
97
                        $arr = (array) $data[$value['attr']];
98
                        unset($arr['count']);
99
                        $attrs[$attr] = $arr;
100
101
                    break;
102
                    case 'string':
103
                         $attrs[$attr] = (string) $store;
104
105
                    break;
106
                    case 'int':
107
                         $attrs[$attr] = (int) $store;
108
109
                    break;
110
                    case 'bool':
111
                         $attrs[$attr] = (bool) $store;
112
113
                    break;
114
                    default:
115
                        $this->logger->error('unknown attribute type ['.$value['type'].'] for attribute ['.$attr.']; use one of [array,string,int,bool]', [
116
                            'category' => get_class($this),
117
                        ]);
118
119
                    break;
120
                }
121
            } else {
122
                $this->logger->warning('auth attribute ['.$value['attr'].'] was not found from authentication adapter response', [
123
                    'category' => get_class($this),
124
                ]);
125
            }
126
        }
127
128
        return $attrs;
129
    }
130
}
131