HydrateWithMapTrait   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
c 1
b 0
f 0
dl 0
loc 32
ccs 13
cts 13
cp 1
rs 10
wmc 5

1 Method

Rating   Name   Duplication   Size   Complexity  
A hydrateUserWithAttributesMap() 0 23 5
1
<?php
2
3
namespace DoL\LdapBundle\Hydrator;
4
5
use Symfony\Component\Security\Core\User\UserInterface;
6
7
/**
8
 * Offer methods for dynamic hydration based in attribute-method maps.
9
 *
10
 * @author DarwinOnLine
11
 * @author Maks3w
12
 *
13
 * @see https://github.com/DarwinOnLine/DoLLdapBundle
14
 */
15
trait HydrateWithMapTrait
16
{
17
    /**
18
     * Fill the given user with the following the attribute-method map.
19
     *
20
     * @param UserInterface $user               target user
21
     * @param array[]       $ldapUserAttributes raw LDAP data
22
     * @param string[]      $attributeMap       attribute-method map
23
     */
24 5
    protected function hydrateUserWithAttributesMap(
25
        UserInterface $user,
26
        array $ldapUserAttributes,
27
        array $attributeMap
28
    ) {
29 5
        foreach ($attributeMap as $attr) {
30 5
            if (!array_key_exists($attr['ldap_attr'], $ldapUserAttributes)) {
31 5
                continue;
32
            }
33
34 4
            $ldapValue = $ldapUserAttributes[$attr['ldap_attr']];
35
36 4
            if (array_key_exists('count', $ldapValue)) {
37 2
                unset($ldapValue['count']);
38 2
            }
39
40 4
            if (1 === count($ldapValue)) {
41 2
                $value = array_shift($ldapValue);
42 2
            } else {
43 2
                $value = $ldapValue;
44
            }
45
46 4
            call_user_func([$user, $attr['user_method']], $value);
47 5
        }
48 5
    }
49
}
50