GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

AuthenticationProcessFactory::createProcess()   B
last analyzed

Complexity

Conditions 11
Paths 32

Size

Total Lines 50
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 11
eloc 32
nc 32
nop 4
dl 0
loc 50
rs 7.3166
c 1
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace LM\AuthAbstractor\Factory;
6
7
use LM\AuthAbstractor\Configuration\IApplicationConfiguration;
8
use LM\AuthAbstractor\Enum\AuthenticationProcess\Status;
9
use LM\AuthAbstractor\Model\AuthenticationProcess;
10
use LM\AuthAbstractor\Model\IAuthenticationProcess;
11
use LM\AuthAbstractor\Model\IU2fRegistration;
12
use LM\AuthAbstractor\Model\PersistOperation;
13
use LM\Common\DataStructure\TypedMap;
14
use LM\Common\Enum\Scalar;
15
use LM\Common\Model\IntegerObject;
16
use LM\Common\Model\ArrayObject;
17
use LM\Common\Model\StringObject;
18
use InvalidArgumentException;
19
20
/**
21
 * This is a factory class aiming at making new authentication processes
22
 * easier for the application.
23
 *
24
 * It takes care of choosing an implementation of IAuthenticationProcess, and
25
 * sets up its internals.
26
 *
27
 * @see \LM\AuthAbstractor\Model\IAuthenticationProcess
28
 * @see \LM\AuthAbstractor\Model\AuthenticationProcess
29
 */
30
class AuthenticationProcessFactory
31
{
32
    /** @var IApplicationConfiguration */
33
    private $appConfig;
34
35
    /**
36
     * @internal
37
     */
38
    public function __construct(IApplicationConfiguration $appConfig)
39
    {
40
        $this->appConfig = $appConfig;
41
    }
42
43
    /**
44
     * This method can be used to instantiate new authentication processes.
45
     *
46
     * @api
47
     * @param string[] $challenges An array of FQCNs of challenges
48
     * (implementations of IChallenge).
49
     * @param int $maxNFailedAttempts The maximum number of attempts users
50
     * can try in a row before the authentication process fails.
51
     * @param null|string $username The username of the user, null if the user
52
     * is not logged in yet.
53
     * @param mixed[] $additionalData Additional data that can is
54
     * application-specific and can be retrieved later by the callback. This
55
     * data shouldn't be used by any challenge.
56
     * @return AuthenticationProcess A new authentication process.
57
     * @see \LM\AuthAbstractor\Challenge\IChallenge
58
     * @todo Put $additionalData in a separate scope.
59
     * @todo Check $additionalData's format is correct.
60
     */
61
    public function createProcess(
62
        array $challenges,
63
        int $maxNFailedAttempts = 3,
64
        ?string $username = null,
65
        array $additionalData = []
66
    ): IAuthenticationProcess {
67
        $dataArray = array_merge($additionalData, [
68
            'used_u2f_key_public_keys' => new ArrayObject([], Scalar::_STR),
0 ignored issues
show
Deprecated Code introduced by
The class LM\Common\Model\ArrayObject has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

68
            'used_u2f_key_public_keys' => /** @scrutinizer ignore-deprecated */ new ArrayObject([], Scalar::_STR),
Loading history...
69
            'challenges' => new ArrayObject($challenges, Scalar::_STR),
0 ignored issues
show
Deprecated Code introduced by
The class LM\Common\Model\ArrayObject has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

69
            'challenges' => /** @scrutinizer ignore-deprecated */ new ArrayObject($challenges, Scalar::_STR),
Loading history...
70
            'max_n_failed_attempts' => new IntegerObject($maxNFailedAttempts),
71
            'n_failed_attempts' => new IntegerObject(0),
72
            'persist_operations' => new ArrayObject([], PersistOperation::class),
0 ignored issues
show
Deprecated Code introduced by
The class LM\Common\Model\ArrayObject has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

72
            'persist_operations' => /** @scrutinizer ignore-deprecated */ new ArrayObject([], PersistOperation::class),
Loading history...
73
            'status' => new Status(Status::ONGOING),
74
            'new_u2f_registrations' => new ArrayObject([], IU2fRegistration::class),
0 ignored issues
show
Deprecated Code introduced by
The class LM\Common\Model\ArrayObject has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

74
            'new_u2f_registrations' => /** @scrutinizer ignore-deprecated */ new ArrayObject([], IU2fRegistration::class),
Loading history...
75
            'n_u2f_registrations' => new IntegerObject(0),
76
        ]);
77
        if (null !== $username) {
78
            $dataArray['username'] = new StringObject($username);
79
        }
80
81
        if (
82
            !isset($additionalData['u2f_registrations']) &&
83
            null !== $username
84
        ) {
85
            $dataArray['u2f_registrations'] = $this->appConfig->getU2fRegistrations($username);
86
        } elseif (
87
            !isset($additionalData['u2f_registrations']) &&
88
            null === $username
89
        ) {
90
            $dataArray['u2f_registrations'] = [];
91
        } elseif (isset($additionalData['u2f_registrations'])) {
92
            $dataArray['u2f_registrations'] = $additionalData['u2f_registrations'];
93
        }
94
95
        if (isset($additionalData['used_u2f_key_public_keys'])) {
96
            if (!is_array($additionalData['used_u2f_key_public_keys'])) {
97
                throw new InvalidArgumentException('used_u2f_key_public_keys');
98
            }
99
            foreach ($additionalData['used_u2f_key_public_keys'] as $pb) {
100
                if (!is_string($pb)) {
101
                    throw new InvalidArgumentException('Public key must be string');
102
                }
103
            }
104
            $data['used_u2f_key_public_keys'] = $additionalData['used_u2f_key_public_keys'];
0 ignored issues
show
Comprehensibility Best Practice introduced by
$data was never initialized. Although not strictly required by PHP, it is generally a good practice to add $data = array(); before regardless.
Loading history...
105
        } else {
106
            $data['used_u2f_key_public_keys'] = [];
107
        }
108
        $typedMap = new TypedMap($dataArray);
109
110
        return new AuthenticationProcess($typedMap);
111
    }
112
}
113