AuthenticationFactory::build()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 19
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 19
ccs 9
cts 9
cp 1
rs 9.2
cc 4
eloc 9
nc 3
nop 0
crap 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