Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
60.00% |
6 / 10 |
|
66.67% |
2 / 3 |
CRAP | |
0.00% |
0 / 1 |
| BoundedContextSelected | |
60.00% |
6 / 10 |
|
66.67% |
2 / 3 |
8.30 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getBoundedContextFromRequest | |
50.00% |
4 / 8 |
|
0.00% |
0 / 1 |
6.00 | |||
| getBoundedContextFromClassName | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | namespace Apie\ApieBundle\Wrappers; |
| 3 | |
| 4 | use Apie\Common\Interfaces\BoundedContextSelection; |
| 5 | use Apie\Core\BoundedContext\BoundedContext; |
| 6 | use Apie\Core\BoundedContext\BoundedContextHashmap; |
| 7 | use Apie\Core\ContextConstants; |
| 8 | use Apie\Core\Entities\EntityInterface; |
| 9 | use ReflectionClass; |
| 10 | use Symfony\Component\HttpFoundation\RequestStack; |
| 11 | |
| 12 | /** |
| 13 | * Helper class that returns the current bounded context. |
| 14 | */ |
| 15 | final class BoundedContextSelected implements BoundedContextSelection |
| 16 | { |
| 17 | public function __construct( |
| 18 | private readonly RequestStack $requestStack, |
| 19 | private readonly BoundedContextHashmap $boundedContextHashmap |
| 20 | ) { |
| 21 | } |
| 22 | |
| 23 | /** |
| 24 | * Returns a bounded context from a symfony request if it is available. |
| 25 | */ |
| 26 | public function getBoundedContextFromRequest(): ?BoundedContext |
| 27 | { |
| 28 | $request = $this->requestStack->getMainRequest(); |
| 29 | if (!$request) { |
| 30 | return null; |
| 31 | } |
| 32 | if ($request->attributes->has(ContextConstants::BOUNDED_CONTEXT_ID)) { |
| 33 | return $this->boundedContextHashmap[$request->attributes->get(ContextConstants::BOUNDED_CONTEXT_ID)]; |
| 34 | } |
| 35 | if ($request->attributes->has(ContextConstants::RESOURCE_NAME)) { |
| 36 | return $this->getBoundedContextFromClassName($request->attributes->get(ContextConstants::RESOURCE_NAME)); |
| 37 | } |
| 38 | return null; |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * Returns the boundedcontext linked to an entity class. If the entity is linked to multiple bounded contexts it will return |
| 43 | * the first bounded context. |
| 44 | * |
| 45 | * @param class-string<EntityInterface> $className |
| 46 | */ |
| 47 | public function getBoundedContextFromClassName(string $className): ?BoundedContext |
| 48 | { |
| 49 | return $this->boundedContextHashmap->getBoundedContextFromClassName(new ReflectionClass($className)); |
| 50 | } |
| 51 | } |