| Total Complexity | 43 |
| Total Lines | 336 |
| Duplicated Lines | 0 % |
| Changes | 3 | ||
| Bugs | 0 | Features | 0 |
Complex classes like QueryService 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 QueryService, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 21 | class QueryService implements QueryServiceInterface |
||
| 22 | { |
||
| 23 | /** |
||
| 24 | * @var class-string |
||
| 25 | */ |
||
| 26 | protected string $entityName; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @var EntityManagerInterface |
||
| 30 | */ |
||
| 31 | protected EntityManagerInterface $entityManager; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @var LoggerInterface |
||
| 35 | */ |
||
| 36 | protected LoggerInterface $logger; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @param class-string $entityName |
||
| 40 | * @param EntityManagerInterface $entityManager |
||
| 41 | * @param LoggerInterface $logger |
||
| 42 | */ |
||
| 43 | public function __construct(string $entityName, EntityManagerInterface $entityManager, LoggerInterface $logger) |
||
| 44 | { |
||
| 45 | $this->entityName = $entityName; |
||
| 46 | $this->entityManager = $entityManager; |
||
| 47 | $this->logger = $logger; |
||
| 48 | } |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @return string |
||
| 52 | */ |
||
| 53 | public function getEntityName(): string |
||
| 54 | { |
||
| 55 | return $this->entityName; |
||
| 56 | } |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @param object|AbstractQuery|QueryBuilder $queryOrBuilder |
||
| 60 | * @param array<string, mixed> $options |
||
| 61 | * |
||
| 62 | * @return EntityInterface|null|array<mixed> |
||
| 63 | * |
||
| 64 | * @throws QueryServiceException |
||
| 65 | */ |
||
| 66 | public function getSingleResultOrNull(object $queryOrBuilder, array $options = []) |
||
| 83 | } |
||
| 84 | |||
| 85 | /** |
||
| 86 | * @param object|AbstractQuery|QueryBuilder $queryOrBuilder |
||
| 87 | * @param array<string, mixed> $options |
||
| 88 | * |
||
| 89 | * @return int|mixed|string |
||
| 90 | * |
||
| 91 | * @throws QueryServiceException |
||
| 92 | */ |
||
| 93 | public function getSingleScalarResult(object $queryOrBuilder, array $options = []) |
||
| 94 | { |
||
| 95 | try { |
||
| 96 | return $this->getQuery($queryOrBuilder, $options)->getSingleScalarResult(); |
||
| 97 | } catch (QueryServiceException $e) { |
||
| 98 | throw $e; |
||
| 99 | } catch (\Exception $e) { |
||
| 100 | $message = sprintf( |
||
| 101 | 'An error occurred while loading fetching a single scalar result: %s', |
||
| 102 | $e->getMessage() |
||
| 103 | ); |
||
| 104 | |||
| 105 | $this->logger->error($message, ['exception' => $e]); |
||
| 106 | |||
| 107 | throw new QueryServiceException($message, $e->getCode(), $e); |
||
| 108 | } |
||
| 109 | } |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Construct and execute the query. |
||
| 113 | * |
||
| 114 | * @param object|AbstractQuery|QueryBuilder $queryOrBuilder |
||
| 115 | * @param array<string, mixed> $options |
||
| 116 | * |
||
| 117 | * @return mixed |
||
| 118 | * |
||
| 119 | * @throws QueryServiceException |
||
| 120 | */ |
||
| 121 | public function execute(object $queryOrBuilder, array $options = []) |
||
| 122 | { |
||
| 123 | try { |
||
| 124 | return $this->getQuery($queryOrBuilder, $options)->execute(); |
||
| 125 | } catch (QueryServiceException $e) { |
||
| 126 | throw $e; |
||
| 127 | } catch (\Exception $e) { |
||
| 128 | $message = sprintf('Failed to execute query : %s', $e->getMessage()); |
||
| 129 | |||
| 130 | $this->logger->error($message, ['exception' => $e]); |
||
| 131 | |||
| 132 | throw new QueryServiceException($message, $e->getCode(), $e); |
||
| 133 | } |
||
| 134 | } |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Find a single entity matching the provided identity. |
||
| 138 | * |
||
| 139 | * @param mixed $id The identity of the entity to match. |
||
| 140 | * @param array<string, mixed> $options The optional query options. |
||
| 141 | * |
||
| 142 | * @return EntityInterface|null |
||
| 143 | * |
||
| 144 | * @throws QueryServiceException |
||
| 145 | */ |
||
| 146 | public function findOneById($id, array $options = []): ?EntityInterface |
||
| 149 | } |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Find a single entity matching the provided criteria. |
||
| 153 | * |
||
| 154 | * @param array<string, mixed> $criteria The search criteria that should be matched on. |
||
| 155 | * @param array<string, mixed> $options The optional query options. |
||
| 156 | * |
||
| 157 | * @return EntityInterface|null |
||
| 158 | * |
||
| 159 | * @throws QueryServiceException |
||
| 160 | */ |
||
| 161 | public function findOne(array $criteria, array $options = []): ?EntityInterface |
||
| 162 | { |
||
| 163 | try { |
||
| 164 | $persist = $this->entityManager->getUnitOfWork()->getEntityPersister($this->entityName); |
||
| 165 | |||
| 166 | $entity = $persist->load( |
||
| 167 | $criteria, |
||
| 168 | $options[QueryServiceOption::ENTITY] ?? null, |
||
| 169 | $options[QueryServiceOption::ASSOCIATION] ?? null, |
||
| 170 | $options[QueryServiceOption::HINTS] ?? [], |
||
| 171 | $options[QueryServiceOption::LOCK_MODE] ?? null, |
||
| 172 | 1, |
||
| 173 | $options[QueryServiceOption::ORDER_BY] ?? null |
||
| 174 | ); |
||
| 175 | |||
| 176 | return ($entity instanceof EntityInterface) ? $entity : null; |
||
| 177 | } catch (\Exception $e) { |
||
| 178 | $message = sprintf('Failed to execute \'findOne\' query: %s', $e->getMessage()); |
||
| 179 | |||
| 180 | $this->logger->error($message, ['exception' => $e, 'criteria' => $criteria, 'options' => $options]); |
||
| 181 | |||
| 182 | throw new QueryServiceException($message, $e->getCode(), $e); |
||
| 183 | } |
||
| 184 | } |
||
| 185 | |||
| 186 | /** |
||
| 187 | * Find a collection of entities that match the provided criteria. |
||
| 188 | * |
||
| 189 | * @param array<string, mixed> $criteria The search criteria that should be matched on. |
||
| 190 | * @param array<string, mixed> $options The optional query options. |
||
| 191 | * |
||
| 192 | * @return iterable<EntityInterface> |
||
| 193 | * |
||
| 194 | * @throws QueryServiceException |
||
| 195 | */ |
||
| 196 | public function findMany(array $criteria, array $options = []): iterable |
||
| 213 | } |
||
| 214 | } |
||
| 215 | |||
| 216 | /** |
||
| 217 | * Return the result set count. |
||
| 218 | * |
||
| 219 | * @param array<string, mixed> $criteria |
||
| 220 | * |
||
| 221 | * @return int |
||
| 222 | */ |
||
| 223 | public function count(array $criteria): int |
||
| 224 | { |
||
| 225 | $unitOfWork = $this->entityManager->getUnitOfWork(); |
||
| 226 | |||
| 227 | return $unitOfWork->getEntityPersister($this->entityName)->count($criteria); |
||
| 228 | } |
||
| 229 | |||
| 230 | /** |
||
| 231 | * Set the query builder options. |
||
| 232 | * |
||
| 233 | * @param QueryBuilder $queryBuilder The query builder to update. |
||
| 234 | * @param array<string, mixed> $options The query builder options to set. |
||
| 235 | * |
||
| 236 | * @return QueryBuilder |
||
| 237 | */ |
||
| 238 | protected function prepareQueryBuilder(QueryBuilder $queryBuilder, array $options = []): QueryBuilder |
||
| 258 | } |
||
| 259 | |||
| 260 | /** |
||
| 261 | * Prepare the provided query by setting the $options. |
||
| 262 | * |
||
| 263 | * @param AbstractQuery $query |
||
| 264 | * @param array<string, mixed> $options |
||
| 265 | * |
||
| 266 | * @return AbstractQuery |
||
| 267 | * |
||
| 268 | * @throws QueryServiceException |
||
| 269 | */ |
||
| 270 | protected function prepareQuery(AbstractQuery $query, array $options = []): AbstractQuery |
||
| 271 | { |
||
| 272 | if (isset($options['params'])) { |
||
| 273 | $query->setParameters($options['params']); |
||
| 274 | } |
||
| 275 | |||
| 276 | if (isset($options[QueryServiceOption::HYDRATION_MODE])) { |
||
| 277 | $query->setHydrationMode($options[QueryServiceOption::HYDRATION_MODE]); |
||
| 278 | } |
||
| 279 | |||
| 280 | if (isset($options['result_set_mapping'])) { |
||
| 281 | $query->setResultSetMapping($options['result_set_mapping']); |
||
| 282 | } |
||
| 283 | |||
| 284 | if (isset($options[QueryServiceOption::HINTS]) && is_array($options[QueryServiceOption::HINTS])) { |
||
| 285 | foreach ($options[QueryServiceOption::HINTS] as $hint => $hintValue) { |
||
| 286 | $query->setHint($hint, $hintValue); |
||
| 287 | } |
||
| 288 | } |
||
| 289 | |||
| 290 | if ($query instanceof Query) { |
||
| 291 | if (!empty($options[QueryServiceOption::DQL])) { |
||
| 292 | $query->setDQL($options[QueryServiceOption::DQL]); |
||
| 293 | } |
||
| 294 | |||
| 295 | if (isset($options[QueryServiceOption::LOCK_MODE])) { |
||
| 296 | try { |
||
| 297 | $query->setLockMode($options[QueryServiceOption::LOCK_MODE]); |
||
| 298 | } catch (TransactionRequiredException $e) { |
||
| 299 | throw new QueryServiceException($e->getMessage(), $e->getCode(), $e); |
||
| 300 | } |
||
| 301 | } |
||
| 302 | } |
||
| 303 | |||
| 304 | return $query; |
||
| 305 | } |
||
| 306 | |||
| 307 | /** |
||
| 308 | * Return a new query builder instance. |
||
| 309 | * |
||
| 310 | * @param string|null $alias The optional query builder alias. |
||
| 311 | * |
||
| 312 | * @return QueryBuilder |
||
| 313 | */ |
||
| 314 | public function createQueryBuilder(string $alias = null): QueryBuilder |
||
| 315 | { |
||
| 316 | $queryBuilder = $this->entityManager->createQueryBuilder(); |
||
| 317 | |||
| 318 | if (null !== $alias) { |
||
| 319 | $queryBuilder->select($alias)->from($this->entityName, $alias); |
||
| 320 | } |
||
| 321 | |||
| 322 | return $queryBuilder; |
||
| 323 | } |
||
| 324 | |||
| 325 | /** |
||
| 326 | * Resolve the ORM Query instance for a QueryBuilder and set the optional $options |
||
| 327 | * |
||
| 328 | * @param object|AbstractQuery|QueryBuilder $queryOrBuilder |
||
| 329 | * @param array<mixed> $options |
||
| 330 | * |
||
| 331 | * @return AbstractQuery |
||
| 332 | * |
||
| 333 | * @throws QueryServiceException |
||
| 334 | */ |
||
| 335 | private function getQuery(object $queryOrBuilder, array $options = []): AbstractQuery |
||
| 357 | } |
||
| 358 | } |
||
| 359 |