Completed
Push — staging ( 83f973...0db769 )
by Matthew
02:32
created

AbstractEndpointRepository::readAllByFields()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 12
rs 9.4285
cc 2
eloc 6
nc 2
nop 1
1
<?php
2
3
namespace Ps2alerts\Api\Repository;
4
5
use Aura\SqlQuery\AbstractQuery;
6
use Aura\SqlQuery\QueryFactory;
7
use Ps2alerts\Api\Contract\DatabaseAwareInterface;
8
use Ps2alerts\Api\Contract\DatabaseAwareTrait;
9
use Ps2alerts\Api\Contract\RedisAwareInterface;
10
use Ps2alerts\Api\Contract\RedisAwareTrait;
11
use Ps2alerts\Api\Contract\UuidAwareInterface;
12
use Ps2alerts\Api\Contract\UuidAwareTrait;
13
use Ps2alerts\Api\QueryObjects\QueryObject;
14
15
abstract class AbstractEndpointRepository implements
16
    DatabaseAwareInterface,
17
    RedisAwareInterface,
18
    UuidAwareInterface
19
{
20
    use DatabaseAwareTrait;
21
    use RedisAwareTrait;
22
    use UuidAwareTrait;
23
24
    /**
25
     * Determines the table that the DB is interfacing with
26
     *
27
     * @return string
28
     */
29
    abstract public function getTable();
30
31
    /**
32
     * Determines the primary key of the table
33
     *
34
     * @return string
35
     */
36
    abstract public function getPrimaryKey();
37
38
    /**
39
     * Determines the Result key of the table
40
     *
41
     * @return string
42
     */
43
    abstract public function getResultKey();
44
45
    /**
46
     * Builds a new query factory ready for use with the QueryObjects
47
     *
48
     * @return \Aura\SqlQuery\AbstractQuery
49
     */
50
    public function newQuery()
51
    {
52
        $factory = new QueryFactory('mysql');
53
54
        $query = $factory->newSelect(); // Suspect I'll only ever need this one
55
        $query->from($this->getTable());
56
57
        return $query;
58
    }
59
60
    /**
61
     * Executes the statement to the DB and returns the results
62
     *
63
     * @param  \Aura\SqlQuery\AbstractQuery $query
64
     * @param  boolean                      $single
65
     *
66
     * @return array
67
     */
68
    public function fireStatementAndReturn($query, $single = false)
69
    {
70
        $pdo = $this->getDatabaseDriver();
71
72
        if ($single === false) {
73
            return $pdo->fetchAll($query->getStatement(), $query->getBindValues());
74
        }
75
76
        return $pdo->fetchOne($query->getStatement(), $query->getBindValues());
77
    }
78
79
    /**
80
     * Reads a single record from the database
81
     *
82
     * @param  string $id
83
     *
84
     * @return array
85
     */
86
    public function readSinglebyId($id, $keyType = 'primary')
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...
87
    {
88
        $query = $this->newQuery();
89
        $key = $this->returnKeyType($keyType);
90
91
        $query->cols(['*'])
92
              ->where("`{$key}` = '{$id}'");
93
94
        return $this->fireStatementAndReturn($query, true);
95
    }
96
97
    /**
98
     * Reads all related records from the database
99
     *
100
     * @param  string $id
101
     *
102
     * @return array
103
     */
104
    public function readAllById($id, $keyType = 'primary')
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...
105
    {
106
        $query = $this->newQuery();
107
        $key = $this->returnKeyType($keyType);
108
109
        $query->cols(['*'])
110
              ->where("`{$key}` = '{$id}'");
111
112
        return $this->fireStatementAndReturn($query);
113
    }
114
115
    /**
116
     * Reads all records based off a simple where statement
117
     *
118
     * @param  array $fields
119
     * @param  string $value
0 ignored issues
show
Bug introduced by
There is no parameter named $value. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
120
     *
121
     * @return array
122
     */
123
    public function readAllByFields($fields)
124
    {
125
        $query = $this->newQuery();
126
127
        $query->cols(['*']);
128
129
        foreach($fields as $field => $value) {
130
            $query->where("`{$field}` = '{$value}'");
131
        }
132
133
        return $this->fireStatementAndReturn($query);
134
    }
135
136
    /**
137
     * Reads the count of records based off a where statement
138
     *
139
     * @param  array $field
0 ignored issues
show
Documentation introduced by
There is no parameter named $field. Did you maybe mean $fields?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

Consider the following example. The parameter $ireland is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $ireland
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was changed, but the annotation was not.

Loading history...
140
     * @param  string $value
0 ignored issues
show
Bug introduced by
There is no parameter named $value. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
141
     *
142
     * @return array
143
     */
144
    public function readCountByFields($fields)
145
    {
146
        $query = $this->newQuery();
147
        $key   = $this->returnKeyType('primary');
148
149
        $query->cols(["COUNT({$key}) as COUNT"]);
150
151
        foreach($fields as $field => $value) {
152
            $query->where("`{$field}` = '{$value}'");
153
        }
154
155
        $result = $this->fireStatementAndReturn($query);
156
157
        // Done this to prevent the need for clients to also do this. Returns a single number this way.
158
        return $result[0]["COUNT"];
159
    }
160
161
    /**
162
     * Sets the proper key to search on based off a string
163
     *
164
     * @param  string $key
165
     *
166
     * @return string
167
     */
168
    public function returnKeyType($key)
169
    {
170
        switch ($key) {
171
            case 'result':
172
                return $this->getResultKey();
173
            case 'primary':
174
            default:
175
                return $this->getPrimaryKey();
176
        }
177
    }
178
}
179