Passed
Push — master ( 8a23ee...729047 )
by Jonathan
02:03
created

ReportingCloudFactory::__invoke()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 3
crap 1
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 18
    public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
13
    {
14 18
        $config = $container->get('Config');
15
16 16
        $credentials = $this->getCredentials($config);
17
18 6
        return new ReportingCloud($credentials);
19
    }
20
21 16
    protected function getCredentials($config)
22
    {
23 16
        $ret = null;
24
25 16
        $source = '/vendor/textcontrol/txtextcontrol-reportingcloud-zf3-module/config/reportingcloud.local.php.dist';
26 16
        $dest   = '/config/autoload/reportingcloud.local.php';
27
28 16
        $help = "Copy '{$source}' to '{$dest}' in your Zend Framework 3 application, ";
29 16
        $help .= "then add your ReportingCloud credentials to that file.";
30
31 16
        if (!array_key_exists('reportingcloud', $config)) {
32 2
            $message = "The key 'reportingcloud' has not been specified in your application's configuration file. ";
33 2
            $message .= $help;
34 2
            throw new InvalidArgumentException($message);
35
        }
36
37 14
        if (!array_key_exists('credentials', $config['reportingcloud'])) {
38 2
            $message = "The key 'credentials' has not been specified under the key 'reportingcloud' ";
39 2
            $message .= "in your application's configuration file. ";
40 2
            $message .= $help;
41 2
            throw new InvalidArgumentException($message);
42
        }
43
44 12
        $c = $config['reportingcloud']['credentials'];
45
46 12
        if (isset($c['api_key']) && !empty($c['api_key'])) {
47
            $ret = [
48 2
                'api_key' => $c['api_key'],
49 1
            ];
50 1
        }
51
52 12
        if (isset($c['username']) && !empty($c['username']) && isset($c['password']) && !empty($c['password'])) {
53
            $ret = [
54 4
                'username' => $c['username'],
55 4
                'password' => $c['password'],
56 2
            ];
57 2
        }
58
59 12
        if (null === $ret) {
60 6
            $message = "Neither the key 'api_key', nor the keys 'username' and 'password' have been specified under ";
61 6
            $message .= "the key 'reportingcloud', sub-key 'credentials' in your application's configuration file. ";
62 6
            $message .= $help;
63 6
            throw new InvalidArgumentException($message);
64
        }
65
66 6
        return $ret;
67
    }
68
}
69