DefinitionRegistry::get()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
c 0
b 0
f 0
dl 0
loc 7
rs 10
cc 2
nc 2
nop 2
1
<?php
2
3
namespace ByTIC\Models\SmartProperties\Definitions;
4
5
use ByTIC\Models\SmartProperties\Definitions\Builders\RepositoryBuilder;
6
use Nip\Utility\Traits\SingletonTrait;
7
8
/**
9
 * Class DefinitionRegistry
10
 * @package ByTIC\Models\SmartProperties\Definitions
11
 */
12
class DefinitionRegistry
13
{
14
    use SingletonTrait;
15
16
    /**
17
     * @var RepositoryDefinitions[]
18
     */
19
    protected array $definitions = [];
20
21
    /**
22
     * @param $manager
23
     * @param \Closure|null $builder
24
     * @return RepositoryDefinitions
25
     */
26
    public function get($manager, \Closure $builder = null)
27
    {
28
        $managerName = $this->managerName($manager);
29
        if (!isset($this->definitions[$managerName])) {
30
            $this->definitions[$managerName] = RepositoryBuilder::for($manager, $builder);
0 ignored issues
show
Bug introduced by
It seems like $builder can also be of type null; however, parameter $builder of ByTIC\Models\SmartProper...epositoryBuilder::for() does only seem to accept Closure, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

30
            $this->definitions[$managerName] = RepositoryBuilder::for($manager, /** @scrutinizer ignore-type */ $builder);
Loading history...
31
        }
32
        return $this->definitions[$managerName];
33
    }
34
35
    /**
36
     * @param object|string $manager
37
     * @return string
38
     */
39
    protected function managerName($manager): string
40
    {
41
        if (is_object($manager)) {
42
            return get_class($manager);
43
        }
44
        return (string)$manager;
45
    }
46
}
47