AuthenticationFactory   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
wmc 11
lcom 0
cbo 2
dl 0
loc 35
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B getInstance() 0 27 11
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
}