AttributeMap::map()   C
last analyzed

Complexity

Conditions 9
Paths 17

Size

Total Lines 52
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 52
rs 6.5703
c 0
b 0
f 0
cc 9
eloc 35
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
 * @author    Raffael Sahli <[email protected]>
8
 * @copyright Copyright (c) 2017 gyselroth GmbH (https://gyselroth.com)
9
 * @license   MIT https://opensource.org/licenses/MIT
10
 */
11
12
namespace Micro\Auth;
13
14
use \Psr\Log\LoggerInterface as Logger;
15
16
class AttributeMap
17
{
18
    /**
19
     * Attribute map
20
     *
21
     * @var Iterable
22
     */
23
    protected $map = [];
24
25
26
    /**
27
     * Logger
28
     *
29
     * @var Logger
30
     */
31
    protected $logger;
32
33
34
    /**
35
     * Initialize
36
     *
37
     * @param   Iterable $map
38
     * @param   Logger $logger
39
     * @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...
40
     */
41
    public function __construct(Iterable $map, Logger $logger)
42
    {
43
        $this->logger  = $logger;
44
        $this->map     = $map;
45
    }
46
47
48
    /**
49
     * Get attribute map
50
     *
51
     * @return Iterable
52
     */
53
    public function getAttributeMap(): Iterable
54
    {
55
        return $this->map;
56
    }
57
58
59
    /**
60
     * Prepare attributes
61
     *
62
     * @param  array $data
63
     * @return array
64
     */
65
    public function map(array $data): array
66
    {
67
        $attrs = [];
68
        foreach ($this->map as $attr => $value) {
69
            if (array_key_exists($value['attr'], $data)) {
70
                $this->logger->info('found attribute 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
                    $store = $data[$value['attr']];
78
                    if (is_array($store)) {
79
                        $store = $store[0];
80
                    }
81
                }
82
83
                switch ($value['type']) {
84
                    case 'array':
85
                        $arr =  (array)$data[$value['attr']];
86
                        unset($arr['count']);
87
                        $attrs[$attr] = $arr;
88
                    break;
89
                        
90
                    case 'string':
91
                         $attrs[$attr] = (string)$store;
92
                    break;
93
                                            
94
                    case 'int':
95
                         $attrs[$attr] = (int)$store;
96
                    break;
97
                                            
98
                    case 'bool':
99
                         $attrs[$attr] = (bool)$store;
100
                    break;
101
                    
102
                    default:
103
                        $this->logger->error('unknown attribute type ['.$value['type'].'] for attribute ['.$attr.']; use one of [array,string,int,bool]', [
104
                            'category' => get_class($this),
105
                        ]);
106
                    break;
107
                }
108
            } else {
109
                $this->logger->warning('auth attribute ['.$value['attr'].'] was not found from authentication adapter response', [
110
                    'category' => get_class($this),
111
                ]);
112
            }
113
        }
114
115
        return $attrs;
116
    }
117
}
118