| Total Complexity | 40 |
| Total Lines | 272 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| Bugs | 1 | 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, OptionalBinaryTransformation; |
||
| 16 | |||
| 17 | /** |
||
| 18 | * Automatically generate unique binary id. |
||
| 19 | */ |
||
| 20 | public static function bootHasCompositePrimaryKey() |
||
| 21 | { |
||
| 22 | static::creating(function ($model) { |
||
| 23 | foreach ($model->getRawKeyName() as $key) { |
||
| 24 | if (!isset($model->{$key}) && in_array($key, $model->getBinaryColumns())) { |
||
| 25 | $v = uniqid(rand(), true); |
||
| 26 | $model->{$key} = $model->hexBinaryColumns() ? strtoupper( |
||
| 27 | md5($v) |
||
| 28 | ) : md5($v, true); |
||
| 29 | } |
||
| 30 | } |
||
| 31 | }); |
||
| 32 | } |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Destroy the models for the given IDs. |
||
| 36 | * |
||
| 37 | * @param array|int $ids |
||
| 38 | * |
||
| 39 | * @return int |
||
| 40 | */ |
||
| 41 | public static function destroy($ids) |
||
| 42 | { |
||
| 43 | // We'll initialize a count here so we will return the total number of deletes |
||
| 44 | // for the operation. The developers can then check this number as a boolean |
||
| 45 | // type value or get this total count of records deleted for logging, etc. |
||
| 46 | $count = 0; |
||
| 47 | |||
| 48 | $ids = is_array($ids) ? $ids : func_get_args(); |
||
| 49 | |||
| 50 | foreach ((new static())->applyIds($ids)->get() as $model) { |
||
|
|
|||
| 51 | if ($model->delete()) { |
||
| 52 | $count++; |
||
| 53 | } |
||
| 54 | } |
||
| 55 | |||
| 56 | return $count; |
||
| 57 | } |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Convert the object into something JSON serializable. |
||
| 61 | * |
||
| 62 | * @return array |
||
| 63 | */ |
||
| 64 | public function jsonSerialize() |
||
| 65 | { |
||
| 66 | $attributes = $this->toArray(); |
||
| 67 | foreach ($attributes as $key => $value) { |
||
| 68 | if (in_array($key, $this->getBinaryColumns())) { |
||
| 69 | $attributes[$key] = strtoupper(bin2hex($value)); |
||
| 70 | } |
||
| 71 | } |
||
| 72 | |||
| 73 | // append virtual row id |
||
| 74 | if (count($this->getRawKeyName()) > 1) { |
||
| 75 | $attributes[$this->getNormalizedKeyName()] = $this->getNormalizedKey(); |
||
| 76 | } |
||
| 77 | |||
| 78 | return $attributes; |
||
| 79 | } |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Get the primary key for the model. |
||
| 83 | * |
||
| 84 | * @return array |
||
| 85 | */ |
||
| 86 | public function getRawKeyName() |
||
| 87 | { |
||
| 88 | return $this->hasCompositeIndex() ? $this->primaryKey : [$this->primaryKey]; |
||
| 89 | } |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Get the value of the model's primary key. |
||
| 93 | * |
||
| 94 | * @return mixed |
||
| 95 | */ |
||
| 96 | public function getRawKey() |
||
| 97 | { |
||
| 98 | $attributes = []; |
||
| 99 | |||
| 100 | foreach ($this->getRawKeyName() as $key) { |
||
| 101 | $attributes[$key] = $this->getAttribute($key); |
||
| 102 | } |
||
| 103 | |||
| 104 | return $attributes; |
||
| 105 | } |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Get the value indicating whether the IDs are incrementing. |
||
| 109 | * |
||
| 110 | * @return bool |
||
| 111 | */ |
||
| 112 | public function getIncrementing() |
||
| 113 | { |
||
| 114 | return false; |
||
| 115 | } |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Get the primary key for the model. |
||
| 119 | * |
||
| 120 | * @return string |
||
| 121 | */ |
||
| 122 | public function getKeyName() |
||
| 123 | { |
||
| 124 | return $this->getNormalizedKeyName(); |
||
| 125 | } |
||
| 126 | |||
| 127 | /** |
||
| 128 | * Get virtual string key, required for proper collection support. |
||
| 129 | * |
||
| 130 | * @return mixed |
||
| 131 | */ |
||
| 132 | public function getKey() |
||
| 133 | { |
||
| 134 | return $this->getNormalizedKey(); |
||
| 135 | } |
||
| 136 | |||
| 137 | /** |
||
| 138 | * @param \Illuminate\Database\Query\Builder $query |
||
| 139 | * @param array|string $ids |
||
| 140 | * @param bool $inverse |
||
| 141 | * |
||
| 142 | * @throws MissingPrimaryKeyValueException |
||
| 143 | * @throws WrongKeyException |
||
| 144 | */ |
||
| 145 | public function scopeApplyIds($query, $ids, $inverse = false) |
||
| 146 | { |
||
| 147 | $keys = ($instance = new static())->getRawKeyName(); |
||
| 148 | |||
| 149 | if (!is_array($ids) || Arr::isAssoc($ids)) { |
||
| 150 | $ids = [$ids]; |
||
| 151 | } |
||
| 152 | |||
| 153 | if ($this->hasCompositeIndex()) { |
||
| 154 | (new CompositeKeyScope($keys, $ids, $inverse, $this->getBinaryColumns()))->apply($query); |
||
| 155 | } else { |
||
| 156 | //remap hex ID to binary ID even if index is not composite |
||
| 157 | if ($this->shouldProcessBinaryAttribute($keys[0])) { |
||
| 158 | $ids = array_map(function ($hex) { |
||
| 159 | return hex2bin($hex); |
||
| 160 | }, $ids); |
||
| 161 | } |
||
| 162 | if ($inverse) { |
||
| 163 | $query->whereNotIn($this->qualifyColumn($keys[0]), $ids); |
||
| 164 | } else { |
||
| 165 | $query->whereIn($this->qualifyColumn($keys[0]), $ids); |
||
| 166 | } |
||
| 167 | } |
||
| 168 | } |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Get a new query to restore one or more models by their queueable IDs. |
||
| 172 | * |
||
| 173 | * @param array|int $ids |
||
| 174 | * |
||
| 175 | *@throws WrongKeyException |
||
| 176 | * @throws MissingPrimaryKeyValueException |
||
| 177 | * |
||
| 178 | * @return Builder |
||
| 179 | */ |
||
| 180 | public function newQueryForRestoration($ids) |
||
| 192 | ) |
||
| 193 | ); |
||
| 194 | } |
||
| 195 | |||
| 196 | /** |
||
| 197 | * @param \Illuminate\Database\Query\Builder $query |
||
| 198 | * |
||
| 199 | * @return Builder|static |
||
| 200 | */ |
||
| 201 | public function newEloquentBuilder($query) |
||
| 202 | { |
||
| 203 | return new CompositeKeyQueryBuilder($query); |
||
| 204 | } |
||
| 205 | |||
| 206 | public function getBinaryColumns() |
||
| 207 | { |
||
| 208 | return $this->binaryColumns ?? []; |
||
| 209 | } |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Get the primary key value for a save query. |
||
| 213 | * |
||
| 214 | * @return mixed |
||
| 215 | */ |
||
| 216 | protected function getKeyForSaveQuery() |
||
| 221 | } |
||
| 222 | |||
| 223 | /** |
||
| 224 | * Set the keys for a save update query. |
||
| 225 | * |
||
| 226 | * @param Builder $query |
||
| 227 | * |
||
| 228 | *@throws MissingPrimaryKeyValueException |
||
| 229 | * |
||
| 230 | * @return Builder |
||
| 231 | */ |
||
| 232 | protected function setKeysForSaveQuery(Builder $query) |
||
| 243 | } |
||
| 244 | |||
| 245 | /** |
||
| 246 | * Run the increment or decrement method on the model. |
||
| 247 | * |
||
| 248 | * @param string $column |
||
| 249 | * @param float|int $amount |
||
| 250 | * @param array $extra |
||
| 251 | * @param string $method |
||
| 252 | * |
||
| 253 | * @return int |
||
| 254 | */ |
||
| 255 | protected function incrementOrDecrement($column, $amount, $extra, $method) |
||
| 270 | } |
||
| 271 | |||
| 272 | /** |
||
| 273 | * Retrieve the model for a bound value. |
||
| 274 | * |
||
| 275 | * @param mixed $value |
||
| 276 | * |
||
| 277 | * @return Model|null |
||
| 278 | */ |
||
| 279 | public function resolveRouteBinding($value) |
||
| 285 | } |
||
| 286 | } |
||
| 288 |