Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
91.30% covered (success)
91.30%
21 / 23
75.00% covered (warning)
75.00%
3 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
BoundedContextHashmap
91.30% covered (success)
91.30%
21 / 23
75.00% covered (warning)
75.00%
3 / 4
14.13
0.00% covered (danger)
0.00%
0 / 1
 getUniqueHash
81.82% covered (warning)
81.82%
9 / 11
0.00% covered (danger)
0.00%
0 / 1
5.15
 offsetGet
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getTupleIterator
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getBoundedContextFromClassName
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
7
1<?php
2namespace Apie\Core\BoundedContext;
3
4use Apie\Core\Actions\BoundedContextHashmapIterator;
5use Apie\Core\Entities\EntityInterface;
6use Apie\Core\Identifiers\IdentifierInterface;
7use Apie\Core\Lists\ItemHashmap;
8use ReflectionClass;
9
10/**
11 * Contains multiple bounded contexts mapped by key.
12 */
13final class BoundedContextHashmap extends ItemHashmap
14{
15    protected bool $mutable = false;
16
17    private string $calculatedHash;
18
19    public function getUniqueHash(): string
20    {
21        if (isset($this->calculatedHash)) {
22            return $this->calculatedHash;
23        }
24        $hash = '';
25        foreach ($this as $value) {
26            $list = [];
27            foreach ($value->resources as $resource) {
28                $list[] = $resource->name;
29            }
30            foreach ($value->actions as $action) {
31                $list[] = $action->getDeclaringClass()->name . '::' . $action->getName();
32            }
33            $hash .= ';;' . $value->getId()->toNative() . ',' . implode(',', $list);
34        }
35
36        return $this->calculatedHash = md5($hash);
37    }
38    public function offsetGet(mixed $offset): BoundedContext
39    {
40        return parent::offsetGet($offset);
41    }
42
43    public function getTupleIterator(): BoundedContextHashmapIterator
44    {
45        return new BoundedContextHashmapIterator($this);
46    }
47
48    /**
49     * @param ReflectionClass<EntityInterface>|ReflectionClass<IdentifierInterface<EntityInterface>> $class
50     */
51    public function getBoundedContextFromClassName(ReflectionClass $class, ?BoundedContextId $prio = null): ?BoundedContext
52    {
53        if (in_array(IdentifierInterface::class, $class->getInterfaceNames())) {
54            $class = $class->getMethod('getReferenceFor')->invoke(null);
55        }
56        if ($prio && isset($this[$prio->toNative()])) {
57            $boundedContext = $this[$prio->toNative()];
58            if ($boundedContext->contains($class)) {
59                return $boundedContext;
60            }
61        }
62        foreach ($this as $boundedContext) {
63            if ($boundedContext->contains($class)) {
64                return $boundedContext;
65            }
66        }
67        return null;
68    }
69}