AuthenticationFactory::getInstance()   B
last analyzed

Complexity

Conditions 11
Paths 4

Size

Total Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 27
rs 7.3166
c 0
b 0
f 0
cc 11
nc 4
nop 2

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
namespace Integracao\ControlPay\Factory;
4
5
use Integracao\ControlPay\Client;
6
use Integracao\ControlPay\Constants\ControlPayParameterConst;
7
use Integracao\ControlPay\Impl\BasicAuthentication;
8
use Integracao\ControlPay\Impl\KeyQueryStringAuthentication;
9
use Integracao\ControlPay\Interfaces\IAuthentication;
10
11
/**
12
 * Class AuthenticationFactory
13
 * @package Integracao\NTKOnline
14
 */
15
class AuthenticationFactory
16
{
17
    /**
18
     * @param array $params
19
     * @return IAuthentication
20
     * @throws \Exception
21
     */
22
    public static function getInstance(array $params, Client $client = null)
23
    {
24
        if(!isset($params[ControlPayParameterConst::CONTROLPAY_OAUTH_TYPE]))
25
            throw new \Exception("Tipo de autenticação não especificado");
26
27
        switch ($params[ControlPayParameterConst::CONTROLPAY_OAUTH_TYPE])
28
        {
29
            case BasicAuthentication::class:
30
                return new BasicAuthentication(
31
                    isset($params[ControlPayParameterConst::CONTROLPAY_USER]) ?$params[ControlPayParameterConst::CONTROLPAY_USER] : null,
32
                    isset($params[ControlPayParameterConst::CONTROLPAY_PWD]) ?$params[ControlPayParameterConst::CONTROLPAY_PWD] : null,
33
                    isset($params[ControlPayParameterConst::CONTROLPAY_KEY]) ?$params[ControlPayParameterConst::CONTROLPAY_KEY] : null
34
                );
35
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
36
            case KeyQueryStringAuthentication::class:
37
                return new KeyQueryStringAuthentication(
38
                    isset($params[ControlPayParameterConst::CONTROLPAY_USER]) ?$params[ControlPayParameterConst::CONTROLPAY_USER] : null,
39
                    isset($params[ControlPayParameterConst::CONTROLPAY_PWD]) ?$params[ControlPayParameterConst::CONTROLPAY_PWD] : null,
40
                    isset($params[ControlPayParameterConst::CONTROLPAY_KEY]) ?$params[ControlPayParameterConst::CONTROLPAY_KEY] : null,
41
                    isset($params[ControlPayParameterConst::CONTROLPAY_DEFAULT_PESSOA_ID]) ?$params[ControlPayParameterConst::CONTROLPAY_DEFAULT_PESSOA_ID] : null,
42
                    $client
0 ignored issues
show
Bug introduced by
It seems like $client defined by parameter $client on line 22 can be null; however, Integracao\ControlPay\Im...tication::__construct() does not accept null, maybe add an additional type check?

It seems like you allow that null is being passed for a parameter, however the function which is called does not seem to accept null.

We recommend to add an additional type check (or disallow null for the parameter):

function notNullable(stdClass $x) { }

// Unsafe
function withoutCheck(stdClass $x = null) {
    notNullable($x);
}

// Safe - Alternative 1: Adding Additional Type-Check
function withCheck(stdClass $x = null) {
    if ($x instanceof stdClass) {
        notNullable($x);
    }
}

// Safe - Alternative 2: Changing Parameter
function withNonNullableParam(stdClass $x) {
    notNullable($x);
}
Loading history...
43
                );
44
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
45
        }
46
47
        throw new \Exception("Implementação não tratada no factory");
48
    }
49
}