Completed
Push — master ( 7860e0...d3b4dd )
by Matthew
02:30 queued 24s
created

AbstractEndpointRepository::read()   F

Complexity

Conditions 18
Paths 360

Size

Total Lines 87
Code Lines 44

Duplication

Lines 0
Ratio 0 %

Importance

Changes 15
Bugs 2 Features 11
Metric Value
c 15
b 2
f 11
dl 0
loc 87
rs 3.6715
cc 18
eloc 44
nc 360
nop 1

3 Methods

Rating   Name   Duplication   Size   Complexity  
A AbstractEndpointRepository::readSinglebyId() 0 10 1
A AbstractEndpointRepository::readAllById() 0 10 1
A AbstractEndpointRepository::readAllByFields() 0 12 2

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
14
abstract class AbstractEndpointRepository implements
15
    DatabaseAwareInterface,
16
    RedisAwareInterface,
17
    UuidAwareInterface
18
{
19
    use DatabaseAwareTrait;
20
    use RedisAwareTrait;
21
    use UuidAwareTrait;
22
23
    /**
24
     * Determines the table that the DB is interfacing with
25
     *
26
     * @return string
27
     */
28
    abstract public function getTable();
29
30
    /**
31
     * Determines the primary key of the table
32
     *
33
     * @return string
34
     */
35
    abstract public function getPrimaryKey();
36
37
    /**
38
     * Determines the Result key of the table
39
     *
40
     * @return string
41
     */
42
    abstract public function getResultKey();
43
44
    /**
45
     * Builds a new query factory ready for use with the QueryObjects
46
     *
47
     * @return \Aura\SqlQuery\AbstractQuery
48
     */
49
    public function newQuery()
50
    {
51
        $factory = new QueryFactory('mysql');
52
53
        $query = $factory->newSelect(); // Suspect I'll only ever need this one
54
        $query->from($this->getTable());
55
56
        return $query;
57
    }
58
59
    /**
60
     * Executes the statement to the DB and returns the results
61
     *
62
     * @param  \Aura\SqlQuery\AbstractQuery $query
63
     * @param  boolean                      $single
64
     *
65
     * @return array
66
     */
67
    public function fireStatementAndReturn($query, $single = false)
68
    {
69
        $pdo = $this->getDatabaseDriver();
70
71
        if ($single === false) {
72
            return $pdo->fetchAll($query->getStatement(), $query->getBindValues());
73
        }
74
75
        return $pdo->fetchOne($query->getStatement(), $query->getBindValues());
76
    }
77
78
    /**
79
     * Reads a single record from the database
80
     *
81
     * @param  string $id
82
     *
83
     * @return array
84
     */
85
    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...
86
    {
87
        $query = $this->newQuery();
88
        $key = $this->returnKeyType($keyType);
89
90
        $query->cols(['*'])
91
              ->where("`{$key}` = '{$id}'");
92
93
        return $this->fireStatementAndReturn($query, true);
94
    }
95
96
    /**
97
     * Reads all related records from the database
98
     *
99
     * @param  string $id
100
     *
101
     * @return array
102
     */
103
    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...
104
    {
105
        $query = $this->newQuery();
106
        $key = $this->returnKeyType($keyType);
107
108
        $query->cols(['*'])
109
              ->where("`{$key}` = '{$id}'");
110
111
        return $this->fireStatementAndReturn($query);
112
    }
113
114
    /**
115
     * Reads all records based off a simple where statement
116
     *
117
     * @param  array $fields
118
     *
119
     * @return array
120
     */
121
    public function readAllByFields($fields)
122
    {
123
        $query = $this->newQuery();
124
125
        $query->cols(['*']);
126
127
        foreach ($fields as $field => $value) {
128
            $query->where("`{$field}` = '{$value}'");
129
        }
130
131
        return $this->fireStatementAndReturn($query);
132
    }
133
134
    /**
135
     * Reads the count of records based off a where statement
136
     *
137
     * @param  array $fields
138
     *
139
     * @return array
140
     */
141
    public function readCountByFields($fields)
142
    {
143
        $query = $this->newQuery();
144
        $key   = $this->returnKeyType('primary');
145
146
        $query->cols(["COUNT({$key}) as COUNT"]);
147
148
        foreach ($fields as $field => $value) {
149
            $query->where("`{$field}` = '{$value}'");
150
        }
151
152
        $result = $this->fireStatementAndReturn($query);
153
154
        // Done this to prevent the need for clients to also do this. Returns a single number this way.
155
        return $result[0]["COUNT"];
156
    }
157
158
    /**
159
     * Sets the proper key to search on based off a string
160
     *
161
     * @param  string $key
162
     *
163
     * @return string
164
     */
165
    public function returnKeyType($key)
166
    {
167
        switch ($key) {
168
            case 'result':
169
                return $this->getResultKey();
170
            case 'primary':
171
            default:
172
                return $this->getPrimaryKey();
173
        }
174
    }
175
}
176