Test Setup Failed
Push — master ( 871edc...5a5b1b )
by Jonathan
02:39
created

ReportingCloudFactory::getCredentials()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 29
Code Lines 17

Duplication

Lines 8
Ratio 27.59 %

Code Coverage

Tests 17
CRAP Score 5

Importance

Changes 0
Metric Value
dl 8
loc 29
ccs 17
cts 17
cp 1
rs 8.439
c 0
b 0
f 0
cc 5
eloc 17
nc 5
nop 1
crap 5
1
<?php
2
3
namespace TxTextControl\ReportingCloud\Service;
4
5
use Interop\Container\ContainerInterface;
6
use TxTextControl\ReportingCloud\Exception\InvalidArgumentException;
7
use TxTextControl\ReportingCloud\ReportingCloud;
8
use Zend\ServiceManager\Factory\FactoryInterface;
9
10
class ReportingCloudFactory implements FactoryInterface
11
{
12 7
    public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
13
    {
14 7
        $config = $container->get('Config');
15
16 6
        $credentials = $this->getCredentials($config);
17
18 1
        return new ReportingCloud($credentials);
19
    }
20
21 6
    protected function getCredentials($config)
22
    {
23 6
        $help = "Copy '/vendor/textcontrol/txtextcontrol-reportingcloud-zf3-module/config/reportingcloud.local.php.dist' to '/config/autoload/reportingcloud.local.php' in your Zend Framework 3 application, then add your ReportingCloud credentials to that file.";
24
25 6
        if (!array_key_exists('reportingcloud', $config)) {
26 1
            $message = "The key 'reportingcloud' has not been specified in your application's configuration file. {$help}";
27 1
            throw new InvalidArgumentException($message);
28
        }
29
30 5
        if (!array_key_exists('credentials', $config['reportingcloud'])) {
31 1
            $message = "The key 'credentials' has not been specified under the key 'reportingcloud' in your application's configuration file. {$help}";
32 1
            throw new InvalidArgumentException($message);
33
        }
34
35 4 View Code Duplication
        if (!array_key_exists('username', $config['reportingcloud']['credentials'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
36 2
            $message = "The key 'username' has not been specified under the key 'reportingcloud', sub-key 'credentials' in your application's configuration file. {$help}";
37 2
            throw new InvalidArgumentException($message);
38
        }
39
40 2 View Code Duplication
        if (!array_key_exists('password', $config['reportingcloud']['credentials'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
41 1
            $message = "The key 'password' has not been specified under the key 'reportingcloud', sub-key 'credentials' in your application's configuration file. {$help}";
42 1
            throw new InvalidArgumentException($message);
43
        }
44
45
        return [
46 1
            'username' => $config['reportingcloud']['credentials']['username'],
47 1
            'password' => $config['reportingcloud']['credentials']['password'],
48 1
        ];
49
    }
50
}