Passed
Push — master ( 3bceb6...12b03a )
by Seth
02:10
created

EntriesBinding::listByLog()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
cc 4
eloc 11
nc 2
nop 2
dl 0
loc 19
ccs 0
cts 19
cp 0
crap 20
rs 9.2
c 0
b 0
f 0
1
<?php
2
/** EntriesBinding */
3
4
namespace Battis\SharedLogs\Database\Bindings;
5
6
use Battis\SharedLogs\Database\AbstractBinding;
7
use Battis\SharedLogs\Database\Bindings\Traits\EntriesBindingTrait;
8
use Battis\SharedLogs\Database\Bindings\Traits\LogsBindingTrait;
9
use Battis\SharedLogs\Database\Bindings\Traits\UsersBindingTrait;
10
use Battis\SharedLogs\Exceptions\BindingException;
11
use Battis\SharedLogs\Objects\Entry;
12
use Battis\SharedLogs\Objects\Log;
13
use Battis\SharedLogs\Objects\User;
14
use PDO;
15
16
/**
17
 * A binding between `Entry` objects and the `entries` database table
18
 *
19
 * @author Seth Battis <[email protected]>
20
 */
21
class EntriesBinding extends AbstractBinding
22
{
23
    use EntriesBindingTrait, LogsBindingTrait, UsersBindingTrait;
24
25
    const SCOPE_COUNT = 'count';
26
27
    const INCLUDE_USER = 'user';
28
    const INCLUDE_LOG = 'log';
29
30
    /**
31
     * Constuct an entries binding from a database connector
32
     *
33
     * @param PDO $database
34
     *
35
     * @throws BindingException
36
     */
37
    public function __construct(PDO $database)
38
    {
39
        parent::__construct($database, 'entries', Entry::class);
40
    }
41
42
    /**
43
     * Retrieve an entry by ID
44
     *
45
     * By default, entries contain both log and user sub-objects.
46
     *
47
     * @param int|string $id
48
     * @param array $params
49
     *
50
     * @return Entry|null
51
     */
52
    public function get($id, $params = [self::SCOPE_INCLUDE => [self::INCLUDE_LOG, self::INCLUDE_USER]])
53
    {
54
        return parent::get($id, $params);
55
    }
56
57
    /**
58
     * Instantiate an entry retrieved via `get()`
59
     *
60
     * @used-by EntriesBinding::instantiateListedObject()
61
     *
62
     * @param array $databaseRow
63
     * @param array $params
64
     *
65
     * @return Entry
66
     */
67
    protected function instantiateObject($databaseRow, $params)
68
    {
69
        $log = Entry::SUPPRESS_LOG;
70
        $user = Entry::SUPPRESS_USER;
71 View Code Duplication
        if (self::parameterValueExists($params,self::SCOPE_INCLUDE,self::INCLUDE_LOG)) {
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...
72
            $params = self::consumeParameterValue($params,self::SCOPE_INCLUDE,self::INCLUDE_LOG);
73
            $params = self::consumeParameterValue($params,self::SCOPE_INCLUDE,LogsBinding::INCLUDE_ENTRIES);
74
            $log = $this->logs()->get($databaseRow[Log::ID], $params);
75
        }
76
        if (self::parameterValueExists($params, self::SCOPE_INCLUDE, self::INCLUDE_USER)) {
77
            $params = self::consumeParameterValue($params, self::SCOPE_INCLUDE,self::INCLUDE_USER);
78
            $user = $this->users()->get($databaseRow[User::ID], $params);
79
        }
80
        return $this->object($databaseRow, $log, $user);
81
    }
82
83
    /**
84
     * Instantiate an entry retrieved via `all()`
85
     *
86
     * @param array $databaseRow
87
     * @param array $params
88
     *
89
     * @uses EntriesBinding::instantiateObject()
90
     *
91
     * @return Entry
92
     */
93
    protected function instantiateListedObject($databaseRow, $params)
94
    {
95
        return $this->instantiateObject($databaseRow, $params);
96
    }
97
98
    /**
99
     * Retrieve all entries in a specific log, by log ID
100
     *
101
     * By default, entries retrieved by this method contain user sub-object, but _not_ a log sub-object.
102
     *
103
     * @param integer|string $id Numeric log ID
104
     * @param array $params (Optional) Associative array of additional request parameters
105
     * @return Entry[]
106
     */
107
    public function listByLog($id, $params = [self::SCOPE_INCLUDE => [self::INCLUDE_USER]])
108
    {
109
        $statement = $this->database()->prepare("
110
            SELECT *
111
                FROM `" . $this->databaseTable() . "`
112
                WHERE
113
                  `" . Log::ID . "` = :id
114
                ORDER BY
115
                    " . $this->listOrder() . "
116
                " . (($count = self::getScope($params, self::SCOPE_COUNT) !== null) ? "LIMIT $count" : "") . "
117
        ");
118
        $list = [];
119
        if ($statement->execute(['id' => $id])) {
120
            while ($row = $statement->fetch()) {
121
                $list[] = $this->instantiateListedObject($row, $params);
122
            }
123
        }
124
        return $list;
125
    }
126
}
127