Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
94.74% |
18 / 19 |
|
85.71% |
6 / 7 |
CRAP | |
0.00% |
0 / 1 |
| FakerDatalayer | |
94.74% |
18 / 19 |
|
85.71% |
6 / 7 |
8.01 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| all | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
1 | |||
| find | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
| persistNew | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
2 | |||
| persistExisting | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| removeExisting | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| upsert | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | namespace Apie\Faker\Datalayers; |
| 3 | |
| 4 | use Apie\Core\BoundedContext\BoundedContextId; |
| 5 | use Apie\Core\Datalayers\ApieDatalayerWithFilters; |
| 6 | use Apie\Core\Datalayers\Concerns\FiltersOnAllFields; |
| 7 | use Apie\Core\Datalayers\Lists\EntityListInterface; |
| 8 | use Apie\Core\Datalayers\Search\LazyLoadedListFilterer; |
| 9 | use Apie\Core\Entities\EntityInterface; |
| 10 | use Apie\Core\Identifiers\AutoIncrementInteger; |
| 11 | use Apie\Core\Identifiers\IdentifierInterface; |
| 12 | use Apie\Core\IdentifierUtils; |
| 13 | use Faker\Generator; |
| 14 | use ReflectionClass; |
| 15 | |
| 16 | class FakerDatalayer implements ApieDatalayerWithFilters |
| 17 | { |
| 18 | use FiltersOnAllFields; |
| 19 | |
| 20 | public function __construct(private readonly Generator $faker, private readonly LazyLoadedListFilterer $filterer) |
| 21 | { |
| 22 | } |
| 23 | |
| 24 | public function all(ReflectionClass $class, ?BoundedContextId $boundedContextId = null): EntityListInterface |
| 25 | { |
| 26 | return new FakeEntityList( |
| 27 | $class, |
| 28 | $boundedContextId ?? new BoundedContextId('unknown'), |
| 29 | $this->filterer, |
| 30 | $this->faker |
| 31 | ); |
| 32 | } |
| 33 | |
| 34 | public function find(IdentifierInterface $identifier, ?BoundedContextId $boundedContextId = null): EntityInterface |
| 35 | { |
| 36 | $class = IdentifierUtils::identifierToEntityClass($identifier); |
| 37 | $object = $this->faker->fakeClass($class->name); |
| 38 | IdentifierUtils::injectIdentifier($object, $identifier); |
| 39 | return $object; |
| 40 | } |
| 41 | |
| 42 | public function persistNew(EntityInterface $entity, ?BoundedContextId $boundedContextId = null): EntityInterface |
| 43 | { |
| 44 | $identifier = $entity->getId(); |
| 45 | if ($identifier instanceof AutoIncrementInteger) { |
| 46 | $className = get_class($identifier); |
| 47 | IdentifierUtils::injectIdentifier($entity, $className::createRandom($this->faker)); |
| 48 | } |
| 49 | return $entity; |
| 50 | } |
| 51 | |
| 52 | public function persistExisting(EntityInterface $entity, ?BoundedContextId $boundedContextId = null): EntityInterface |
| 53 | { |
| 54 | return $entity; |
| 55 | } |
| 56 | |
| 57 | public function removeExisting(EntityInterface $entity, ?BoundedContextId $boundedContextId = null): void |
| 58 | { |
| 59 | } |
| 60 | |
| 61 | public function upsert(EntityInterface $entity, ?BoundedContextId $boundedContextId): EntityInterface |
| 62 | { |
| 63 | return $this->persistNew($entity, $boundedContextId); |
| 64 | } |
| 65 | } |