Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
31 / 31
100.00% covered (success)
100.00%
8 / 8
CRAP
100.00% covered (success)
100.00%
1 / 1
FakerDatalayer
100.00% covered (success)
100.00%
31 / 31
100.00% covered (success)
100.00%
8 / 8
9
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
 all
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
1
 find
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 getMetadata
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
1 / 1
1
 persistNew
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
2
 persistExisting
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 removeExisting
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 upsert
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2namespace Apie\Faker\Datalayers;
3
4use Apie\Core\BoundedContext\BoundedContextId;
5use Apie\Core\Datalayers\ApieDatalayerWithFilters;
6use Apie\Core\Datalayers\Concerns\FiltersOnAllFields;
7use Apie\Core\Datalayers\DataLayerMetadata;
8use Apie\Core\Datalayers\Lists\EntityListInterface;
9use Apie\Core\Datalayers\Search\LazyLoadedListFilterer;
10use Apie\Core\Entities\EntityInterface;
11use Apie\Core\Identifiers\AutoIncrementInteger;
12use Apie\Core\Identifiers\IdentifierInterface;
13use Apie\Core\IdentifierUtils;
14use DateTimeImmutable;
15use Faker\Generator;
16use ReflectionClass;
17
18class FakerDatalayer implements ApieDatalayerWithFilters
19{
20    use FiltersOnAllFields;
21
22    public function __construct(private readonly Generator $faker, private readonly LazyLoadedListFilterer $filterer)
23    {
24    }
25
26    public function all(ReflectionClass $class, ?BoundedContextId $boundedContextId = null): EntityListInterface
27    {
28        return new FakeEntityList(
29            $class,
30            $boundedContextId ?? new BoundedContextId('unknown'),
31            $this->filterer,
32            $this->faker
33        );
34    }
35
36    public function find(IdentifierInterface $identifier, ?BoundedContextId $boundedContextId = null): EntityInterface
37    {
38        $class = IdentifierUtils::identifierToEntityClass($identifier);
39        $object = $this->faker->fakeClass($class->name);
40        IdentifierUtils::injectIdentifier($object, $identifier);
41        return $object;
42    }
43
44    /**
45     * @template T of EntityInterface
46     * @param IdentifierInterface<T> $identifier
47     */
48    public function getMetadata(
49        IdentifierInterface $identifier,
50        ?BoundedContextId $boundedContextId = null
51    ): DataLayerMetadata {
52        $createdAt = DateTimeImmutable::createFromMutable(
53            $this->faker->dateTimeBetween('-5 years', 'now')
54        );
55        $updatedAt = DateTimeImmutable::createFromMutable(
56            $this->faker->dateTimeBetween($createdAt->format('Y-m-d H:i:s'), 'now')
57        );
58
59        return new DataLayerMetadata(
60            $createdAt,
61            $updatedAt,
62            $this->faker->numerify('#.#.#'),
63            $this->faker->numerify('#.#.#')
64        );
65    }
66
67    public function persistNew(EntityInterface $entity, ?BoundedContextId $boundedContextId = null): EntityInterface
68    {
69        $identifier = $entity->getId();
70        if ($identifier instanceof AutoIncrementInteger) {
71            $className = get_class($identifier);
72            IdentifierUtils::injectIdentifier($entity, $className::createRandom($this->faker));
73        }
74        return $entity;
75    }
76
77    public function persistExisting(EntityInterface $entity, ?BoundedContextId $boundedContextId = null): EntityInterface
78    {
79        return $entity;
80    }
81
82    public function removeExisting(EntityInterface $entity, ?BoundedContextId $boundedContextId = null): void
83    {
84    }
85
86    public function upsert(EntityInterface $entity, ?BoundedContextId $boundedContextId): EntityInterface
87    {
88        return $this->persistNew($entity, $boundedContextId);
89    }
90}