Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
93.10% covered (success)
93.10%
54 / 58
75.00% covered (warning)
75.00%
3 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
Dataset
93.10% covered (success)
93.10%
54 / 58
75.00% covered (warning)
75.00%
3 / 4
11.04
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getId
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 createRandom
100.00% covered (success)
100.00%
13 / 13
100.00% covered (success)
100.00%
1 / 1
1
 toDatalayer
90.48% covered (success)
90.48%
38 / 42
0.00% covered (danger)
0.00%
0 / 1
8.06
1<?php
2namespace Apie\Dataset;
3
4use Apie\Core\Attributes\FakeCount;
5use Apie\Core\Attributes\FakeMethod;
6use Apie\Core\Attributes\StoreOptions;
7use Apie\Core\BoundedContext\BoundedContext;
8use Apie\Core\BoundedContext\BoundedContextHashmap;
9use Apie\Core\BoundedContext\BoundedContextId;
10use Apie\Core\Entities\EntityInterface;
11use Apie\Core\FileStorage\FileStorageFactory;
12use Apie\Core\FileStorage\SqliteFile;
13use Apie\Core\Indexing\Indexer;
14use Apie\Core\Lists\ReflectionClassList;
15use Apie\Core\Lists\ReflectionMethodList;
16use Apie\Dataset\DummyApp\Resources\DummyUser;
17use Apie\DoctrineEntityConverter\Factories\PersistenceLayerFactory;
18use Apie\DoctrineEntityConverter\OrmBuilder as DoctrineEntityConverterOrmBuilder;
19use Apie\DoctrineEntityDatalayer\DoctrineEntityDatalayer;
20use Apie\DoctrineEntityDatalayer\EntityReindexer;
21use Apie\DoctrineEntityDatalayer\Factories\DoctrineListFactory;
22use Apie\DoctrineEntityDatalayer\Factories\EntityQueryFilterFactory;
23use Apie\DoctrineEntityDatalayer\IndexStrategy\DirectIndexStrategy;
24use Apie\DoctrineEntityDatalayer\OrmBuilder;
25use Apie\StorageMetadata\DomainToStorageConverter;
26
27#[FakeMethod('createRandom')]
28#[FakeCount(1)]
29final class Dataset implements EntityInterface
30{
31    private DatasetIdentifier $id;
32
33    #[StoreOptions(noStorage: true)]
34    private ?DoctrineEntityDatalayer $datalayer = null;
35
36    protected SqliteFile $sqliteFile;
37
38    public function __construct(
39        #[StoreOptions(alwaysMixedData: true)]
40        protected BoundedContextHashmap $hashmap
41    ) {
42        $this->id = DatasetIdentifier::createRandom();
43        $this->sqliteFile = SqliteFile::createRandom();
44    }
45
46    public function getId(): DatasetIdentifier
47    {
48        return $this->id;
49    }
50
51    public static function createRandom(): static
52    {
53        $instance = new self(
54            new BoundedContextHashmap([
55                'test' => new BoundedContext(
56                    new BoundedContextId('test'),
57                    new ReflectionClassList([new \ReflectionClass(DummyUser::class)]),
58                    new ReflectionMethodList()
59                )
60            ])
61        );
62        $datalayer = $instance->toDatalayer();
63        $datalayer->all(new \ReflectionClass(DummyUser::class), new BoundedContextId('test'))
64            ->getTotalCount();
65
66        return $instance;
67    }
68
69    public function toDatalayer(): DoctrineEntityDatalayer
70    {
71        if ($this->datalayer) {
72            return $this->datalayer;
73        }
74        $serverPath = $this->sqliteFile->getOrSetOnServerPath();
75
76        $tempFolder = sys_get_temp_dir() . DIRECTORY_SEPARATOR . $this->hashmap->getUniqueHash();
77        if (!@mkdir($tempFolder) && !is_dir($tempFolder)) {
78            throw new \LogicException('Can not create temp folder ' . $tempFolder);
79        }
80        $entityPath = $tempFolder . DIRECTORY_SEPARATOR . 'entities';
81        if (!@mkdir($entityPath) && !is_dir($entityPath)) {
82            throw new \LogicException('Can not create entity folder ' . $entityPath);
83        }
84        $proxyPath = $tempFolder . DIRECTORY_SEPARATOR . 'proxies';
85        if (!@mkdir($proxyPath) && !is_dir($proxyPath)) {
86            throw new \LogicException('Can not create proxy folder ' . $proxyPath);
87        }
88        $ormBuilder = new DoctrineEntityConverterOrmBuilder(
89            new PersistenceLayerFactory(),
90            $this->hashmap,
91            true
92        );
93        $ormBuilder = new OrmBuilder(
94            $ormBuilder,
95            buildOnce: false,
96            runMigrations: true,
97            devMode: true,
98            proxyDir: $proxyPath,
99            cache: null,
100            path: $entityPath,
101            connectionConfig: [
102                'driver' => 'pdo_sqlite',
103                'path' => $serverPath
104            ]
105        );
106        $domainToStorageConverter = DomainToStorageConverter::create(FileStorageFactory::create());
107        $doctrineListFactory = new DoctrineListFactory(
108            $ormBuilder,
109            new EntityQueryFilterFactory(),
110            $domainToStorageConverter
111        );
112        return $this->datalayer = new DoctrineEntityDatalayer(
113            $ormBuilder,
114            $domainToStorageConverter,
115            new DirectIndexStrategy(new EntityReindexer($ormBuilder, Indexer::create())),
116            $doctrineListFactory
117        );
118    }
119}