Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
5 / 5
CRAP
100.00% covered (success)
100.00%
1 / 1
EntityQueryFactory
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
5 / 5
6
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 createQueryFor
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 getDoctrineClass
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getOriginalClass
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 doCreateQuery
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2namespace Apie\DoctrineEntityDatalayer\Factories;
3
4use Apie\Core\BoundedContext\BoundedContextId;
5use Apie\Core\Datalayers\Search\QuerySearch;
6use Apie\Core\Entities\EntityInterface;
7use Apie\DoctrineEntityDatalayer\Query\EntityQuery;
8use Apie\DoctrineEntityDatalayer\Query\EntityQueryFilterInterface;
9use Doctrine\ORM\EntityManagerInterface;
10use ReflectionClass;
11
12final class EntityQueryFactory
13{
14    /**
15     * @var array<string, EntityQuery>
16     */
17    private array $alreadyLoaded = [];
18
19    /**
20     * @var array<int, EntityQueryFilterInterface>
21     */
22    private array $filters;
23    /**
24     * @param ReflectionClass<object> $doctrineEntityClass
25     */
26    public function __construct(
27        private readonly EntityManagerInterface $entityManager,
28        private readonly ReflectionClass $doctrineEntityClass,
29        private readonly BoundedContextId $boundedContextId,
30        EntityQueryFilterInterface... $filters
31    ) {
32        $this->filters = $filters;
33    }
34
35    public function createQueryFor(QuerySearch $querySearch): EntityQuery
36    {
37        $httpQuery = $querySearch->toHttpQuery();
38        if (!isset($this->alreadyLoaded[$httpQuery])) {
39            $this->alreadyLoaded[$httpQuery] = $this->doCreateQuery($querySearch);
40        }
41        return $this->alreadyLoaded[$httpQuery];
42    }
43
44    /**
45     * @return ReflectionClass<object>
46     */
47    public function getDoctrineClass(): ReflectionClass
48    {
49        return $this->doctrineEntityClass;
50    }
51
52    /**
53     * @return ReflectionClass<EntityInterface>
54     */
55    private function getOriginalClass(): ReflectionClass
56    {
57        return $this->doctrineEntityClass->getMethod('getClassReference')->invoke(null);
58    }
59
60    private function doCreateQuery(QuerySearch $querySearch): EntityQuery
61    {
62        return new EntityQuery(
63            $this->entityManager,
64            $this->getOriginalClass(),
65            $this->boundedContextId,
66            $querySearch,
67            ...$this->filters
68        );
69    }
70}