Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
81.82% covered (warning)
81.82%
9 / 11
33.33% covered (danger)
33.33%
1 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
DataLayerByBoundedContext
81.82% covered (warning)
81.82%
9 / 11
33.33% covered (danger)
33.33%
1 / 3
7.29
0.00% covered (danger)
0.00%
0 / 1
 offsetGet
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setDefaultDataLayer
80.00% covered (warning)
80.00%
4 / 5
0.00% covered (danger)
0.00%
0 / 1
2.03
 pickDataLayerFor
80.00% covered (warning)
80.00%
4 / 5
0.00% covered (danger)
0.00%
0 / 1
4.13
1<?php
2namespace Apie\Core\Datalayers\Grouped;
3
4use Apie\Core\BoundedContext\BoundedContextId;
5use Apie\Core\Datalayers\ApieDatalayer;
6use Apie\Core\Entities\EntityInterface;
7use Apie\Core\Exceptions\ObjectIsImmutable;
8use Apie\Core\Lists\ItemHashmap;
9use ReflectionClass;
10
11final class DataLayerByBoundedContext extends ItemHashmap
12{
13    private ApieDataLayer|DataLayerByClass $defaultDataLayer;
14
15    public function offsetGet(mixed $offset): DataLayerByClass
16    {
17        return parent::offsetGet($offset);
18    }
19
20    public function setDefaultDataLayer(ApieDatalayer|DataLayerByClass $defaultDataLayer): self
21    {
22        if (isset($this->defaultDataLayer)) {
23            throw new ObjectIsImmutable($this);
24        }
25        $this->mutable = false;
26        $this->defaultDataLayer = $defaultDataLayer;
27        return $this;
28    }
29
30    /**
31     * @param ReflectionClass<EntityInterface> $class
32     */
33    public function pickDataLayerFor(ReflectionClass $class, ?BoundedContextId $boundedContextId): ApieDatalayer
34    {
35        if ($boundedContextId && isset($this[$boundedContextId->toNative()])) {
36            return $this[$boundedContextId->toNative()]->pickDataLayerFor($class);
37        }
38        if ($this->defaultDataLayer instanceof DataLayerByClass) {
39            return $this->defaultDataLayer->pickDataLayerFor($class);
40        }
41        return $this->defaultDataLayer;
42    }
43}