Completed
Push — master ( a51466...8daf75 )
by Raffael
02:47
created

AttributeMap::map()   B

Complexity

Conditions 9
Paths 17

Size

Total Lines 54
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 54
rs 7.255
c 0
b 0
f 0
cc 9
eloc 36
nc 17
nop 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
declare(strict_types=1);
3
4
/**
5
 * Micro
6
 *
7
 * @copyright Copyright (c) 2017 gyselroth GmbH (https://gyselroth.com)
8
 * @license   MIT https://opensource.org/licenses/MIT
9
 */
10
11
namespace Micro\Auth;
12
13
use \Psr\Log\LoggerInterface as Logger;
14
15
class AttributeMap
16
{
17
    /**
18
     * Attribute map
19
     *
20
     * @var Iterable
21
     */
22
    protected $map = [];
23
24
25
    /**
26
     * Logger
27
     *
28
     * @var Logger
29
     */
30
    protected $logger;
31
32
33
    /**
34
     * Initialize
35
     *
36
     * @param   Iterable $map
37
     * @param   Logger $logger
38
     * @return  void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
39
     */
40
    public function __construct(Iterable $map, Logger $logger)
41
    {
42
        $this->logger  = $logger;
43
        $this->map     = $map;
44
    }
45
46
47
    /**
48
     * Get attribute map
49
     *
50
     * @return Iterable
51
     */
52
    public function getAttributeMap(): Iterable
53
    {
54
        return $this->map;
55
    }
56
57
58
    /**
59
     * Prepare attributes
60
     *
61
     * @param  array $data
62
     * @return array
63
     */
64
    public function map(array $data): array
65
    {
66
        $attrs = [];
67
68
        foreach ($this->map as $attr => $value) {
69
            if (array_key_exists($value['attr'], $data)) {
70
                $this->logger->info('found attribut mapping ['.$attr.'] => [('.$value['type'].') '.$value['attr'].']', [
71
                    'category' => get_class($this),
72
                ]);
73
74
                if ($value['type'] == 'array') {
75
                    $store = $data[$value['attr']];
76
                } else {
77
                    if (is_array($data[$value['attr']])) {
78
                        $store = $data[$value['attr']][0];
79
                    } else {
80
                        $store = $data[$value['attr']];
81
                    }
82
                }
83
84
                switch ($value['type']) {
85
                    case 'array':
86
                         $arr =  (array)$data[$value['attr']];
87
                          unset($arr['count']);
88
                          $attrs[$attr] = $arr;
89
                    break;
90
                        
91
                    case 'string':
92
                         $attrs[$attr]  = (string)$store;
93
                    break;
94
                                            
95
                    case 'int':
96
                         $attrs[$attr]  = (int)$store;
97
                    break;
98
                                            
99
                    case 'bool':
100
                         $attrs[$attr]  = (bool)$store;
101
                    break;
102
                    
103
                    default:
104
                        $this->logger->error('unknown attribute type ['.$value['type'].'] for attribute ['.$attr.']; use one of [array,string,int,bool]', [
105
                            'category' => get_class($this),
106
                        ]);
107
                    break;
108
                }
109
            } else {
110
                $this->logger->warning('auth attribute ['.$value['attr'].'] was not found from authentication adapter response', [
111
                    'category' => get_class($this),
112
                ]);
113
            }
114
        }
115
116
        return $attrs;
117
    }
118
}
119