Failed Conditions
Push — ng ( 76f41c...c3e4f5 )
by Florent
14:09
created

UserAccountDiscoveryManager::find()   C

Complexity

Conditions 7
Paths 11

Size

Total Lines 28
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 28
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 18
nc 11
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * The MIT License (MIT)
7
 *
8
 * Copyright (c) 2014-2018 Spomky-Labs
9
 *
10
 * This software may be modified and distributed under the terms
11
 * of the MIT license.  See the LICENSE file for details.
12
 */
13
14
namespace OAuth2Framework\Component\AuthorizationEndpoint\UserAccountDiscovery;
15
16
use OAuth2Framework\Component\AuthorizationEndpoint\Authorization;
17
use OAuth2Framework\Component\AuthorizationEndpoint\Exception\RedirectToLoginPageException;
18
19
class UserAccountDiscoveryManager
20
{
21
    /**
22
     * @var UserAccountDiscovery[]
23
     */
24
    private $extensions = [];
25
26
    /**
27
     * @param UserAccountDiscovery $extension
28
     */
29
    public function add(UserAccountDiscovery $extension)
30
    {
31
        $this->extensions[] = $extension;
32
    }
33
34
    /**
35
     * @param Authorization $authorization
36
     *
37
     * @return Authorization
38
     *
39
     * @throws RedirectToLoginPageException
40
     */
41
    public function find(Authorization $authorization): Authorization
42
    {
43
        $userAccount = null;
44
        $isFullyAuthenticated = null;
45
        foreach ($this->extensions as $extension) {
46
            $tmpIsFullyAuthenticated = null;
47
            $tmpUserAccount = $extension->find($authorization, $tmpIsFullyAuthenticated);
48
            if (null !== $tmpUserAccount) {
49
                if (null === $userAccount) {
50
                    $userAccount = $tmpUserAccount;
51
                    $isFullyAuthenticated = $tmpIsFullyAuthenticated;
52
                } else {
53
                    if ($tmpUserAccount->getPublicId()->getValue() !== $userAccount->getPublicId()->getValue()) {
54
                        throw new RedirectToLoginPageException($authorization);
55
                    }
56
                    if (true === $tmpIsFullyAuthenticated) {
57
                        $isFullyAuthenticated = $tmpIsFullyAuthenticated;
58
                    }
59
                }
60
            }
61
        }
62
63
        if (null !== $userAccount) {
64
            $authorization = $authorization->withUserAccount($userAccount, $isFullyAuthenticated);
0 ignored issues
show
Bug introduced by
It seems like $isFullyAuthenticated can also be of type null; however, OAuth2Framework\Componen...tion::withUserAccount() does only seem to accept boolean, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
65
        }
66
67
        return $authorization;
68
    }
69
70
    /**
71
     * @param Authorization $authorization
72
     */
73
    public function check(Authorization $authorization)
74
    {
75
        foreach ($this->extensions as $extension) {
76
            $authorization = $extension->check($authorization);
77
        }
78
    }
79
}
80