Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
93.10% |
54 / 58 |
|
75.00% |
3 / 4 |
CRAP | |
0.00% |
0 / 1 |
| Dataset | |
93.10% |
54 / 58 |
|
75.00% |
3 / 4 |
11.04 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| getId | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| createRandom | |
100.00% |
13 / 13 |
|
100.00% |
1 / 1 |
1 | |||
| toDatalayer | |
90.48% |
38 / 42 |
|
0.00% |
0 / 1 |
8.06 | |||
| 1 | <?php |
| 2 | namespace Apie\Dataset; |
| 3 | |
| 4 | use Apie\Core\Attributes\FakeCount; |
| 5 | use Apie\Core\Attributes\FakeMethod; |
| 6 | use Apie\Core\Attributes\StoreOptions; |
| 7 | use Apie\Core\BoundedContext\BoundedContext; |
| 8 | use Apie\Core\BoundedContext\BoundedContextHashmap; |
| 9 | use Apie\Core\BoundedContext\BoundedContextId; |
| 10 | use Apie\Core\Entities\EntityInterface; |
| 11 | use Apie\Core\FileStorage\FileStorageFactory; |
| 12 | use Apie\Core\FileStorage\SqliteFile; |
| 13 | use Apie\Core\Indexing\Indexer; |
| 14 | use Apie\Core\Lists\ReflectionClassList; |
| 15 | use Apie\Core\Lists\ReflectionMethodList; |
| 16 | use Apie\Dataset\DummyApp\Resources\DummyUser; |
| 17 | use Apie\DoctrineEntityConverter\Factories\PersistenceLayerFactory; |
| 18 | use Apie\DoctrineEntityConverter\OrmBuilder as DoctrineEntityConverterOrmBuilder; |
| 19 | use Apie\DoctrineEntityDatalayer\DoctrineEntityDatalayer; |
| 20 | use Apie\DoctrineEntityDatalayer\EntityReindexer; |
| 21 | use Apie\DoctrineEntityDatalayer\Factories\DoctrineListFactory; |
| 22 | use Apie\DoctrineEntityDatalayer\Factories\EntityQueryFilterFactory; |
| 23 | use Apie\DoctrineEntityDatalayer\IndexStrategy\DirectIndexStrategy; |
| 24 | use Apie\DoctrineEntityDatalayer\OrmBuilder; |
| 25 | use Apie\StorageMetadata\DomainToStorageConverter; |
| 26 | |
| 27 | #[FakeMethod('createRandom')] |
| 28 | #[FakeCount(1)] |
| 29 | final 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 | } |