Completed
Push — master ( 096858...7ec34d )
by Gabriel
03:50
created

HasInstantiatorTrait   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
c 1
b 0
f 0
dl 0
loc 33
ccs 12
cts 12
cp 1
rs 10
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setInstantiator() 0 3 1
A getInstantiator() 0 6 2
A generateInstantiator() 0 7 2
1
<?php
2
3
namespace Nip\Records\Instantiator;
4
5
/**
6
 * Trait HasInstantiatorTrait
7
 * @package Nip\Records\Instantiator
8
 */
9
trait HasInstantiatorTrait
10
{
11
    protected $instantiator = null;
12
13
    /**
14
     * @return Instantiator
15
     */
16 3
    public function getInstantiator()
17
    {
18 3
        if ($this->instantiator === null) {
19 1
            $this->setInstantiator($this->generateInstantiator());
20
        }
21 3
        return $this->instantiator;
22
    }
23
24
    /**
25
     * @param Instantiator $instantiator
26
     */
27 3
    public function setInstantiator($instantiator): void
28
    {
29 3
        $this->instantiator = $instantiator;
30 3
    }
31
32
    /**
33
     * @return Instantiator
34
     */
35 1
    protected function generateInstantiator()
36
    {
37 1
        $instantiator = new Instantiator();
38 1
        if (method_exists($this, 'getModelRegistry')) {
39 1
            $instantiator->setModelRegistry($this->getModelRegistry());
40
        }
41 1
        return $instantiator;
42
    }
43
}