ReportingCloudFactory   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Test Coverage

Coverage 67.5%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 36
c 1
b 0
f 0
dl 0
loc 77
ccs 27
cts 40
cp 0.675
rs 10
wmc 14

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 8 1
A getHelp() 0 9 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://tinyurl.com/4mefj7xz for the canonical source repository
11
 * @license   https://tinyurl.com/4xh3utpp
12
 * @copyright © 2023 Text Control GmbH
13
 */
14
15
namespace TextControl\Laminas\ReportingCloud\Service;
16
17
use Laminas\ServiceManager\Factory\FactoryInterface;
18
use Psr\Container\ContainerInterface;
19
use TextControl\ReportingCloud\Exception\InvalidArgumentException;
20
use TextControl\ReportingCloud\ReportingCloud;
21
22
class ReportingCloudFactory implements FactoryInterface
23
{
24
    /**
25
     * @param string             $requestedName
26
     */
27 12
    public function __invoke(ContainerInterface $container, $requestedName, ?array $options = null): ReportingCloud
28
    {
29 12
        $config = $container->get('Config');
30 10
        assert(is_array($config));
31
32 10
        $credentials = $this->getCredentials($config);
33
34
        return new ReportingCloud($credentials);
35
    }
36
37
    /**
38
     * Return required credentials to use the Reporting Cloud Web API
39
     *
40
     * @param array<string, array> $config
41
     *
42
     * @return array<string, string>
43
     */
44 10
    protected function getCredentials(array $config): array
45
    {
46 10
        if (!array_key_exists('reportingcloud', $config)) {
47 2
            $message = "The key 'reportingcloud' has not been specified in your application's configuration file. ";
48 2
            $message .= $this->getHelp();
49 2
            throw new InvalidArgumentException($message);
50
        }
51
52 8
        if (!array_key_exists('credentials', $config['reportingcloud'])) {
53 2
            $message = "The key 'credentials' has not been specified under the key 'reportingcloud' ";
54 2
            $message .= "in your application's configuration file. ";
55 2
            $message .= $this->getHelp();
56 2
            throw new InvalidArgumentException($message);
57
        }
58
59 6
        $credentials = $config['reportingcloud']['credentials'];
60
61 6
        if (array_key_exists('api_key', $credentials)) {
62
            $apiKey = $credentials['api_key'];
63
            if (is_string($apiKey) && 0 < strlen($apiKey)) {
64
                return [
65
                    'api_key' => $apiKey,
66
                ];
67
            }
68
        }
69
70 6
        if (array_key_exists('username', $credentials) && array_key_exists('password', $credentials)) {
71
            $username = $credentials['username'];
72
            $password = $credentials['password'];
73
            if (is_string($username) && 0 < strlen($username) && is_string($password) && 0 < strlen($password)) {
74
                return [
75
                    'username' => $username,
76
                    'password' => $password,
77
                ];
78
            }
79
        }
80
81 6
        $message = "Either the key 'api_key', or the keys 'username' and 'password' have not been specified under ";
82 6
        $message .= "the key 'reportingcloud', sub-key 'credentials' in your application's configuration file. ";
83 6
        $message .= $this->getHelp();
84 6
        throw new InvalidArgumentException($message);
85
    }
86
87
    /**
88
     * Return help text, used as assistance in exception message
89
     */
90 10
    protected function getHelp(): string
91
    {
92 10
        $path   = '/vendor/textcontrol/textcontrol-reportingcloud-laminas-module';
93 10
        $source = sprintf('%s/config/reportingcloud.local.php.dist', $path);
94 10
        $dest   = '/config/autoload/reportingcloud.local.php';
95
96 10
        $ret = sprintf("Copy '%s' to '%s' in your Laminas application, ", $source, $dest);
97
98 10
        return $ret . 'then add your ReportingCloud credentials to that file.';
99
    }
100
}
101