shared-logs /
api
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | /** LogsBinding */ |
||
| 3 | |||
| 4 | namespace Battis\SharedLogs\Database\Bindings; |
||
| 5 | |||
| 6 | use Battis\SharedLogs\Database\AbstractBinding; |
||
| 7 | use Battis\SharedLogs\Database\Bindings\Traits\DevicesBindingTrait; |
||
| 8 | use Battis\SharedLogs\Database\Bindings\Traits\EntriesBindingTrait; |
||
| 9 | use Battis\SharedLogs\Database\Bindings\Traits\LogsBindingTrait; |
||
| 10 | use Battis\SharedLogs\Exceptions\BindingException; |
||
| 11 | use Battis\SharedLogs\Objects\Device; |
||
| 12 | use Battis\SharedLogs\Objects\Log; |
||
| 13 | use PDO; |
||
| 14 | |||
| 15 | /** |
||
| 16 | * A binding between `Log` objects and the `logs` database table |
||
| 17 | * |
||
| 18 | * @author Seth Battis <[email protected]> |
||
| 19 | */ |
||
| 20 | class LogsBinding extends AbstractBinding |
||
| 21 | { |
||
| 22 | use LogsBindingTrait, DevicesBindingTrait, EntriesBindingTrait; |
||
| 23 | |||
| 24 | const INCLUDE_DEVICE = 'device'; |
||
| 25 | const INCLUDE_ENTRIES = 'entries'; |
||
| 26 | const INCLUDE_RECENT_ENTRIES = 'recent'; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * Construct a logs binding from a database connector |
||
| 30 | * |
||
| 31 | * @param PDO $database |
||
| 32 | * |
||
| 33 | * @throws BindingException |
||
| 34 | */ |
||
| 35 | public function __construct(PDO $database) |
||
| 36 | { |
||
| 37 | parent::__construct($database, 'logs', Log::class); |
||
| 38 | } |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Retrieve all logs from database |
||
| 42 | * |
||
| 43 | * By default, logs contain a device sub-object |
||
| 44 | * |
||
| 45 | * @param array $params |
||
| 46 | * |
||
| 47 | * @return Object[] |
||
| 48 | */ |
||
| 49 | public function all($params = [self::SCOPE_INCLUDE => [self::INCLUDE_DEVICE]]) |
||
| 50 | { |
||
| 51 | return parent::all($params); |
||
| 52 | } |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Retrieve log by ID from database |
||
| 56 | * |
||
| 57 | * By default the log contains a device sub-object |
||
| 58 | * |
||
| 59 | * @param int|string $id |
||
| 60 | * @param array $params |
||
| 61 | * |
||
| 62 | * @return Log|null |
||
| 63 | */ |
||
| 64 | public function get($id, $params = [self::SCOPE_INCLUDE => [self::INCLUDE_DEVICE]]) |
||
| 65 | { |
||
| 66 | return parent::get($id, $params); |
||
| 67 | } |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Instantiate a log retrieved via `get()` |
||
| 71 | * |
||
| 72 | * @used-by LogsBinding::instantiateListedObject() |
||
| 73 | * |
||
| 74 | * @param array $databaseRow |
||
| 75 | * @param array $params |
||
| 76 | * |
||
| 77 | * @return Log |
||
| 78 | */ |
||
| 79 | protected function instantiateObject($databaseRow, $params) |
||
| 80 | { |
||
| 81 | $device = Log::SUPPRESS_DEVICE; |
||
| 82 | $entries = Log::SUPPRESS_ENTRIES; |
||
| 83 | View Code Duplication | if (self::parameterValueExists($params, self::SCOPE_INCLUDE, self::INCLUDE_DEVICE)) { |
|
|
0 ignored issues
–
show
|
|||
| 84 | $params = self::consumeParameterValue($params, self::SCOPE_INCLUDE, self::INCLUDE_DEVICE); |
||
| 85 | $params = self::consumeParameterValue($params, self::SCOPE_INCLUDE, DevicesBinding::INCLUDE_LOGS); |
||
| 86 | $device = $this->devices()->get($databaseRow[Device::ID], $params); |
||
| 87 | } |
||
| 88 | if (self::parameterValueExists($params,self::SCOPE_INCLUDE, self::INCLUDE_ENTRIES)) { |
||
| 89 | $params = self::consumeParameterValue($params, self::SCOPE_INCLUDE, self::INCLUDE_ENTRIES); |
||
| 90 | $params = self::consumeParameterValue($params, self::SCOPE_INCLUDE, EntriesBinding::INCLUDE_LOG); |
||
| 91 | $entries = $this->entries()->listByLog($databaseRow['id'], $params); |
||
| 92 | View Code Duplication | } elseif (self::parameterValueExists($params, self::SCOPE_INCLUDE, self::INCLUDE_RECENT_ENTRIES)) { |
|
|
0 ignored issues
–
show
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...
|
|||
| 93 | $params = self::consumeParameterValue($params, self::SCOPE_INCLUDE, self::INCLUDE_RECENT_ENTRIES); |
||
| 94 | $params = self::consumeParameterValue($params, self::SCOPE_INCLUDE, EntriesBinding::INCLUDE_LOG); |
||
| 95 | $params[EntriesBinding::SCOPE_COUNT] = 1; |
||
| 96 | $entries = $this->entries()->listByLog($databaseRow['id'], $params); |
||
| 97 | } |
||
| 98 | |||
| 99 | return $this->object($databaseRow, $device, $entries); |
||
| 100 | } |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Instantiate a log retrieved via `all()` or `listByDevice()` |
||
| 104 | * |
||
| 105 | * @used-by LogsBinding::listByDevice() |
||
| 106 | * |
||
| 107 | * @param array $databaseRow |
||
| 108 | * @param array $params |
||
| 109 | * |
||
| 110 | * @uses LogsBinding::instantiateObject() |
||
| 111 | * |
||
| 112 | * @return Log |
||
| 113 | */ |
||
| 114 | protected function instantiateListedObject($databaseRow, $params) |
||
| 115 | { |
||
| 116 | return $this->instantiateObject($databaseRow, $params); |
||
| 117 | } |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Retrieve all logs associated with a specific device, by device ID |
||
| 121 | * |
||
| 122 | * By default, the logs retrieved will _not_ contain a device sub-object |
||
| 123 | * |
||
| 124 | * @param string|integer $id Numeric device ID |
||
| 125 | * @param array $params (Optional) Associative array of additional request parameters |
||
| 126 | * |
||
| 127 | * @uses LogsBinding::instantiateListedObject() |
||
| 128 | * |
||
| 129 | * @return Log[] |
||
| 130 | */ |
||
| 131 | public function listByDevice($id, $params = []) |
||
| 132 | { |
||
| 133 | $statement = $this->database()->prepare(" |
||
| 134 | SELECT * |
||
| 135 | FROM `" . $this->databaseTable() . "` |
||
| 136 | WHERE |
||
| 137 | `" . Device::ID . "` = :id |
||
| 138 | ORDER BY |
||
| 139 | " . $this->listOrder() . " |
||
| 140 | "); |
||
| 141 | $list = []; |
||
| 142 | if ($statement->execute(['id' => $id])) { |
||
| 143 | while ($row = $statement->fetch()) { |
||
| 144 | $list[] = $this->instantiateListedObject($row, $params); |
||
| 145 | } |
||
| 146 | } |
||
| 147 | return $list; |
||
| 148 | } |
||
| 149 | } |
||
| 150 |
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.