Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
55.17% |
16 / 29 |
|
40.00% |
2 / 5 |
CRAP | |
0.00% |
0 / 1 |
| GeneralServiceFactory | |
55.17% |
16 / 29 |
|
40.00% |
2 / 5 |
13.77 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| createContextBuilderFactory | |
60.00% |
6 / 10 |
|
0.00% |
0 / 1 |
2.26 | |||
| createRoutedDefinitionProvider | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| createDataLayerMap | |
42.86% |
6 / 14 |
|
0.00% |
0 / 1 |
4.68 | |||
| createFaker | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | namespace Apie\Common\Wrappers; |
| 3 | |
| 4 | use Apie\Common\ContextBuilderFactory as CommonContextBuilderFactory; |
| 5 | use Apie\Common\ContextBuilders\BoundedContextProviderContextBuilder; |
| 6 | use Apie\Common\Interfaces\RouteDefinitionProviderInterface; |
| 7 | use Apie\Common\RouteDefinitions\ChainedRouteDefinitionsProvider; |
| 8 | use Apie\Core\BoundedContext\BoundedContextHashmap; |
| 9 | use Apie\Core\ContextBuilders\ContextBuilderFactory; |
| 10 | use Apie\Core\ContextBuilders\ContextBuilderInterface; |
| 11 | use Apie\Core\Datalayers\Grouped\DataLayerByBoundedContext; |
| 12 | use Apie\Core\Datalayers\Grouped\DataLayerByClass; |
| 13 | use Apie\Faker\ApieObjectFaker; |
| 14 | use Apie\Faker\Interfaces\ApieClassFaker; |
| 15 | use Apie\Serializer\DecoderHashmap; |
| 16 | use Faker\Factory; |
| 17 | use Faker\Generator; |
| 18 | use Symfony\Component\DependencyInjection\Argument\ServiceLocator; |
| 19 | |
| 20 | /** |
| 21 | * This is basically a work around around !tagged_iterators support with variadic arguments. |
| 22 | */ |
| 23 | final class GeneralServiceFactory |
| 24 | { |
| 25 | private function __construct() |
| 26 | { |
| 27 | } |
| 28 | |
| 29 | /** |
| 30 | * @param iterable<int, ContextBuilderInterface> $contextBuilders |
| 31 | */ |
| 32 | public static function createContextBuilderFactory( |
| 33 | BoundedContextHashmap $boundedContextHashmap, |
| 34 | ?DecoderHashmap $decoderHashmap, |
| 35 | iterable $contextBuilders |
| 36 | ): ContextBuilderFactory { |
| 37 | if ($decoderHashmap) { |
| 38 | return CommonContextBuilderFactory::create( |
| 39 | $boundedContextHashmap, |
| 40 | $decoderHashmap, |
| 41 | ...$contextBuilders |
| 42 | ); |
| 43 | } |
| 44 | return new ContextBuilderFactory( |
| 45 | new BoundedContextProviderContextBuilder($boundedContextHashmap), |
| 46 | ...$contextBuilders |
| 47 | ); |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * @param iterable<int, RouteDefinitionProviderInterface> $routeDefinitionProviders |
| 52 | */ |
| 53 | public static function createRoutedDefinitionProvider(iterable $routeDefinitionProviders): RouteDefinitionProviderInterface |
| 54 | { |
| 55 | return new ChainedRouteDefinitionsProvider(...$routeDefinitionProviders); |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * @param array<string, mixed> $dataLayerConfig |
| 60 | */ |
| 61 | public static function createDataLayerMap( |
| 62 | array $dataLayerConfig, |
| 63 | ServiceLocator $serviceLocator |
| 64 | ): DataLayerByBoundedContext { |
| 65 | $hashmap = new DataLayerByBoundedContext([]); |
| 66 | foreach (($dataLayerConfig['context_mapping'] ?? []) as $boundedContextId => $config) { |
| 67 | $map = new DataLayerByClass(); |
| 68 | foreach (($config['entity_mapping'] ?? []) as $entityClass => $serviceId) { |
| 69 | $map[$entityClass] = $serviceLocator->get($serviceId); |
| 70 | } |
| 71 | $defaultServiceId = $config['default_datalayer'] ?? $dataLayerConfig['default_datalayer'] ?? RequestAwareInMemoryDatalayer::class; |
| 72 | $map->setDefaultDataLayer( |
| 73 | $serviceLocator->get($defaultServiceId) |
| 74 | ); |
| 75 | $hashmap[$boundedContextId] = $map; |
| 76 | } |
| 77 | $hashmap->setDefaultDataLayer( |
| 78 | $serviceLocator->get($dataLayerConfig['default_datalayer'] ?? RequestAwareInMemoryDatalayer::class) |
| 79 | ); |
| 80 | return $hashmap; |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * @param iterable<int, ApieClassFaker<object>> $fakeProviders |
| 85 | */ |
| 86 | public static function createFaker(iterable $fakeProviders): Generator |
| 87 | { |
| 88 | $faker = Factory::create(); |
| 89 | $faker->addProvider(ApieObjectFaker::createWithDefaultFakers($faker, ...$fakeProviders)); |
| 90 | |
| 91 | return $faker; |
| 92 | } |
| 93 | } |