Passed
Push — master ( 12b03a...508f3d )
by Seth
08:34
created

UrlsBinding::listByDevice()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 18
Code Lines 10

Duplication

Lines 18
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 3
eloc 10
nc 2
nop 2
dl 18
loc 18
ccs 0
cts 18
cp 0
crap 12
rs 9.4285
c 1
b 1
f 0
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: sbattis
5
 * Date: 6/7/2018
6
 * Time: 2:19 PM
7
 */
8
9
namespace Battis\SharedLogs\Database\Bindings;
10
11
12
use Battis\SharedLogs\Database\AbstractBinding;
13
use Battis\SharedLogs\Database\Bindings\Traits\DevicesBindingTrait;
14
use Battis\SharedLogs\Objects\Device;
15
use Battis\SharedLogs\Objects\Url;
16
use PDO;
17
18
class UrlsBinding extends AbstractBinding
19
{
20
    use DevicesBindingTrait;
21
22
    const INCLUDE_DEVICE = "device";
23
24
    public function __construct(PDO $database)
25
    {
26
        parent::__construct($database, "urls", Url::class);
27
    }
28
29
    public function instantiateObject($databaseRow, $params)
30
    {
31
        $device = Url::SUPPRESS_DEVICE;
32 View Code Duplication
        if (self::parameterExists($params, self::SCOPE_INCLUDE, self::INCLUDE_DEVICE)) {
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...
33
            $params = self::consumeParameterValue($params, self::SCOPE_INCLUDE, self::INCLUDE_DEVICE);
34
            $params = self::consumeParameterValue($params, self::SCOPE_INCLUDE, DevicesBinding::INCLUDE_URLS);
35
            $device = $this->devices()->get($databaseRow[Device::ID], $params);
36
        }
37
        return new Url($databaseRow, $device);
38
    }
39
40
    public function instantiateListedObject($databaseRow, $params)
41
    {
42
        return $this->instantiateObject($databaseRow, $params);
43
    }
44
45
    /**
46
     * Retrieve all urls for a specific device, by device ID
47
     *
48
     * By default, urls retrieved by this method do _not_ contain a device sub-object.
49
     *
50
     * @param integer|string $id Numeric device ID
51
     * @param array $params (Optional) Associative array of additional request parameters
52
     * @return Url[]
53
     */
54 View Code Duplication
    public function listByDevice($id, $params = [])
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...
55
    {
56
        $statement = $this->database()->prepare("
57
            SELECT *
58
                FROM `" . $this->databaseTable() . "`
59
                WHERE
60
                  `" . Device::ID . "` = :id
61
                ORDER BY
62
                    " . $this->listOrder() . "
63
        ");
64
        $list = [];
65
        if ($statement->execute(['id' => $id])) {
66
            while ($row = $statement->fetch()) {
67
                $list[] = $this->instantiateListedObject($row, $params);
68
            }
69
        }
70
        return $list;
71
    }
72
73
    protected function listOrder()
74
    {
75
        return '`name` ASC';
76
    }
77
}