ReportingCloudFactory   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 1 Features 0
Metric Value
eloc 37
dl 0
loc 86
ccs 36
cts 36
cp 1
rs 10
c 2
b 1
f 0
wmc 14

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getHelp() 0 10 1
A __invoke() 0 8 1
C getCredentials() 0 41 12
1
<?php
2
declare(strict_types=1);
3
4
/**
5
 * ReportingCloud Laminas Module
6
 *
7
 * Laminas Module for ReportingCloud Web API. Authored and supported by Text Control GmbH.
8
 *
9
 * @link      https://www.reporting.cloud to learn more about ReportingCloud
10
 * @link      https://git.io/JexF4 for the canonical source repository
11
 * @license   https://git.io/JexFB
12
 * @copyright © 2022 Text Control GmbH
13
 */
14
15
namespace TxTextControl\ReportingCloud\Service;
16
17
use Laminas\ServiceManager\Factory\FactoryInterface;
18
use Psr\Container\ContainerExceptionInterface;
19
use Psr\Container\ContainerInterface;
20
use Psr\Container\NotFoundExceptionInterface;
21
use TxTextControl\ReportingCloud\Exception\InvalidArgumentException;
22
use TxTextControl\ReportingCloud\ReportingCloud;
23
24
/**
25
 * Class ReportingCloudFactory
26
 *
27
 * @package TxTextControl\ReportingCloud
28
 * @author  Jonathan Maron (@JonathanMaron)
29
 */
30
class ReportingCloudFactory implements FactoryInterface
31
{
32
    /**
33
     * @param ContainerInterface $container
34
     * @param string             $requestedName
35
     * @param array|null         $options
36
     *
37
     * @return ReportingCloud
38
     * @throws ContainerExceptionInterface
39
     * @throws NotFoundExceptionInterface
40
     */
41 18
    public function __invoke(ContainerInterface $container, $requestedName, ?array $options = null): ReportingCloud
42
    {
43 18
        $config = $container->get('Config');
44 16
        assert(is_array($config));
45
46 16
        $credentials = $this->getCredentials($config);
47
48 6
        return new ReportingCloud($credentials);
49
    }
50
51
    /**
52
     * Return required credentials to use the Reporting Cloud Web API
53
     *
54
     * @param array<string, array> $config
55
     *
56
     * @return array<string, string>
57
     */
58 16
    protected function getCredentials(array $config): array
59
    {
60 16
        if (!array_key_exists('reportingcloud', $config)) {
61 2
            $message = "The key 'reportingcloud' has not been specified in your application's configuration file. ";
62 2
            $message .= $this->getHelp();
63 2
            throw new InvalidArgumentException($message);
64
        }
65
66 14
        if (!array_key_exists('credentials', $config['reportingcloud'])) {
67 2
            $message = "The key 'credentials' has not been specified under the key 'reportingcloud' ";
68 2
            $message .= "in your application's configuration file. ";
69 2
            $message .= $this->getHelp();
70 2
            throw new InvalidArgumentException($message);
71
        }
72
73 12
        $credentials = $config['reportingcloud']['credentials'];
74
75 12
        if (array_key_exists('api_key', $credentials)) {
76 2
            $apiKey = $credentials['api_key'];
77 2
            if (is_string($apiKey) && strlen($apiKey) > 0) {
78
                return [
79 2
                    'api_key' => $apiKey,
80
                ];
81
            }
82
        }
83
84 10
        if (array_key_exists('username', $credentials) && array_key_exists('password', $credentials)) {
85 4
            $username = $credentials['username'];
86 4
            $password = $credentials['password'];
87 4
            if (is_string($username) && strlen($username) > 0 && is_string($password) && strlen($password) > 0) {
88
                return [
89 4
                    'username' => $username,
90
                    'password' => $password,
91
                ];
92
            }
93
        }
94
95 6
        $message = "Either the key 'api_key', or the keys 'username' and 'password' have not been specified under ";
96 6
        $message .= "the key 'reportingcloud', sub-key 'credentials' in your application's configuration file. ";
97 6
        $message .= $this->getHelp();
98 6
        throw new InvalidArgumentException($message);
99
    }
100
101
    /**
102
     * Return help text, used as assistance in exception message
103
     *
104
     * @return string
105
     */
106 10
    protected function getHelp(): string
107
    {
108 10
        $path   = '/vendor/textcontrol/txtextcontrol-reportingcloud-laminas-module';
109 10
        $source = "{$path}/config/reportingcloud.local.php.dist";
110 10
        $dest   = '/config/autoload/reportingcloud.local.php';
111
112 10
        $ret = "Copy '{$source}' to '{$dest}' in your Laminas application, ";
113 10
        $ret .= "then add your ReportingCloud credentials to that file.";
114
115 10
        return $ret;
116
    }
117
}
118