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

MySQLConnector::query()   B

Complexity

Conditions 6
Paths 1

Size

Total Lines 48
Code Lines 27

Duplication

Lines 8
Ratio 16.67 %

Code Coverage

Tests 25
CRAP Score 6.0944

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 8
loc 48
ccs 25
cts 29
cp 0.8621
rs 8.551
cc 6
eloc 27
nc 1
nop 1
crap 6.0944
1
<?php
2
3
namespace AsyncPHP\Icicle\Database\Connector;
4
5
use AsyncPHP\Icicle\Database\Connector;
6
use Icicle\Loop;
7
use Icicle\Promise\Deferred;
8
use Icicle\Promise\PromiseInterface;
9
use InvalidArgumentException;
10
use MySQLi;
11
12
final class MySQLConnector implements Connector
13
{
14
    /**
15
     * @var null|MySQLi
16
     */
17
    private $connection = null;
18
19
    /**
20
     * @var bool
21
     */
22
    private $ready = true;
23
24
    /**
25
     * @var float
26
     */
27
    private $poll = 0.000001;
28
29
    /**
30
     * @inheritdoc
31
     *
32
     * @param array $config
33
     *
34
     * @throws InvalidArgumentException
35
     */
36 1
    public function connect(array $config)
37
    {
38 1
        if (!isset($config["host"])) {
39 1
            $config["host"] = "127.0.0.1";
40 1
        }
41
42 1
        if (!isset($config["username"])) {
43
            throw new InvalidArgumentException("Undefined username");
44
        }
45
46 1
        if (!isset($config["password"])) {
47
            throw new InvalidArgumentException("Undefined password");
48
        }
49
50 1
        if (!isset($config["database"])) {
51
            throw new InvalidArgumentException("Undefined database");
52
        }
53
54 1
        if (!isset($config["port"])) {
55 1
            $config["port"] = 3306;
56 1
        }
57
58 1
        if (!isset($config["socket"])) {
59 1
            $config["socket"] = null;
60 1
        }
61
62 1
        $this->connection = mysqli_connect(
63 1
            $config["host"],
64 1
            $config["username"],
65 1
            $config["password"],
66 1
            $config["database"],
67 1
            $config["port"],
68 1
            $config["socket"]
69 1
        );
70 1
    }
71
72
    /**
73
     * @inheritdoc
74
     *
75
     * @param string $query
76
     *
77
     * @return PromiseInterface
78
     */
79 1
    public function query($query)
80
    {
81 1
        $deferred = new Deferred();
82
83
        $links = $errors = $rejects = [
84 1
            $this->connection,
85 1
        ];
86
87
        $outer = Loop\periodic($this->poll, function () use (&$outer, $links, $errors, $rejects, $query, $deferred) {
0 ignored issues
show
Unused Code introduced by
$outer is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
88 1
            if ($this->ready) {
89 1
                $this->ready = false;
90 1
                $outer->stop();
91
92 1
                $result = $this->connection->query($query, MYSQLI_ASYNC);
93
94 1 View Code Duplication
                if ($result === false) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
95
                    $this->ready = true;
96
                    return $deferred->reject($this->connection->error);
97
                }
98
99 1
                $inner = Loop\periodic($this->poll, function () use (&$inner, $links, $errors, $rejects, $deferred) {
0 ignored issues
show
Unused Code introduced by
$inner is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
100 1
                    if (mysqli_poll($links, $errors, $rejects, $this->poll)) {
101 1
                        $inner->stop();
102
103 1
                        $result = $this->connection->reap_async_query();
104
105 1 View Code Duplication
                        if ($result === false) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
106
                            $this->ready = true;
107
                            return $deferred->reject($this->connection->error);
108
                        }
109
110 1
                        if ($result === true) {
111 1
                            $this->ready = true;
112 1
                            return $deferred->resolve();
113
                        }
114
115 1
                        $rows = $result->fetch_all(MYSQLI_ASSOC);
116 1
                        $result->free();
117
118 1
                        $this->ready = true;
119 1
                        return $deferred->resolve($rows);
120
                    }
121 1
                });
122 1
            }
123 1
        });
124
125 1
        return $deferred->getPromise();
126
    }
127
128
    /**
129
     * @inheritdoc
130
     *
131
     * @param mixed $value
132
     *
133
     * @return mixed
134
     */
135 1
    public function escape($value)
136
    {
137 1
        return mysqli_real_escape_string($this->connection, $value);
138
    }
139
}
140