Manager   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 1
dl 0
loc 45
ccs 21
cts 21
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A __call() 0 13 3
A addQuery() 0 8 2
A fromObjectToNameCallableList() 0 10 4
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