Passed
Branch backend-2.5 (7bf067)
by Jonathan
15:06 queued 24s
created

ReportingCloud::__construct()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 26
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4

Importance

Changes 0
Metric Value
eloc 14
dl 0
loc 26
ccs 7
cts 7
cp 1
rs 9.7998
c 0
b 0
f 0
cc 4
nc 2
nop 1
crap 4
1
<?php
2
declare(strict_types=1);
3
4
/**
5
 * ReportingCloud PHP SDK
6
 *
7
 * PHP SDK 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://github.com/TextControl/txtextcontrol-reportingcloud-php for the canonical source repository
11
 * @license   https://raw.githubusercontent.com/TextControl/txtextcontrol-reportingcloud-php/master/LICENSE.md
12
 * @copyright © 2019 Text Control GmbH
13
 */
14
15
namespace TxTextControl\ReportingCloud;
16
17
/**
18
 * Class ReportingCloud
19
 *
20
 * @package TxTextControl\ReportingCloud
21
 * @author  Jonathan Maron (@JonathanMaron)
22
 */
23
class ReportingCloud extends AbstractReportingCloud
24
{
25
    use BuildTrait;
26
    use DeleteTrait;
27
    use GetTrait;
28
    use PostTrait;
29
    use PutTrait;
30
31
    // <editor-fold desc="Methods">
32
33
    /**
34
     * ReportingCloud constructor
35
     *
36
     * @param array|null $options
37
     */
38 283
    public function __construct(?array $options = null)
39
    {
40 283
        if (is_array($options)) {
41
42
            $methods = [
43
                // Credentials
44 274
                'api_key'  => 'setApiKey',
45
                // Credentials (deprecated, use 'api_key' only)
46
                'username' => 'setUsername',
47
                'password' => 'setPassword',
48
                // Settings
49
                'base_uri' => 'setBaseUri',
50
                'debug'    => 'setDebug',
51
                'test'     => 'setTest',
52
                'timeout'  => 'setTimeout',
53
                'version'  => 'setVersion',
54
            ];
55
56 274
            foreach ($methods as $key => $method) {
57 274
                if (array_key_exists($key, $options)) {
58 274
                    $this->$method($options[$key]);
59
                }
60
            }
61
        }
62
63 283
        $this->setDefaultSettings();
64 283
    }
65
66
    /**
67
     * Assign default values to settings properties
68
     *
69
     * @return ReportingCloud
70
     */
71 283
    private function setDefaultSettings(): self
72
    {
73 283
        if (null === $this->getBaseUri()) {
74 283
            $baseUri = $this->getBaseUriFromEnv() ?? self::DEFAULT_BASE_URI;
75 283
            $this->setBaseUri($baseUri);
76
        }
77
78 283
        if (null === $this->getDebug()) {
79 283
            $this->setDebug(self::DEFAULT_DEBUG);
80
        }
81
82 283
        if (null === $this->getTest()) {
83 283
            $this->setTest(self::DEFAULT_TEST);
84
        }
85
86 283
        if (null === $this->getTimeout()) {
87 283
            $this->setTimeout(self::DEFAULT_TIMEOUT);
88
        }
89
90 283
        if (null === $this->getVersion()) {
91 283
            $this->setVersion(self::DEFAULT_VERSION);
92
        }
93
94 283
        return $this;
95
    }
96
97
    // </editor-fold>
98
}
99