AuthenticationFactory   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 6
c 2
b 0
f 0
lcom 1
cbo 2
dl 0
loc 43
ccs 14
cts 14
cp 1
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 17
    public function __construct(array $config)
27
    {
28 17
        if (empty($config['marek_toggable']['security'])) {
29 1
            throw new InvalidArgumentException('Please provide security configuration.');
30
        }
31 16
        $this->config = $config;
32 16
    }
33
34
    /**
35
     * @inheritdoc
36
     */
37 16
    public function build()
38
    {
39 16
        if (!empty($this->config['marek_toggable']['security']['token'])) {
40
41 14
            return new ApiToken($this->config['marek_toggable']['security']['token']);
42
43 2
        } else if (!empty($this->config['marek_toggable']['security']['username']) && !empty($this->config['marek_toggable']['security']['password'])) {
44
45 1
            return new UsernameAndPasswordToken(
46 1
                $this->config['marek_toggable']['security']['username'],
47 1
                $this->config['marek_toggable']['security']['password']
48 1
            );
49
50
        } else {
51
52 1
            throw new InvalidArgumentException('Please provide valid security configuration.');
53
54
        }
55
    }
56
}
57