Completed
Push — master ( f59d63...2f2930 )
by Christopher
02:24
created

Manager::table()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 2
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
    public function __construct(Connector $connector, Builder $builder)
33
    {
34
        $this->connector = $connector;
35
        $this->builder = $builder;
36
    }
37
38
    /**
39
     * @param string $method
40
     * @param array $parameters
41
     *
42
     * @return mixed
43
     */
44
    public function __call($method, array $parameters = [])
45
    {
46
        $operations = $this->operations;
47
        $operations[] = [$method, $parameters];
48
49
        return $this->cloneWith("operations", $operations);
50
    }
51
52
    /**
53
     * @param string $table
54
     *
55
     * @return static
56
     */
57
    public function table($table)
58
    {
59
        return $this->cloneWith("builder", $this->builder->table($table));
60
    }
61
62
    /**
63
     * @param string $columns
64
     *
65
     * @return static
66
     */
67
    public function select($columns = "*")
68
    {
69
        return $this->cloneWith("builder", $this->builder->select($columns));
70
    }
71
72
    /**
73
     * @return PromiseInterface
74
     */
75
    public function first()
76
    {
77
        return Coroutine\create(function () {
78
            $rows = (yield $this->limit(1)->get());
79
            yield reset($rows);
80
        });
81
    }
82
83
    /**
84
     * @return PromiseInterface
85
     */
86
    public function get()
87
    {
88
        return Coroutine\create(function () {
89
            $builder = $this->applyOperationsTo($this->builder);
90
91
            yield $this->connector->query(
92
                $this->interpolate($builder->build())
93
            );
94
        });
95
    }
96
97
    /**
98
     * @param array $build
99
     *
100
     * @return string
101
     */
102
    private function interpolate(array $build)
103
    {
104
        list($statement, $values) = $build;
105
106
        $pattern = "/\\:([_0-9a-zA-Z]+)/";
107
108
        $replacement = function ($matches) use ($values) {
109
            return $this->quote($values[$matches[1]]);
110
        };
111
112
        return preg_replace_callback($pattern, $replacement, $statement);
113
    }
114
115
    /**
116
     * @param mixed $value
117
     *
118
     * @return string
119
     */
120
    private function quote($value)
121
    {
122
        return "'" . $this->connector->escape($value) . "'";
123
    }
124
125
    /**
126
     * @param array $data
127
     *
128
     * @return PromiseInterface
129
     */
130 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
            $builder = $this->builder->insert($data);
134
            $builder = $this->applyOperationsTo($builder);
135
136
            yield $this->connector->query(
137
                $this->interpolate($builder->build())
138
            );
139
        });
140
    }
141
142
    /**
143
     * @param Builder $builder
144
     *
145
     * @return Builder
146
     */
147
    private function applyOperationsTo($builder)
148
    {
149
        foreach ($this->operations as $operation) {
150
            list($method, $parameters) = $operation;
151
            $builder = call_user_func_array([$builder, $method], $parameters);
152
        }
153
154
        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