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

ReportingCloudFactory   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 41
Duplicated Lines 19.51 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
lcom 0
cbo 3
dl 8
loc 41
ccs 21
cts 21
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 8 1
B getCredentials() 8 29 5

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
}