Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
54.05% covered (warning)
54.05%
20 / 37
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
BoundedContextHashmapFactory
54.05% covered (warning)
54.05%
20 / 37
50.00% covered (danger)
50.00%
1 / 2
11.75
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 create
52.78% covered (warning)
52.78%
19 / 36
0.00% covered (danger)
0.00%
0 / 1
9.79
1<?php
2namespace Apie\Common\Wrappers;
3
4use Apie\Common\Config\Configuration;
5use Apie\Common\ValueObjects\EntityNamespace;
6use Apie\Core\ApieLib;
7use Apie\Core\BoundedContext\BoundedContext;
8use Apie\Core\BoundedContext\BoundedContextHashmap;
9use Apie\Core\BoundedContext\BoundedContextId;
10use Apie\Core\Entities\EntityInterface;
11use Symfony\Component\Finder\Finder;
12
13/**
14 * Creates a BoundedContextHashmap instance from the ApieBundle configuration.
15 *
16 * @see Configuration in apie/apie-bundle for Symfony
17 * @see resources/apie.php in apie/laravel-apie for Laravel
18 */
19final class BoundedContextHashmapFactory
20{
21    /**
22     * @param array<string, mixed> $boundedContexts
23     * @param array<string, string> $scanBoundedContexts
24     */
25    public function __construct(
26        private readonly array $boundedContexts,
27        private readonly array $scanBoundedContexts
28    ) {
29    }
30
31    public function create(): BoundedContextHashmap
32    {
33        $result = [];
34        $entities = [];
35        foreach ($this->boundedContexts as $boundedContextId => $boundedContextConfig) {
36            $contextId = new BoundedContextId($boundedContextId);
37            $namespace = new EntityNamespace($boundedContextConfig['entities_namespace']);
38            $classes = $namespace->getClasses($boundedContextConfig['entities_folder']);
39            $entities = array_merge($entities, $classes->toStringArray());
40            $namespace = new EntityNamespace($boundedContextConfig['actions_namespace']);
41            $methods = $namespace->getMethods($boundedContextConfig['actions_folder']);
42            $result[$boundedContextId] = new BoundedContext(
43                $contextId,
44                $classes,
45                $methods
46            );
47        }
48        ApieLib::registerAlias(EntityInterface::class, implode('|', $entities));
49        if (!empty($this->scanBoundedContexts['search_path'])
50            && !empty($this->scanBoundedContexts['search_namespace'])
51            && is_dir($this->scanBoundedContexts['search_path'])
52        ) {
53            $paths = Finder::create()
54                ->in($this->scanBoundedContexts['search_path'])
55                ->depth(0)
56                ->directories();
57            $namespace = new EntityNamespace($this->scanBoundedContexts['search_namespace']);
58            foreach ($paths as $path) {
59                $contextId = new BoundedContextId(strtolower($path->getBasename()));
60                $pathNamespace = $namespace->getChildNamespace($path->getBasename());
61                $resourceNamespace = $pathNamespace->getChildNamespace('Resources');
62                $classes = $resourceNamespace->getClasses($path . '/Resources');
63                $methodNamespace = $pathNamespace->getChildNamespace('Actions');
64                $methods = $methodNamespace->getMethods($path . '/Actions');
65                $result[$contextId->toNative()] = new BoundedContext(
66                    $contextId,
67                    $classes,
68                    $methods
69                );
70            }
71        }
72        return new BoundedContextHashmap($result);
73    }
74}