Completed
Push — staging ( 5f9089...8e7d49 )
by Matthew
04:33
created

AbstractEndpointRepository::newQuery()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 2
Metric Value
c 3
b 0
f 2
dl 0
loc 9
rs 9.6666
cc 1
eloc 5
nc 1
nop 0
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
    public function fireStatementAndReturn($query, $single = false)
61
    {
62
        $pdo = $this->getDatabaseDriver();
63
64
        if ($single === false) {
65
            return $pdo->fetchAll($query->getStatement(), $query->getBindValues());
66
        }
67
68
        return $pdo->fetchOne($query->getStatement(), $query->getBindValues());
69
    }
70
71
    /**
72
     * Reads a single record from the database
73
     *
74
     * @param  string $id
75
     *
76
     * @return array
77
     */
78 View Code Duplication
    public function readSingle($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...
79
    {
80
        $query = $this->newQuery();
81
        $key = $this->returnKeyType($keyType);
82
83
        $query->cols(['*'])
84
              ->where("{$key} = {$id}");
85
86
        return $this->fireStatementAndReturn($query, true);
87
    }
88
89
    /**
90
     * Reads all related records from the database
91
     *
92
     * @param  string $id
93
     *
94
     * @return array
95
     */
96 View Code Duplication
    public function readAll($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...
97
    {
98
        $query = $this->newQuery();
99
        $key = $this->returnKeyType($keyType);
100
101
        $query->cols(['*'])
102
              ->where("{$key} = {$id}");
103
104
        return $this->fireStatementAndReturn($query);
105
    }
106
107
    public function returnKeyType($key)
108
    {
109
        switch ($key) {
110
            case 'primary':
111
            default:
112
                return $this->getPrimaryKey();
113
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
114
            case 'result':
0 ignored issues
show
Unused Code introduced by
case 'result': retur...ResultKey(); break; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
115
                return $this->getResultKey();
116
                break;
117
        }
118
    }
119
}
120