Completed
Pull Request — develop (#107)
by
unknown
05:31
created

ConsentService::findAllFor()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 32
rs 9.408
c 1
b 0
f 0
cc 3
nc 3
nop 1
1
<?php
2
3
/**
4
 * Copyright 2015 SURFnet B.V.
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 *     http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
18
19
namespace OpenConext\ProfileBundle\Service;
20
21
use OpenConext\Profile\Repository\ConsentRepository;
22
use OpenConext\Profile\Value\ConsentList;
23
use OpenConext\Profile\Entity\AuthenticatedUser;
24
use Psr\Log\LoggerInterface;
25
use Surfnet\SamlBundle\SAML2\Attribute\AttributeDefinition;
26
27
final class ConsentService
28
{
29
    /**
30
     * @var LoggerInterface
31
     */
32
    private $logger;
33
34
    /**
35
     * @var ConsentRepository
36
     */
37
    private $consentRepository;
38
39
    /**
40
     * @var AttributeDefinition
41
     */
42
    private $identifyingAttribute;
43
44
    public function __construct(
45
        LoggerInterface $logger,
46
        ConsentRepository $consentRepository,
47
        AttributeDefinition $identifyingAttribute
48
    ) {
49
        $this->logger               = $logger;
50
        $this->consentRepository    = $consentRepository;
51
        $this->identifyingAttribute = $identifyingAttribute;
52
    }
53
54
    /**
55
     * @param AuthenticatedUser $user
56
     * @return ConsentList|null
57
     */
58
    public function findAllFor(AuthenticatedUser $user)
59
    {
60
        if (!$user->getAttributes()->containsAttributeDefinedBy($this->identifyingAttribute)) {
61
            $message = sprintf(
62
                'Cannot get consent list for user: user does not have identifying attribute "%s"',
63
                $this->identifyingAttribute->getName()
64
            );
65
66
            $this->logger->error($message);
67
68
            return null;
69
        }
70
71
        $userIdentifier    = $user->getAttributes()->getAttributeByDefinition($this->identifyingAttribute);
72
        $identifyingValues = $userIdentifier->getValue();
73
        $identifyingValue  = array_shift($identifyingValues);
74
75
        if (!is_string($identifyingValue)) {
76
            $message = sprintf(
77
                'In order to get the consent list for a user, the identifying attribute must have a string value. "%s"'
78
                . 'given for identifying attribute "%s"',
79
                gettype($identifyingValue),
80
                $this->identifyingAttribute->getName()
81
            );
82
83
            $this->logger->error($message);
84
85
            return null;
86
        }
87
88
        return $this->consentRepository->findAllFor($identifyingValue);
89
    }
90
}
91