RepositoryDefinitions::setRepositoryName()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
namespace ByTIC\Models\SmartProperties\Definitions;
4
5
use ByTIC\Models\SmartProperties\Exceptions\InvalidArgumentException;
6
use Nip\Collections\Lazy\AbstractLazyCollection;
7
8
/**
9
 * Class RepositoryDefinitions
10
 * @package ByTIC\Models\SmartProperties\Definitions
11
 */
12
class RepositoryDefinitions extends AbstractLazyCollection
13
{
14
    protected $repositoryName;
15
16
    /**
17
     * @var \Closure
18
     */
19
    protected $builder;
20
21
    /**
22
     * @param mixed $repositoryName
23
     */
24
    public function setRepositoryName($repositoryName): void
25
    {
26
        $this->repositoryName = $repositoryName;
27
    }
28
29
    /**
30
     * @param \Closure $builder
31
     */
32
    public function setBuilder(\Closure $builder): void
33
    {
34
        $this->builder = $builder;
35
    }
36
37
    public function get($key, $default = null)
38
    {
39
        $definition = parent::get($key, $default);
40
        if ($definition instanceof Definition) {
41
            return $definition;
42
        }
43
        throw new InvalidArgumentException(sprintf(
44
            'Unable to find a definition "%s" for class "%s".',
45
            $key, $this->repositoryName
46
        ));
47
    }
48
49
    /**
50
     * @param $definition
51
     * @return bool
52
     */
53
    public function offsetExists($key): bool
54
    {
55
        $key = \ByTIC\Models\SmartProperties\Utility\Definition::name($key);
56
        return parent::offsetExists($key);
57
    }
58
59
    /**
60
     * @param $manager
61
     * @param string|Definition $definition
62
     * @return mixed
63
     */
64
    public function offsetGet($key): mixed
65
    {
66
        $key = \ByTIC\Models\SmartProperties\Utility\Definition::name($key);
67
68
        if ($this->has($key)) {
69
            return parent::offsetGet($key);
70
        }
71
72
        throw new InvalidArgumentException(sprintf(
73
            'Unable to find a definition "%s" for class "%s".',
74
            $key, $this->repositoryName
75
        ));
76
    }
77
78
    public function offsetSet($key, $value): void
79
    {
80
        $key = $key ?? \ByTIC\Models\SmartProperties\Utility\Definition::name($value);
81
        parent::offsetSet($key, $value);
82
    }
83
84
    protected function doLoad(): void
85
    {
86
        call_user_func($this->builder);
87
    }
88
}
89