Completed
Push — master ( 4dbb52...f13a61 )
by Christopher
03:07
created

Manager::update()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 8
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 8
loc 8
ccs 0
cts 7
cp 0
rs 9.4286
cc 1
eloc 4
nc 1
nop 1
crap 2
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
            yield reset($rows);
54
        });
55
    }
56
57
    /**
58
     * @return PromiseInterface
59
     */
60
    public function get()
61
    {
62
        return Coroutine\create(function () {
63
            yield $this->connector->query(
64
                $this->interpolate($this->builder->build())
65
            );
66
        });
67
    }
68
69
    /**
70
     * @param array $build
71
     *
72
     * @return string
73
     */
74
    private function interpolate(array $build)
75
    {
76
        list($statement, $values) = $build;
77
78
        return preg_replace_callback("/\\:([_0-9a-zA-Z]+)/", function ($matches) use ($values) {
79
            return "'" . $this->connector->escape($values[$matches[1]]) . "'";
80
        }, $statement);
81
    }
82
83
    /**
84
     * @param array $data
85
     *
86
     * @return PromiseInterface
87
     */
88 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...
89
    {
90
        return Coroutine\create(function () use ($data) {
91
            yield $this->connector->query(
92
                $this->interpolate($this->builder->insert($data)->build())
93
            );
94
        });
95
    }
96
97
    /**
98
     * @param array $data
99
     *
100
     * @return PromiseInterface
101
     */
102 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...
103
    {
104
        return Coroutine\create(function () use ($data) {
105
            yield $this->connector->query(
106
                $this->interpolate($this->builder->update($data)->build())
107
            );
108
        });
109
    }
110
111
    /**
112
     * @return PromiseInterface
113
     */
114
    public function delete()
115
    {
116
        return Coroutine\create(function () {
117
            yield $this->connector->query(
118
                $this->interpolate($this->builder->delete()->build())
119
            );
120
        });
121
    }
122
}
123