Completed
Push — master ( c59301...f49e6f )
by Joe
02:01
created

Scripting::addConnection()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 23
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

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