Completed
Push — master ( b37fc9...4dbb52 )
by Christopher
03:45
created

Manager   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 2 Features 2
Metric Value
c 2
b 2
f 2
dl 0
loc 78
wmc 6
lcom 1
cbo 2
ccs 0
cts 35
cp 0
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A __call() 0 9 1
A first() 0 12 2
A get() 0 12 1
A run() 0 4 1
1
<?php
2
3
namespace AsyncPHP\Icicle\Database;
4
5
use Icicle\Coroutine;
6
use Icicle\Promise\PromiseInterface;
7
8
final class Manager
9
{
10
    /**
11
     * @var Connector
12
     */
13
    private $connector;
14
15
    /**
16
     * @var Builder
17
     */
18
    private $builder;
19
20
    /**
21
     * @param Connector $connector
22
     * @param Builder $builder
23
     */
24
    public function __construct(Connector $connector, Builder $builder)
25
    {
26
        $this->connector = $connector;
27
        $this->builder = $builder;
28
    }
29
30
    /**
31
     * @param string $method
32
     * @param array $parameters
33
     *
34
     * @return mixed
35
     */
36
    public function __call($method, array $parameters = [])
37
    {
38
        $builder = call_user_func_array([$this->builder, $method], $parameters);
39
40
        $clone = clone $this;
41
        $clone->builder = $builder;
42
43
        return $clone;
44
    }
45
46
    /**
47
     * @return PromiseInterface
48
     */
49
    public function first()
50
    {
51
        return Coroutine\create(function () {
52
            $rows = (yield $this->limit(1)->get());
53
54
            if (count($rows) > 0) {
55
                yield $rows[0];
56
            } else {
57
                yield [];
58
            }
59
        });
60
    }
61
62
    /**
63
     * @return PromiseInterface
64
     */
65
    public function get()
66
    {
67
        return Coroutine\create(function () {
68
            list($statement, $values) = $this->builder->build();
69
70
            $query = preg_replace_callback("/\\:([_0-9a-zA-Z]+)/", function($matches) use ($values) {
71
                return "'" . $this->connector->escape($values[$matches[1]]) . "'";
72
            }, $statement);
73
74
            yield $this->connector->query($query);
75
        });
76
    }
77
78
    /**
79
     * @return PromiseInterface
80
     */
81
    public function run()
82
    {
83
        return $this->get();
84
    }
85
}
86