Manager::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php
2
namespace InfluxDB;
3
4
use InvalidArgumentException;
5
use RuntimeException;
6
use InfluxDB\Client;
7
8
class Manager
9
{
10
    private $client;
11
    private $queries;
12
13 19
    public function __construct(Client $client)
14
    {
15 19
        $this->client = $client;
16 19
        $this->queries = [];
17 19
    }
18
19 17
    public function addQuery($name, callable $query = null)
20
    {
21 17
        if ($query === null) {
22 15
            list($name, $query) = $this->fromObjectToNameCallableList($name);
23
        }
24
25 15
        $this->queries[$name] = $query;
26 15
    }
27
28 15
    private function fromObjectToNameCallableList($name)
29
    {
30 15
        if (is_object($name) && is_callable($name)) {
31 14
            if (method_exists($name, "__toString")) {
32 13
                return [(string)$name, $name];
33
            }
34
        }
35
36 2
        throw new InvalidArgumentException("Your command should implements '__toString' method and should be a callable thing");
37
    }
38
39 17
    public function __call($name, $args)
40
    {
41 17
        if (method_exists($this->client, $name)) {
42 7
            return call_user_func_array([$this->client, $name], $args);
43
        }
44
45 16
        if (array_key_exists($name, $this->queries)) {
46 15
            $query = call_user_func_array($this->queries[$name], $args);
47 15
            return $this->client->query($query);
48
        }
49
50 1
        throw new RuntimeException("The method you are using is not allowed: '{$name}', do you have to add it with 'addQuery'");
51
    }
52
}
53