Completed
Pull Request — develop (#22)
by A.
05:52
created

ConsentListingService::getConsentListing()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 20
rs 9.4286
cc 3
eloc 11
nc 3
nop 0
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\Value\Consent;
22
use OpenConext\Profile\Value\ConsentList;
23
use OpenConext\ProfileBundle\Exception\RuntimeException;
24
use OpenConext\ProfileBundle\Security\Authentication\Entity\User;
25
use OpenConext\ProfileBundle\User\UserProvider;
26
use Surfnet\SamlBundle\SAML2\Attribute\Filter\AttributeFilter;
27
28
class ConsentListingService
29
{
30
    /**
31
     * @var UserProvider
32
     */
33
    private $userProvider;
34
35
    /**
36
     * @var ConsentService
37
     */
38
    private $consentService;
39
40
    /**
41
     * @var AttributeFilter
42
     */
43
    private $attributeFilter;
44
45
    /**
46
     * @param UserProvider $userProvider
47
     * @param ConsentService $consentService
48
     * @param AttributeFilter $attributeFilter
49
     */
50
    public function __construct(
51
        UserProvider $userProvider,
52
        ConsentService $consentService,
53
        AttributeFilter $attributeFilter
54
    ) {
55
        $this->userProvider    = $userProvider;
56
        $this->consentService  = $consentService;
57
        $this->attributeFilter = $attributeFilter;
58
    }
59
60
    /**
61
     * @return ConsentList
62
     */
63
    public function getConsentListing()
64
    {
65
        if (!$this->userProvider->hasCurrentUser()) {
66
            throw new RuntimeException('Cannot get consent listing: no current user available');
67
        }
68
69
        $user               = $this->userProvider->getCurrentUser();
70
        $consentList        = $this->consentService->findAllFor($user);
71
        $filteredAttributes = $user->getAttributes()->apply($this->attributeFilter);
72
73
        $newConsents = [];
74
75
        /** @var Consent $consent */
76
        foreach ($consentList as $consent) {
0 ignored issues
show
Bug introduced by
The expression $consentList of type null|object<OpenConext\Profile\Value\ConsentList> is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
77
            $newConsent = $consent->givenFor($filteredAttributes);
78
            $newConsents[] = $newConsent;
79
        }
80
81
        return new ConsentList($newConsents);
82
    }
83
}
84