Completed
Push — master ( 2e002a...ff3a53 )
by Mario
04:03
created

AuthenticationFactory   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
c 1
b 0
f 0
lcom 0
cbo 2
dl 0
loc 43
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 2
A build() 0 19 4
1
<?php
2
3
namespace Marek\Toggable\Factory\Authentication;
4
5
use Marek\Toggable\API\Security\ApiToken;
6
use Marek\Toggable\API\Security\UsernameAndPasswordToken;
7
use Marek\Toggable\Factory\FactoryInterface;
8
use InvalidArgumentException;
9
10
/**
11
 * Class AuthenticationFactory
12
 * @package Marek\Toggable\Factory\Authentication
13
 */
14
class AuthenticationFactory implements FactoryInterface
15
{
16
    /**
17
     * @var array
18
     */
19
    private $config;
20
21
    /**
22
     * AuthenticationFactory constructor.
23
     *
24
     * @param array $config
25
     */
26
    public function __construct(array $config)
27
    {
28
        if (empty($config['marek_toggable']['security'])) {
29
            throw new InvalidArgumentException('Please provide security configuration.');
30
        }
31
        $this->config = $config;
32
    }
33
34
    /**
35
     * @inheritdoc
36
     */
37
    public function build()
38
    {
39
        if (!empty($config['marek_toggable']['security']['token'])) {
40
41
            return new ApiToken($config['marek_toggable']['security']['token']);
42
43
        } else if (!empty($config['marek_toggable']['security']['username']) && !empty($config['marek_toggable']['security']['password'])) {
0 ignored issues
show
Bug introduced by
The variable $config seems to never exist, and therefore empty should always return true. Did you maybe rename this variable?

This check looks for calls to isset(...) or empty() on variables that are yet undefined. These calls will always produce the same result and can be removed.

This is most likely caused by the renaming of a variable or the removal of a function/method parameter.

Loading history...
44
45
            return new UsernameAndPasswordToken(
46
                $config['marek_toggable']['security']['username'],
47
                $config['marek_toggable']['security']['password']
48
            );
49
50
        } else {
51
52
            throw new InvalidArgumentException('Please provide valid security configuration.');
53
54
        }
55
    }
56
}
57