| Total Complexity | 41 |
| Total Lines | 157 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| Bugs | 1 | Features | 0 |
Complex classes like DynamicQuery 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 DynamicQuery, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 14 | class DynamicQuery |
||
| 15 | { |
||
| 16 | |||
| 17 | /** |
||
| 18 | * 删除动作 |
||
| 19 | * |
||
| 20 | * @author Dennis Lui <[email protected]> |
||
| 21 | * @param int $id |
||
| 22 | * @param string $type |
||
| 23 | * @return bool |
||
| 24 | */ |
||
| 25 | public static function delete(int $id, string $type): bool |
||
| 32 | } |
||
| 33 | |||
| 34 | /** |
||
| 35 | * 添加动作 |
||
| 36 | * |
||
| 37 | * @author Dennis Lui <[email protected]> |
||
| 38 | * @param array $data |
||
| 39 | * @return bool |
||
| 40 | */ |
||
| 41 | public static function create(array $data, string $type): bool |
||
| 42 | { |
||
| 43 | $model = self::convertModel($type); |
||
| 44 | list($sql, $file) = self::getFileSql($data, $type); |
||
| 45 | $item = $model::create($sql); |
||
| 46 | if ($file) { |
||
| 47 | $item->addMedia($file)->toMediaCollection($model::MEDIA_FILE); |
||
| 48 | } |
||
| 49 | self::addItems($data, $type, $item); |
||
| 50 | return !empty($item); |
||
| 51 | } |
||
| 52 | |||
| 53 | private static function addItems(array $data, string $type = 'attribute', DynamicUnitModel $dynamicUnit): void |
||
| 58 | } |
||
| 59 | } |
||
| 60 | } |
||
| 61 | |||
| 62 | private static function updateItems(array $data, string $type = 'attribute', DynamicUnitModel $dynamicUnit): void |
||
| 63 | { |
||
| 64 | if ($type == 'unit' && array_key_exists('items', $data) && $data['items']) { |
||
| 65 | self::removeUnderId($dynamicUnit, $data['items']); |
||
| 66 | foreach ($data['items'] as $rs) { |
||
| 67 | if (array_key_exists('id', $rs) && $rs['id']) { |
||
| 68 | self::update((int) $rs['id'], array_merge(['dynamic_unit_id' => $dynamicUnit->id], $rs), 'attribute'); |
||
| 69 | } else { |
||
| 70 | self::create(array_merge(['dynamic_unit_id' => $dynamicUnit->id], $rs), 'attribute'); |
||
| 71 | } |
||
| 72 | } |
||
| 73 | } |
||
| 74 | } |
||
| 75 | |||
| 76 | private static function getFileSql(array $data, string $type = 'attribute'): array |
||
| 77 | { |
||
| 78 | $file = null; |
||
| 79 | if ($type == 'attribute') { |
||
| 80 | $sql = self::convertAttributeSql($data); |
||
| 81 | $file = self::pullFile($data); |
||
| 82 | } else { |
||
| 83 | $sql = self::convertUnitSql($data); |
||
| 84 | } |
||
| 85 | return [$sql, $file]; |
||
| 86 | } |
||
| 87 | |||
| 88 | /** |
||
| 89 | * 修改动作 |
||
| 90 | * |
||
| 91 | * @author Dennis Lui <[email protected]> |
||
| 92 | * @param int $id |
||
| 93 | * @param array $data |
||
| 94 | * @return bool |
||
| 95 | */ |
||
| 96 | public static function update(int $id, array $data, string $type): bool |
||
| 97 | { |
||
| 98 | $model = self::convertModel($type); |
||
| 99 | if (!$item = $model::find($id)) { |
||
| 100 | return false; |
||
| 101 | } |
||
| 102 | list($sql, $file) = self::getFileSql($data, $type); |
||
| 103 | $item->fill($sql); |
||
| 104 | $result = $item->save(); |
||
| 105 | if ($file) { |
||
| 106 | $item->media && $item->media->each(fn($media) => $media->delete()); |
||
| 107 | $item->addMedia($file)->toMediaCollection($model::MEDIA_FILE); |
||
| 108 | } |
||
| 109 | self::addItems($data, $type, $item); |
||
| 110 | return $result; |
||
| 111 | } |
||
| 112 | |||
| 113 | private static function removeUnderId(DynamicUnitModel $item, array $array): void |
||
| 114 | { |
||
| 115 | $collection = new Collection($array); |
||
| 116 | $idList = $collection->filter(fn($n) => isset ($n['id']) && !empty ($n['id']))->values()->pluck('id')->toArray(); |
||
| 117 | if ($idList) { |
||
| 118 | $item->items()->whereNotIn('id', $idList)->delete(); |
||
| 119 | } |
||
| 120 | } |
||
| 121 | |||
| 122 | private static function convertModel(string $type) |
||
| 123 | { |
||
| 124 | return $type == 'attribute' ? DynamicAttribute::class : DynamicUnitModel::class; |
||
| 125 | } |
||
| 126 | |||
| 127 | |||
| 128 | private static function convertSql(array $data): array|bool |
||
| 129 | { |
||
| 130 | $sql = []; |
||
| 131 | if (array_key_exists('name', $data) && trim($data['name'])) { |
||
| 132 | $sql['name'] = trim($data['name']); |
||
| 133 | } |
||
| 134 | if (array_key_exists('code', $data) && trim($data['code'])) { |
||
| 135 | $sql['code'] = trim($data['code']); |
||
| 136 | } |
||
| 137 | if (!$sql) |
||
| 138 | return false; |
||
| 139 | $file = array_key_exists("file", $data) ? $data['file'] : null; |
||
| 140 | $id = array_key_exists("id", $data) ? $data['id'] : null; |
||
| 141 | return [$sql, $file, $id]; |
||
| 142 | } |
||
| 143 | |||
| 144 | private static function pullFile(array $data): null|UploadedFile |
||
| 150 | } |
||
| 151 | |||
| 152 | private static function convertAttributeSql(array $data): array |
||
| 153 | { |
||
| 154 | $sql = array_merge([ |
||
| 155 | 'dynamic_unit_id' => 0, |
||
| 156 | 'name' => null, |
||
| 157 | 'code' => null |
||
| 158 | ], $data); |
||
| 159 | $collection = new Collection($sql); |
||
| 160 | return $collection->only(['dynamic_unit_id', 'name', 'code'])->toArray(); |
||
| 161 | } |
||
| 162 | |||
| 163 | private static function convertUnitSql(array $data): array |
||
| 171 | } |
||
| 172 | } |