ReportingCloudFactory::getCredentials()   C
last analyzed

Complexity

Conditions 12
Paths 9

Size

Total Lines 41
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 22.2031

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 12
eloc 26
c 1
b 0
f 0
nc 9
nop 1
dl 0
loc 41
ccs 17
cts 29
cp 0.5862
crap 22.2031
rs 6.9666

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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