| Total Complexity | 52 |
| Total Lines | 324 |
| 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 | * Automatically generate unique binary id. |
||
| 21 | */ |
||
| 22 | public static function bootHasCompositePrimaryKey() |
||
| 23 | { |
||
| 24 | static::creating(function ($model) { |
||
| 25 | foreach($model->getRawKeyName() as $key) { |
||
| 26 | if(!isset($model->{$key}) && in_array($key, $model->getBinaryColumns())) { |
||
| 27 | $v = uniqid(rand(), true); |
||
| 28 | $model->{$key} = $model->hexBinaryColumns() ? strtoupper( |
||
| 29 | md5($v) |
||
| 30 | ) : md5($v, true); |
||
| 31 | } |
||
| 32 | } |
||
| 33 | }); |
||
| 34 | } |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Destroy the models for the given IDs. |
||
| 38 | * |
||
| 39 | * @param array|int $ids |
||
| 40 | * |
||
| 41 | * @return int |
||
| 42 | */ |
||
| 43 | public static function destroy($ids) |
||
| 44 | { |
||
| 45 | // We'll initialize a count here so we will return the total number of deletes |
||
| 46 | // for the operation. The developers can then check this number as a boolean |
||
| 47 | // type value or get this total count of records deleted for logging, etc. |
||
| 48 | $count = 0; |
||
| 49 | |||
| 50 | $ids = is_array($ids) ? $ids : func_get_args(); |
||
| 51 | |||
| 52 | foreach ((new static())->applyIds($ids)->get() as $model) { |
||
|
|
|||
| 53 | if ($model->delete()) { |
||
| 54 | $count++; |
||
| 55 | } |
||
| 56 | } |
||
| 57 | |||
| 58 | return $count; |
||
| 59 | } |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Convert the object into something JSON serializable. |
||
| 63 | * |
||
| 64 | * @return array |
||
| 65 | */ |
||
| 66 | public function jsonSerialize() |
||
| 67 | { |
||
| 68 | $attributes = $this->toArray(); |
||
| 69 | foreach ($attributes as $key => $value) { |
||
| 70 | if (in_array($key, $this->getBinaryColumns())) { |
||
| 71 | $attributes[$key] = strtoupper(bin2hex($value)); |
||
| 72 | } |
||
| 73 | } |
||
| 74 | |||
| 75 | // append virtual row id |
||
| 76 | if (count($this->getRawKeyName()) > 1) { |
||
| 77 | $attributes[$this->getNormalizedKeyName()] = $this->getNormalizedKey(); |
||
| 78 | } |
||
| 79 | |||
| 80 | return $attributes; |
||
| 81 | } |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Get the primary key for the model. |
||
| 85 | * |
||
| 86 | * @return array |
||
| 87 | */ |
||
| 88 | public function getRawKeyName() |
||
| 89 | { |
||
| 90 | return $this->hasCompositeIndex() ? $this->primaryKey : [$this->primaryKey]; |
||
| 91 | } |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Get the value of the model's primary key. |
||
| 95 | * |
||
| 96 | * @return mixed |
||
| 97 | */ |
||
| 98 | public function getRawKey() |
||
| 99 | { |
||
| 100 | $attributes = []; |
||
| 101 | |||
| 102 | foreach ($this->getRawKeyName() as $key) { |
||
| 103 | $attributes[$key] = $this->getAttribute($key); |
||
| 104 | } |
||
| 105 | |||
| 106 | return $attributes; |
||
| 107 | } |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Get the value indicating whether the IDs are incrementing. |
||
| 111 | * |
||
| 112 | * @return bool |
||
| 113 | */ |
||
| 114 | public function getIncrementing() |
||
| 115 | { |
||
| 116 | return false; |
||
| 117 | } |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Get the primary key for the model. |
||
| 121 | * |
||
| 122 | * @return string |
||
| 123 | */ |
||
| 124 | public function getKeyName() |
||
| 125 | { |
||
| 126 | return $this->getNormalizedKeyName(); |
||
| 127 | } |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Get virtual string key, required for proper collection support. |
||
| 131 | * |
||
| 132 | * @return mixed |
||
| 133 | */ |
||
| 134 | public function getKey() |
||
| 135 | { |
||
| 136 | return $this->getNormalizedKey(); |
||
| 137 | } |
||
| 138 | |||
| 139 | /** |
||
| 140 | * @param \Illuminate\Database\Query\Builder $query |
||
| 141 | * @param array|string $ids |
||
| 142 | * @param bool $inverse |
||
| 143 | * |
||
| 144 | * @throws MissingPrimaryKeyValueException |
||
| 145 | * @throws WrongKeyException |
||
| 146 | */ |
||
| 147 | public function scopeApplyIds($query, $ids, $inverse = false) |
||
| 148 | { |
||
| 149 | $keys = ($instance = new static())->getRawKeyName(); |
||
| 150 | |||
| 151 | if (!is_array($ids) || Arr::isAssoc($ids)) { |
||
| 152 | $ids = [$ids]; |
||
| 153 | } |
||
| 154 | |||
| 155 | if ($this->hasCompositeIndex()) { |
||
| 156 | (new CompositeKeyScope($keys, $ids, $inverse, $this->getBinaryColumns()))->apply($query); |
||
| 157 | } else { |
||
| 158 | //remap hex ID to binary ID even if index is not composite |
||
| 159 | if ($this->shouldProcessBinaryAttribute($keys[0])) { |
||
| 160 | $ids = array_map(function ($hex) { |
||
| 161 | return hex2bin($hex); |
||
| 162 | }, $ids); |
||
| 163 | } |
||
| 164 | if ($inverse) { |
||
| 165 | $query->whereNotIn($this->qualifyColumn($keys[0]), $ids); |
||
| 166 | } else { |
||
| 167 | $query->whereIn($this->qualifyColumn($keys[0]), $ids); |
||
| 168 | } |
||
| 169 | } |
||
| 170 | } |
||
| 171 | |||
| 172 | /** |
||
| 173 | * Get a new query to restore one or more models by their queueable IDs. |
||
| 174 | * |
||
| 175 | * @param array|int $ids |
||
| 176 | * |
||
| 177 | *@throws WrongKeyException |
||
| 178 | * @throws MissingPrimaryKeyValueException |
||
| 179 | * |
||
| 180 | * @return Builder |
||
| 181 | */ |
||
| 182 | public function newQueryForRestoration($ids) |
||
| 194 | ) |
||
| 195 | ); |
||
| 196 | } |
||
| 197 | |||
| 198 | /** |
||
| 199 | * @param \Illuminate\Database\Query\Builder $query |
||
| 200 | * |
||
| 201 | * @return Builder|static |
||
| 202 | */ |
||
| 203 | public function newEloquentBuilder($query) |
||
| 204 | { |
||
| 205 | return new CompositeKeyQueryBuilder($query); |
||
| 206 | } |
||
| 207 | |||
| 208 | public function hexBinaryColumns() |
||
| 209 | { |
||
| 210 | return $this->hexBinaryColumns; |
||
| 211 | } |
||
| 212 | |||
| 213 | public function getBinaryColumns() |
||
| 214 | { |
||
| 215 | return $this->binaryColumns ?? []; |
||
| 216 | } |
||
| 217 | |||
| 218 | /** |
||
| 219 | * Get the primary key value for a save query. |
||
| 220 | * |
||
| 221 | * @return mixed |
||
| 222 | */ |
||
| 223 | protected function getKeyForSaveQuery() |
||
| 224 | { |
||
| 225 | $originalKeys = array_intersect_key($this->original, array_flip($this->getRawKeyName())); |
||
| 226 | |||
| 227 | return array_merge($this->getRawKey(), $originalKeys); |
||
| 228 | } |
||
| 229 | |||
| 230 | /** |
||
| 231 | * Set the keys for a save update query. |
||
| 232 | * |
||
| 233 | * @param Builder $query |
||
| 234 | * |
||
| 235 | *@throws MissingPrimaryKeyValueException |
||
| 236 | * |
||
| 237 | * @return Builder |
||
| 238 | */ |
||
| 239 | protected function setKeysForSaveQuery(Builder $query) |
||
| 240 | { |
||
| 241 | foreach ($this->getRawKeyName() as $key) { |
||
| 242 | if (isset($this->{$key})) { |
||
| 243 | $query->where($key, '=', $this->getAttributeFromArray($key)); |
||
| 244 | } else { |
||
| 245 | throw new MissingPrimaryKeyValueException($key, 'Missing value for key '.$key); |
||
| 246 | } |
||
| 247 | } |
||
| 248 | |||
| 249 | return $query; |
||
| 250 | } |
||
| 251 | |||
| 252 | /** |
||
| 253 | * Run the increment or decrement method on the model. |
||
| 254 | * |
||
| 255 | * @param string $column |
||
| 256 | * @param float|int $amount |
||
| 257 | * @param array $extra |
||
| 258 | * @param string $method |
||
| 259 | * |
||
| 260 | * @return int |
||
| 261 | */ |
||
| 262 | protected function incrementOrDecrement($column, $amount, $extra, $method) |
||
| 263 | { |
||
| 264 | $query = $this->newQuery(); |
||
| 265 | |||
| 266 | if (!$this->exists) { |
||
| 267 | return $query->{$method}($column, $amount, $extra); |
||
| 268 | } |
||
| 269 | |||
| 270 | $this->incrementOrDecrementAttributeValue($column, $amount, $extra, $method); |
||
| 271 | |||
| 272 | foreach ($this->getRawKeyName() as $key) { |
||
| 273 | $query->where($key, $this->getAttribute($key)); |
||
| 274 | } |
||
| 275 | |||
| 276 | return $query->{$method}($column, $amount, $extra); |
||
| 277 | } |
||
| 278 | |||
| 279 | /** |
||
| 280 | * Retrieve the model for a bound value. |
||
| 281 | * |
||
| 282 | * @param mixed $value |
||
| 283 | * |
||
| 284 | * @return Model|null |
||
| 285 | */ |
||
| 286 | public function resolveRouteBinding($value) |
||
| 287 | { |
||
| 288 | if ($this->hasCompositeIndex() && $this->getRouteKeyName() == $this->getKeyName()) { |
||
| 289 | return $this->whereKey($value)->first(); |
||
| 290 | } else { |
||
| 291 | return $this->where($this->getRouteKeyName(), $value)->first(); |
||
| 292 | } |
||
| 293 | } |
||
| 294 | |||
| 295 | private function shouldProcessBinaryAttribute($key) |
||
| 296 | { |
||
| 297 | return $this->hexBinaryColumns() && in_array($key, $this->getBinaryColumns()); |
||
| 298 | } |
||
| 299 | |||
| 300 | public function hasGetMutator($key) |
||
| 301 | { |
||
| 302 | if ($this->shouldProcessBinaryAttribute($key) && isset($this->{$key})) { |
||
| 303 | return true; |
||
| 304 | } |
||
| 305 | |||
| 306 | return parent::hasGetMutator($key); |
||
| 307 | } |
||
| 308 | |||
| 309 | public function mutateAttribute($key, $value) |
||
| 310 | { |
||
| 311 | if ($this->shouldProcessBinaryAttribute($key)) { |
||
| 312 | return strtoupper(bin2hex($value)); |
||
| 313 | } |
||
| 314 | |||
| 315 | return parent::mutateAttribute($key, $value); |
||
| 316 | } |
||
| 317 | |||
| 318 | public function hasSetMutator($key) |
||
| 325 | } |
||
| 326 | |||
| 327 | public function setMutatedAttributeValue($key, $value) |
||
| 337 | } |
||
| 338 | } |
||
| 339 |