Completed
Push — master ( d819e3...4fe283 )
by Christopher
03:01 queued 12s
created

Manager::__construct()   A

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 1
Bugs 1 Features 1
Metric Value
c 1
b 1
f 1
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 9.4286
cc 1
eloc 3
nc 1
nop 2
crap 1
1
<?php
2
3
namespace AsyncPHP\Icicle\Database;
4
5
use AsyncPHP\Icicle\Database\Method\CloneWithMethod;
6
use Icicle\Coroutine;
7
use Icicle\Promise\PromiseInterface;
8
9
final class Manager
10
{
11
    use CloneWithMethod;
12
13
    /**
14
     * @var Connector
15
     */
16
    private $connector;
17
18
    /**
19
     * @var Builder
20
     */
21
    private $builder;
22
23
    /**
24
     * @var array
25
     */
26
    private $operations = [];
27
28
    /**
29
     * @param Connector $connector
30
     * @param Builder $builder
31
     */
32 1
    public function __construct(Connector $connector, Builder $builder)
33
    {
34 1
        $this->connector = $connector;
35 1
        $this->builder = $builder;
36 1
    }
37
38
    /**
39
     * @param string $method
40
     * @param array $parameters
41
     *
42
     * @return mixed
43
     */
44 1
    public function __call($method, array $parameters = [])
45
    {
46 1
        $operations = $this->operations;
47 1
        $operations[] = [$method, $parameters];
48
49 1
        return $this->cloneWith("operations", $operations);
50
    }
51
52
    /**
53
     * @param string $table
54
     *
55
     * @return static
56
     */
57 1
    public function table($table)
58
    {
59 1
        return $this->cloneWith("builder", $this->builder->table($table));
60
    }
61
62
    /**
63
     * @param string $columns
64
     *
65
     * @return static
66
     */
67 1
    public function select($columns = "*")
68
    {
69 1
        return $this->cloneWith("builder", $this->builder->select($columns));
70
    }
71
72
    /**
73
     * @return PromiseInterface
74
     */
75 1
    public function first()
76
    {
77
        return Coroutine\create(function () {
78 1
            $rows = (yield $this->limit(1)->get());
79 1
            yield reset($rows);
80 1
        });
81 1
    }
82
83
    /**
84
     * @return PromiseInterface
85
     */
86 1
    public function get()
87
    {
88
        return Coroutine\create(function () {
89 1
            $builder = $this->applyOperationsTo($this->builder);
90
91 1
            yield $this->connector->query(
92 1
                $this->interpolate($builder->build())
93 1
            );
94 1
        });
95
    }
96
97
    /**
98
     * @param array $build
99
     *
100
     * @return string
101
     */
102 1
    private function interpolate(array $build)
103
    {
104 1
        list($statement, $values) = $build;
105
106 1
        $pattern = "/\\:([_0-9a-zA-Z]+)/";
107
108
        $replacement = function ($matches) use ($values) {
109 1
            return $this->quote($values[$matches[1]]);
110 1
        };
111
112 1
        return preg_replace_callback($pattern, $replacement, $statement);
113
    }
114
115
    /**
116
     * @param mixed $value
117
     *
118
     * @return string
119
     */
120 1
    private function quote($value)
121
    {
122 1
        return "'" . $this->connector->escape($value) . "'";
123
    }
124
125
    /**
126
     * @param array $data
127
     *
128
     * @return PromiseInterface
129
     */
130 1 View Code Duplication
    public function insert(array $data)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
131
    {
132
        return Coroutine\create(function () use ($data) {
133 1
            $builder = $this->builder->insert($data);
134 1
            $builder = $this->applyOperationsTo($builder);
135
136 1
            yield $this->connector->query(
137 1
                $this->interpolate($builder->build())
138 1
            );
139 1
        });
140
    }
141
142
    /**
143
     * @param Builder $builder
144
     *
145
     * @return Builder
146
     */
147 1
    private function applyOperationsTo($builder)
148
    {
149 1
        foreach ($this->operations as $operation) {
150 1
            list($method, $parameters) = $operation;
151 1
            $builder = call_user_func_array([$builder, $method], $parameters);
152 1
        }
153
154 1
        return $builder;
155
    }
156
157
    /**
158
     * @param array $data
159
     *
160
     * @return PromiseInterface
161
     */
162 View Code Duplication
    public function update(array $data)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
163
    {
164
        return Coroutine\create(function () use ($data) {
165
            $builder = $this->builder->update($data);
166
            $builder = $this->applyOperationsTo($builder);
167
168
            yield $this->connector->query(
169
                $this->interpolate($builder->build())
170
            );
171
        });
172
    }
173
174
    /**
175
     * @return PromiseInterface
176
     */
177 View Code Duplication
    public function delete()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
178
    {
179
        return Coroutine\create(function () {
180
            $builder = $this->builder->delete();
181
            $builder = $this->applyOperationsTo($builder);
182
183
            yield $this->connector->query(
184
                $this->interpolate($builder->build())
185
            );
186
        });
187
    }
188
}
189