HasInstantiatorTrait::setInstantiator()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
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
}
44