| Total Complexity | 59 |
| Total Lines | 438 |
| Duplicated Lines | 0 % |
| Changes | 5 | ||
| Bugs | 1 | Features | 0 |
Complex classes like Repository 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 Repository, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 68 | class Repository implements RepositoryInterface |
||
| 69 | { |
||
| 70 | /** |
||
| 71 | * The list of relation to load with the query |
||
| 72 | * @var array<int, string>|array<string, Closure> |
||
| 73 | */ |
||
| 74 | protected array $with = []; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Whether need load relation data immediately |
||
| 78 | * @var bool |
||
| 79 | */ |
||
| 80 | protected bool $immediate = false; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * The order by column(s) |
||
| 84 | * @var string|Closure|Expression|string[]|Expression[]|Closure[] |
||
| 85 | */ |
||
| 86 | protected string|Closure|Expression|array $orderColumns = ''; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * The order direction |
||
| 90 | * @var string |
||
| 91 | */ |
||
| 92 | protected string $orderDir = 'ASC'; |
||
| 93 | |||
| 94 | /** |
||
| 95 | * The offset to use |
||
| 96 | * @var int |
||
| 97 | */ |
||
| 98 | protected int $offset = -1; |
||
| 99 | |||
| 100 | /** |
||
| 101 | * The limit to use |
||
| 102 | * @var int |
||
| 103 | */ |
||
| 104 | protected int $limit = 0; |
||
| 105 | |||
| 106 | /** |
||
| 107 | * The filters list |
||
| 108 | * @var array<string, mixed> |
||
| 109 | */ |
||
| 110 | protected array $filters = []; |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Create new instance |
||
| 114 | * @param EntityManager<TEntity> $manager |
||
| 115 | * @param class-string<TEntity> $entityClass |
||
|
|
|||
| 116 | */ |
||
| 117 | public function __construct( |
||
| 118 | protected EntityManager $manager, |
||
| 119 | protected string $entityClass |
||
| 120 | ) { |
||
| 121 | } |
||
| 122 | |||
| 123 | /** |
||
| 124 | * {@inheritedoc} |
||
| 125 | */ |
||
| 126 | public function query(string|array $with = [], bool $immediate = false): EntityQuery |
||
| 160 | } |
||
| 161 | |||
| 162 | |||
| 163 | /** |
||
| 164 | * {@inheritedoc} |
||
| 165 | */ |
||
| 166 | public function with(string|array $with, bool $immediate = false): self |
||
| 167 | { |
||
| 168 | if (!is_array($with)) { |
||
| 169 | $with = [$with]; |
||
| 170 | } |
||
| 171 | $this->with = $with; |
||
| 172 | $this->immediate = $immediate; |
||
| 173 | |||
| 174 | return $this; |
||
| 175 | } |
||
| 176 | |||
| 177 | /** |
||
| 178 | * {@inheritedoc} |
||
| 179 | */ |
||
| 180 | public function orderBy( |
||
| 181 | string|Closure|Expression|array $columns, |
||
| 182 | string $order = 'ASC' |
||
| 183 | ): self { |
||
| 184 | $this->orderColumns = $columns; |
||
| 185 | $this->orderDir = $order; |
||
| 186 | |||
| 187 | return $this; |
||
| 188 | } |
||
| 189 | |||
| 190 | /** |
||
| 191 | * {@inheritedoc} |
||
| 192 | */ |
||
| 193 | public function limit(int $offset, int $limit): self |
||
| 194 | { |
||
| 195 | $this->offset = $offset; |
||
| 196 | $this->limit = $limit; |
||
| 197 | |||
| 198 | return $this; |
||
| 199 | } |
||
| 200 | |||
| 201 | /** |
||
| 202 | * {@inheritedoc} |
||
| 203 | */ |
||
| 204 | public function filters(string|array $filters = []): self |
||
| 205 | { |
||
| 206 | if (is_string($filters)) { |
||
| 207 | $filters = [$filters => true]; |
||
| 208 | } |
||
| 209 | |||
| 210 | $this->filters = $filters; |
||
| 211 | |||
| 212 | return $this; |
||
| 213 | } |
||
| 214 | |||
| 215 | /** |
||
| 216 | * {@inheritedoc} |
||
| 217 | * @return TEntity[] |
||
| 218 | */ |
||
| 219 | public function all(array $columns = []): array |
||
| 220 | { |
||
| 221 | return $this->query()->all($columns); |
||
| 222 | } |
||
| 223 | |||
| 224 | /** |
||
| 225 | * {@inheritedoc} |
||
| 226 | */ |
||
| 227 | public function create(array $columns = []): Entity |
||
| 228 | { |
||
| 229 | $mapper = $this->manager->getEntityMapper($this->entityClass); |
||
| 230 | |||
| 231 | return new $this->entityClass( |
||
| 232 | $this->manager, |
||
| 233 | $mapper, |
||
| 234 | $columns, |
||
| 235 | [], |
||
| 236 | false, |
||
| 237 | true |
||
| 238 | ); |
||
| 239 | } |
||
| 240 | |||
| 241 | /** |
||
| 242 | * {@inheritedoc} |
||
| 243 | * @return TEntity|null |
||
| 244 | */ |
||
| 245 | public function find(array|string|int|float $id): ?Entity |
||
| 246 | { |
||
| 247 | return $this->query()->find($id); |
||
| 248 | } |
||
| 249 | |||
| 250 | /** |
||
| 251 | * {@inheritedoc} |
||
| 252 | */ |
||
| 253 | public function findBy(array $conditions): ?Entity |
||
| 254 | { |
||
| 255 | $query = $this->query(); |
||
| 256 | foreach ($conditions as $name => $value) { |
||
| 257 | $query->where($name)->is($value); |
||
| 258 | } |
||
| 259 | return $query->get(); |
||
| 260 | } |
||
| 261 | |||
| 262 | /** |
||
| 263 | * {@inheritedoc} |
||
| 264 | */ |
||
| 265 | public function findAll(mixed ...$ids): array |
||
| 266 | { |
||
| 267 | return $this->query()->findAll(...$ids); |
||
| 268 | } |
||
| 269 | |||
| 270 | /** |
||
| 271 | * {@inheritedoc} |
||
| 272 | */ |
||
| 273 | public function findAllBy(array $conditions): array |
||
| 280 | } |
||
| 281 | |||
| 282 | /** |
||
| 283 | * {@inheritedoc} |
||
| 284 | */ |
||
| 285 | public function save(Entity $entity): array|string|int|float|bool|null |
||
| 286 | { |
||
| 287 | $data = Proxy::instance()->getEntityDataMapper($entity); |
||
| 288 | |||
| 289 | if ($data->isNew()) { |
||
| 290 | return (bool) $this->insert($entity); |
||
| 291 | } |
||
| 292 | |||
| 293 | return $this->update($entity); |
||
| 294 | } |
||
| 295 | |||
| 296 | /** |
||
| 297 | * {@inheritedoc} |
||
| 298 | */ |
||
| 299 | public function insert(Entity $entity): array|string|int|float|false|null |
||
| 358 | } |
||
| 359 | |||
| 360 | /** |
||
| 361 | * {@inheritedoc} |
||
| 362 | */ |
||
| 363 | public function update(Entity $entity): bool |
||
| 364 | { |
||
| 365 | $data = Proxy::instance()->getEntityDataMapper($entity); |
||
| 366 | $mapper = $data->getEntityMapper(); |
||
| 367 | $eventsHandlers = $mapper->getEventHandlers(); |
||
| 368 | |||
| 369 | if ($data->isDeleted()) { |
||
| 370 | throw new EntityStateException('The record was deleted'); |
||
| 371 | } |
||
| 372 | |||
| 373 | if ($data->isNew()) { |
||
| 374 | throw new EntityStateException('Can\'t update an unsaved entity'); |
||
| 375 | } |
||
| 376 | |||
| 377 | if (!$data->wasModified()) { |
||
| 378 | return true; |
||
| 379 | } |
||
| 380 | |||
| 381 | $modified = $data->getModifiedColumns(); |
||
| 382 | if (!empty($modified)) { |
||
| 383 | $connection = $this->manager->getConnection(); |
||
| 384 | $result = $connection->transaction(function (Connection $connection) use ($data, $mapper, $modified) { |
||
| 385 | $columns = array_intersect_key($data->getRawColumns(), array_flip($modified)); |
||
| 386 | |||
| 387 | $updatedAt = null; |
||
| 388 | |||
| 389 | if ($mapper->hasTimestamp()) { |
||
| 390 | list(, $updatedAtCol) = $mapper->getTimestampColumns(); |
||
| 391 | $columns[$updatedAtCol] = $updatedAt = date($this->manager->getDateFormat()); |
||
| 392 | } |
||
| 393 | $data->markAsUpdated($updatedAt); |
||
| 394 | |||
| 395 | $update = new Update($connection, $mapper->getTable()); |
||
| 396 | |||
| 397 | $primaryKeys = $mapper->getPrimaryKey()->getValue($data->getRawColumns(), true); |
||
| 398 | if (is_array($primaryKeys)) { |
||
| 399 | foreach ($primaryKeys as $pkColumn => $pkValue) { |
||
| 400 | $update->where($pkColumn)->is($pkValue); |
||
| 401 | } |
||
| 402 | } |
||
| 403 | |||
| 404 | return $update->set($columns) >= 0; |
||
| 405 | }); |
||
| 406 | |||
| 407 | if ($result === false) { |
||
| 408 | return false; |
||
| 409 | } |
||
| 410 | |||
| 411 | if (isset($eventsHandlers['update'])) { |
||
| 412 | foreach ($eventsHandlers['update'] as $callback) { |
||
| 413 | $callback($entity, $data); |
||
| 414 | } |
||
| 415 | } |
||
| 416 | |||
| 417 | return true; |
||
| 418 | } |
||
| 419 | |||
| 420 | $connection = $this->manager->getConnection(); |
||
| 421 | return $connection->transaction(function (Connection $connection) use ($data) { |
||
| 422 | $data->executePendingLinkage(); |
||
| 423 | |||
| 424 | return true; |
||
| 425 | }); |
||
| 426 | } |
||
| 427 | |||
| 428 | /** |
||
| 429 | * {@inheritedoc} |
||
| 430 | */ |
||
| 431 | public function delete(Entity $entity, bool $force = false): bool |
||
| 472 | } |
||
| 473 | |||
| 474 | /** |
||
| 475 | * {@inheritedoc} |
||
| 476 | */ |
||
| 477 | public function exists(array $conditions): bool |
||
| 478 | { |
||
| 479 | return $this->findBy($conditions) !== null; |
||
| 480 | } |
||
| 481 | |||
| 482 | /** |
||
| 483 | * {@inheritedoc} |
||
| 484 | */ |
||
| 485 | public function existIgnore(array $conditions, mixed $value, string $field = 'id'): bool |
||
| 490 | } |
||
| 491 | |||
| 492 | /** |
||
| 493 | * Set the filters |
||
| 494 | * @param EntityQuery<TEntity> $query |
||
| 495 | * @return $this |
||
| 496 | */ |
||
| 497 | protected function setFilters(EntityQuery $query): self |
||
| 506 | } |
||
| 507 | } |
||
| 508 |