Completed
Push — master ( a3cce1...d6fbcc )
by Marcel
09:39
created

AttributeResolver   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 124
Duplicated Lines 0 %

Test Coverage

Coverage 6.12%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 43
c 3
b 0
f 0
dl 0
loc 124
ccs 3
cts 49
cp 0.0612
rs 10
wmc 14

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getAttributesForRole() 0 5 1
A getAttributesForRegistrationCode() 0 5 1
A transformValuesToSimpleArray() 0 8 2
A getResultingAttributeValuesForUser() 0 16 3
A getAttributeValuesForUser() 0 9 2
A getDetailedResultingAttributeValuesForUser() 0 31 3
A getAttributesForType() 0 5 1
A __construct() 0 2 1
1
<?php
2
3
namespace App\Service;
4
5
use App\Entity\ServiceAttributeValue;
6
use App\Entity\ServiceAttributeValueInterface;
7
use App\Entity\User;
8
use App\Entity\RegistrationCode;
9
use App\Entity\UserRole;
10
use App\Entity\UserType;
11
use App\Repository\ServiceAttributeValueRepositoryInterface;
12
use App\Traits\ArrayTrait;
13
14
/**
15
 * Helper which computes all attributes for a given user.
16
 */
17
class AttributeResolver {
18
    use ArrayTrait;
19
20
    private $attributeValueRepository;
21
22 1
    public function __construct(ServiceAttributeValueRepositoryInterface $attributeValueRepository) {
23 1
        $this->attributeValueRepository = $attributeValueRepository;
24 1
    }
25
26
    /**
27
     * @param User|null $user
28
     * @return ServiceAttributeValueInterface[] $user
29
     */
30
    public function getDetailedResultingAttributeValuesForUser(User $user = null) {
31
        if($user === null) {
32
            return [ ];
33
        }
34
35
        $values = [ ];
36
37
        $keyFunc = function(ServiceAttributeValueInterface $attributeValue) {
38
            return $attributeValue->getAttribute()->getId();
39
        };
40
41
        // USER TYPE
42
        $typeValues = $this->attributeValueRepository->getAttributeValuesForUserType($user->getType());
43
        $typeValues = $this->makeArrayWithKeys($typeValues, $keyFunc);
44
        $values = array_merge($values, $typeValues);
45
46
        // USER ROLES
47
        /** @var UserRole[] $userRoles */
48
        $userRoles = $user->getUserRoles();
49
        foreach($userRoles as $role) {
50
            $roleValues = $this->attributeValueRepository->getAttributeValuesForUserRole($role);
51
            $roleValues = $this->makeArrayWithKeys($roleValues, $keyFunc);
52
            $values = array_merge($values, $roleValues);
53
        }
54
55
        // USER
56
        $userValues = $this->attributeValueRepository->getAttributeValuesForUser($user);
57
        $userValues = $this->makeArrayWithKeys($userValues, $keyFunc);
58
        $values = array_merge($values, $userValues);
59
60
        return $values;
61
    }
62
63
    /**
64
     * @param User|null $user
65
     * @return mixed[]
66
     */
67
    public function getResultingAttributeValuesForUser(User $user = null) {
68
        if($user === null) {
69
            return [ ];
70
        }
71
72
        $values = $this->getAttributesForType($user->getType());
73
74
        $userRoles = $user->getUserRoles();
75
76
        foreach($userRoles as $role) {
77
            $values = array_merge($values, $this->getAttributesForRole($role));
78
        }
79
80
        $values = array_merge($values, $this->getAttributeValuesForUser($user));
81
82
        return $values;
83
    }
84
85
    /**
86
     * @param User|null $user
87
     * @return mixed[]
88
     */
89
    public function getAttributeValuesForUser(User $user = null) {
90
        if($user === null) {
91
            return [ ];
92
        }
93
94
        /** @var ServiceAttributeValue[] $userAttributeValues */
95
        $userAttributeValues = $this->attributeValueRepository->getAttributeValuesForUser($user);
96
97
        return $this->transformValuesToSimpleArray($userAttributeValues);
98
    }
99
100
    /**
101
     * @param UserType $userType
102
     * @return mixed[]
103
     */
104
    public function getAttributesForType(UserType $userType) {
105
        /** @var ServiceAttributeValue[] $userTypeAttributeValues */
106
        $userTypeAttributeValues = $this->attributeValueRepository->getAttributeValuesForUserType($userType);
107
108
        return $this->transformValuesToSimpleArray($userTypeAttributeValues);
109
    }
110
111
    /**
112
     * @param UserRole $userRole
113
     * @return mixed[]
114
     */
115
    public function getAttributesForRole(UserRole $userRole) {
116
        /** @var ServiceAttributeValue[] $userRoleAttributeValues */
117
        $userRoleAttributeValues = $this->attributeValueRepository->getAttributeValuesForUserRole($userRole);
118
119
        return $this->transformValuesToSimpleArray($userRoleAttributeValues);
120
    }
121
122
    /**
123
     * @param RegistrationCode $code
124
     * @return mixed[]
125
     */
126
    public function getAttributesForRegistrationCode(RegistrationCode $code) {
127
        /** @var ServiceAttributeValue[] $registrationCodeValues */
128
        $registrationCodeValues = $this->attributeValueRepository->getAttributeValuesForRegistrationCode($code);
129
130
        return $this->transformValuesToSimpleArray($registrationCodeValues);
131
    }
132
133
    private function transformValuesToSimpleArray(array $values) {
134
        $attributeValues = [ ];
135
136
        foreach($values as $attribute) {
137
            $attributeValues[$attribute->getAttribute()->getName()] = $attribute->getValue();
138
        }
139
140
        return $attributeValues;
141
    }
142
}