Scripting::__construct()   A
last analyzed

Complexity

Conditions 6
Paths 32

Size

Total Lines 21
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 11
dl 0
loc 21
rs 9.2222
c 1
b 0
f 0
cc 6
nc 32
nop 1
ccs 8
cts 8
cp 1
crap 6
1
<?php
2
3
namespace PhpWinTools\WmiScripting;
4
5
use PHPUnit\Framework\TestCase;
6
use PhpWinTools\WmiScripting\Testing\FakeFactory;
7
use PhpWinTools\WmiScripting\Configuration\Config;
8
use PhpWinTools\WmiScripting\Connections\ComConnection;
9
use PhpWinTools\WmiScripting\Testing\CallStacks\ComCallStack;
10
use PhpWinTools\WmiScripting\Exceptions\InvalidConnectionException;
11
use PhpWinTools\WmiScripting\Testing\CallStacks\ApiObjectCallStack;
12
use PhpWinTools\WmiScripting\Exceptions\InvalidConfigArgumentException;
13
14
class Scripting
15
{
16
    /** @var Config */
17
    protected $config;
18
19 10
    /**
20
     * @param Config|array|null $config
21 10
     */
22 7
    public function __construct($config = null)
23
    {
24
        if (is_null($config)) {
25 10
            $this->config = Config::instance();
26 1
        }
27
28
        if ($config instanceof Config) {
29 10
            $this->config = $config;
30 1
        }
31
32
        if ($config instanceof ComConnection) {
33 10
            $this->config = Config::instance();
34 1
            $this->setDefaultConnection('default', $config);
35
        }
36 9
37
        if (is_array($config)) {
38
            $this->config = Config::instance($config);
39
        }
40
41
        if (is_null($this->config)) {
42
            throw new InvalidConfigArgumentException('Cannot instantiate Config with given argument(s).');
43
        }
44 2
    }
45
46 2
    /**
47
     * @param TestCase    $testCase
48
     * @param Config|null $config
49
     *
50
     * @return FakeFactory
51
     */
52 2
    public static function fake(TestCase $testCase, Config $config = null): FakeFactory
53
    {
54 2
        ComCallStack::newInstance();
55
        ApiObjectCallStack::newInstance();
56
57
        return new FakeFactory($testCase, $config ?? Config::testInstance());
58
    }
59
60
    /**
61
     * @return Config
62 1
     */
63
    public function getConfig(): Config
64 1
    {
65
        return $this->config;
66
    }
67
68
    /**
69
     * @param ComConnection|string|null $connection
70
     *
71
     * @return WmiQueryFactory
72
     */
73
    public function query($connection = null): WmiQueryFactory
74
    {
75 5
        return new WmiQueryFactory($connection);
76
    }
77 5
78 3
    /**
79
     * @param string                   $name
80 3
     * @param ComConnection|array|null $connection
81
     *
82
     * @throws InvalidConnectionException
83 2
     *
84 1
     * @return self
85 1
     */
86 1
    public function addConnection(string $name, $connection): self
87 1
    {
88 1
        if ($connection instanceof ComConnection) {
89 1
            $this->getConfig()->addConnection($name, $connection);
90 1
91 1
            return $this;
92
        }
93
94 1
        if (is_array($connection)) {
95
            $this->getConfig()->addConnection($name, new ComConnection(
96
                $connection['server'] ?? ComConnection::DEFAULT_SERVER,
97 1
                $connection['namespace'] ?? ComConnection::DEFAULT_NAMESPACE,
98
                $connection['user'] ?? null,
99
                $connection['password'] ?? null,
100
                $connection['locale'] ?? null,
101
                $connection['authority'] ?? null,
102
                $connection['security_flags'] ?? null
103
            ));
104
105
            return $this;
106
        }
107
108 2
        throw new InvalidConnectionException("Could not create connection '{$name}'.");
109
    }
110 2
111 1
    /**
112
     * @param string                   $name
113
     * @param ComConnection|array|null $connection
114 2
     *
115
     * @throws InvalidConnectionException
116 2
     *
117
     * @return self
118
     */
119
    public function setDefaultConnection(string $name, $connection = null): self
120
    {
121
        if ($connection) {
122
            $this->addConnection($name, $connection);
123
        }
124
125
        $this->getConfig()->setDefaultConnection($name);
126
127
        return $this;
128
    }
129
130
    /**
131
     * @param string|null $name
132
     *
133
     * @return ComConnection|null
134
     */
135
    public function getConnection(string $name = null)
136
    {
137
        return $this->getConfig()->getConnection($name);
138
    }
139
140
    /**
141
     * @return ComConnection|null
142
     */
143
    public function getDefaultConnection()
144
    {
145
        return $this->getConnection();
146
    }
147
}
148