| Total Complexity | 47 |
| Total Lines | 307 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like HasCompositePrimaryKey often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use HasCompositePrimaryKey, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 13 | trait HasCompositePrimaryKey |
||
| 14 | { |
||
| 15 | use NormalizedKeysParser, PrimaryKeyInformation, CompositeRelationships; |
||
| 16 | |||
| 17 | protected $hexBinaryColumns = false; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * Destroy the models for the given IDs. |
||
| 21 | * |
||
| 22 | * @param array|int $ids |
||
| 23 | * |
||
| 24 | * @return int |
||
| 25 | */ |
||
| 26 | public static function destroy($ids) |
||
| 27 | { |
||
| 28 | // We'll initialize a count here so we will return the total number of deletes |
||
| 29 | // for the operation. The developers can then check this number as a boolean |
||
| 30 | // type value or get this total count of records deleted for logging, etc. |
||
| 31 | $count = 0; |
||
| 32 | |||
| 33 | $ids = is_array($ids) ? $ids : func_get_args(); |
||
| 34 | |||
| 35 | foreach ((new static())->applyIds($ids)->get() as $model) { |
||
|
|
|||
| 36 | if ($model->delete()) { |
||
| 37 | $count++; |
||
| 38 | } |
||
| 39 | } |
||
| 40 | |||
| 41 | return $count; |
||
| 42 | } |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Convert the object into something JSON serializable. |
||
| 46 | * |
||
| 47 | * @return array |
||
| 48 | */ |
||
| 49 | public function jsonSerialize() |
||
| 50 | { |
||
| 51 | $attributes = $this->toArray(); |
||
| 52 | foreach ($attributes as $key => $value) { |
||
| 53 | if (in_array($key, $this->getBinaryColumns())) { |
||
| 54 | $attributes[$key] = strtoupper(bin2hex($value)); |
||
| 55 | } |
||
| 56 | } |
||
| 57 | |||
| 58 | // append virtual row id |
||
| 59 | if (count($this->getRawKeyName()) > 1) { |
||
| 60 | $attributes[$this->getNormalizedKeyName()] = $this->getNormalizedKey(); |
||
| 61 | } |
||
| 62 | |||
| 63 | return $attributes; |
||
| 64 | } |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Get the primary key for the model. |
||
| 68 | * |
||
| 69 | * @return array |
||
| 70 | */ |
||
| 71 | public function getRawKeyName() |
||
| 72 | { |
||
| 73 | return $this->hasCompositeIndex() ? $this->primaryKey : [$this->primaryKey]; |
||
| 74 | } |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Get the value of the model's primary key. |
||
| 78 | * |
||
| 79 | * @return mixed |
||
| 80 | */ |
||
| 81 | public function getRawKey() |
||
| 82 | { |
||
| 83 | $attributes = []; |
||
| 84 | |||
| 85 | foreach ($this->getRawKeyName() as $key) { |
||
| 86 | $attributes[$key] = $this->getAttribute($key); |
||
| 87 | } |
||
| 88 | |||
| 89 | return $attributes; |
||
| 90 | } |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Get the value indicating whether the IDs are incrementing. |
||
| 94 | * |
||
| 95 | * @return bool |
||
| 96 | */ |
||
| 97 | public function getIncrementing() |
||
| 98 | { |
||
| 99 | return false; |
||
| 100 | } |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Get the primary key for the model. |
||
| 104 | * |
||
| 105 | * @return string |
||
| 106 | */ |
||
| 107 | public function getKeyName() |
||
| 110 | } |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Get virtual string key, required for proper collection support. |
||
| 114 | * |
||
| 115 | * @return mixed |
||
| 116 | */ |
||
| 117 | public function getKey() |
||
| 120 | } |
||
| 121 | |||
| 122 | /** |
||
| 123 | * @param \Illuminate\Database\Query\Builder $query |
||
| 124 | * @param array|string $ids |
||
| 125 | * @param bool $inverse |
||
| 126 | * |
||
| 127 | * @throws MissingPrimaryKeyValueException |
||
| 128 | * @throws WrongKeyException |
||
| 129 | */ |
||
| 130 | public function scopeApplyIds($query, $ids, $inverse = false) |
||
| 131 | { |
||
| 132 | $keys = ($instance = new static())->getRawKeyName(); |
||
| 133 | |||
| 134 | if (!is_array($ids) || Arr::isAssoc($ids)) { |
||
| 135 | $ids = [$ids]; |
||
| 136 | } |
||
| 137 | |||
| 138 | if ($this->hasCompositeIndex()) { |
||
| 139 | (new CompositeKeyScope($keys, $ids, $inverse, $this->getBinaryColumns()))->apply($query); |
||
| 140 | } else { |
||
| 141 | //remap hex ID to binary ID even if index is not composite |
||
| 142 | if ($this->shouldProcessBinaryAttribute($keys[0])) { |
||
| 143 | $ids = array_map(function ($hex) { |
||
| 144 | return hex2bin($hex); |
||
| 145 | }, $ids); |
||
| 146 | } |
||
| 147 | if ($inverse) { |
||
| 148 | $query->whereNotIn($this->qualifyColumn($keys[0]), $ids); |
||
| 149 | } else { |
||
| 150 | $query->whereIn($this->qualifyColumn($keys[0]), $ids); |
||
| 151 | } |
||
| 152 | } |
||
| 153 | } |
||
| 154 | |||
| 155 | /** |
||
| 156 | * Get a new query to restore one or more models by their queueable IDs. |
||
| 157 | * |
||
| 158 | * @param array|int $ids |
||
| 159 | * |
||
| 160 | *@throws WrongKeyException |
||
| 161 | * @throws MissingPrimaryKeyValueException |
||
| 162 | * |
||
| 163 | * @return Builder |
||
| 164 | */ |
||
| 165 | public function newQueryForRestoration($ids) |
||
| 177 | ) |
||
| 178 | ); |
||
| 179 | } |
||
| 180 | |||
| 181 | /** |
||
| 182 | * @param \Illuminate\Database\Query\Builder $query |
||
| 183 | * |
||
| 184 | * @return Builder|static |
||
| 185 | */ |
||
| 186 | public function newEloquentBuilder($query) |
||
| 187 | { |
||
| 188 | return new CompositeKeyQueryBuilder($query); |
||
| 189 | } |
||
| 190 | |||
| 191 | public function hexBinaryColumns() |
||
| 192 | { |
||
| 193 | return $this->hexBinaryColumns; |
||
| 194 | } |
||
| 195 | |||
| 196 | public function getBinaryColumns() |
||
| 197 | { |
||
| 198 | return $this->binaryColumns ?? []; |
||
| 199 | } |
||
| 200 | |||
| 201 | /** |
||
| 202 | * Get the primary key value for a save query. |
||
| 203 | * |
||
| 204 | * @return mixed |
||
| 205 | */ |
||
| 206 | protected function getKeyForSaveQuery() |
||
| 207 | { |
||
| 208 | $originalKeys = array_intersect_key($this->original, array_flip($this->getRawKeyName())); |
||
| 209 | |||
| 210 | return array_merge($this->getRawKey(), $originalKeys); |
||
| 211 | } |
||
| 212 | |||
| 213 | /** |
||
| 214 | * Set the keys for a save update query. |
||
| 215 | * |
||
| 216 | * @param Builder $query |
||
| 217 | * |
||
| 218 | *@throws MissingPrimaryKeyValueException |
||
| 219 | * |
||
| 220 | * @return Builder |
||
| 221 | */ |
||
| 222 | protected function setKeysForSaveQuery(Builder $query) |
||
| 223 | { |
||
| 224 | foreach ($this->getRawKeyName() as $key) { |
||
| 225 | if (isset($this->{$key})) { |
||
| 226 | $query->where($key, '=', $this->getAttributeFromArray($key)); |
||
| 227 | } else { |
||
| 228 | throw new MissingPrimaryKeyValueException($key, 'Missing value for key '.$key); |
||
| 229 | } |
||
| 230 | } |
||
| 231 | |||
| 232 | return $query; |
||
| 233 | } |
||
| 234 | |||
| 235 | /** |
||
| 236 | * Run the increment or decrement method on the model. |
||
| 237 | * |
||
| 238 | * @param string $column |
||
| 239 | * @param float|int $amount |
||
| 240 | * @param array $extra |
||
| 241 | * @param string $method |
||
| 242 | * |
||
| 243 | * @return int |
||
| 244 | */ |
||
| 245 | protected function incrementOrDecrement($column, $amount, $extra, $method) |
||
| 246 | { |
||
| 247 | $query = $this->newQuery(); |
||
| 248 | |||
| 249 | if (!$this->exists) { |
||
| 250 | return $query->{$method}($column, $amount, $extra); |
||
| 251 | } |
||
| 252 | |||
| 253 | $this->incrementOrDecrementAttributeValue($column, $amount, $extra, $method); |
||
| 254 | |||
| 255 | foreach ($this->getRawKeyName() as $key) { |
||
| 256 | $query->where($key, $this->getAttribute($key)); |
||
| 257 | } |
||
| 258 | |||
| 259 | return $query->{$method}($column, $amount, $extra); |
||
| 260 | } |
||
| 261 | |||
| 262 | /** |
||
| 263 | * Retrieve the model for a bound value. |
||
| 264 | * |
||
| 265 | * @param mixed $value |
||
| 266 | * |
||
| 267 | * @return Model|null |
||
| 268 | */ |
||
| 269 | public function resolveRouteBinding($value) |
||
| 270 | { |
||
| 271 | if ($this->hasCompositeIndex() && $this->getRouteKeyName() == $this->getKeyName()) { |
||
| 272 | return $this->whereKey($value)->first(); |
||
| 273 | } else { |
||
| 274 | return $this->where($this->getRouteKeyName(), $value)->first(); |
||
| 275 | } |
||
| 276 | } |
||
| 277 | |||
| 278 | private function shouldProcessBinaryAttribute($key) |
||
| 279 | { |
||
| 280 | return $this->hexBinaryColumns() && in_array($key, $this->getBinaryColumns()); |
||
| 281 | } |
||
| 282 | |||
| 283 | public function hasGetMutator($key) |
||
| 284 | { |
||
| 285 | if ($this->shouldProcessBinaryAttribute($key) && isset($this->{$key})) { |
||
| 286 | return true; |
||
| 287 | } |
||
| 288 | |||
| 289 | return parent::hasGetMutator($key); |
||
| 290 | } |
||
| 291 | |||
| 292 | public function mutateAttribute($key, $value) |
||
| 293 | { |
||
| 294 | if ($this->shouldProcessBinaryAttribute($key)) { |
||
| 295 | return strtoupper(bin2hex($value)); |
||
| 296 | } |
||
| 297 | |||
| 298 | return parent::mutateAttribute($key, $value); |
||
| 299 | } |
||
| 300 | |||
| 301 | public function hasSetMutator($key) |
||
| 308 | } |
||
| 309 | |||
| 310 | public function setMutatedAttributeValue($key, $value) |
||
| 320 | } |
||
| 321 | } |
||
| 322 |