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

Instantiator   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Test Coverage

Coverage 94.12%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 17
c 1
b 0
f 0
dl 0
loc 45
ccs 16
cts 17
cp 0.9412
rs 10
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A prepare() 0 6 1
A instantiate() 0 10 2
A create() 0 9 2
1
<?php
2
3
namespace Nip\Records\Instantiator;
4
5
use Nip\Records\Mapping\Configurator\EntityConfigurator;
6
use Nip\Records\Mapping\MappingManager;
7
use Nip\Records\RecordManager;
8
use Nip\Records\Registry\HasModelRegistry;
9
10
/**
11
 * Class Instantiator
12
 * @package Nip\Records\Instantiator
13
 */
14
class Instantiator
15
{
16
    use HasModelRegistry;
17
18
    /**
19
     * @param $className
20
     * @return mixed
21
     */
22 2
    public function instantiate($className)
23
    {
24 2
        $registry = $this->getModelRegistry();
25 2
        if ($registry->has($className)) {
26 1
            return $registry->get($className);
27
        }
28
29 1
        $manager = $this->create($className);
30 1
        $registry->set($manager->getClassName(), $manager);
31 1
        return $manager;
32
    }
33
34
    /**
35
     * @param $className
36
     * @return mixed
37
     */
38 1
    protected function create($className)
39
    {
40 1
        if (method_exists($className, "instance")) {
41
            $manager = call_user_func([$className, "instance"]);
42
        } else {
43 1
            $manager = new $className();
44
        }
45
46 1
        return $this->prepare($manager);
47
    }
48
49
    /**
50
     * @param RecordManager $manager
51
     * @return RecordManager
52
     */
53 1
    protected function prepare(RecordManager $manager)
54
    {
55 1
        $manager->bootIfNotBooted();
56 1
        $mapping = MappingManager::for($manager);
57 1
        EntityConfigurator::wire($manager, $mapping);
58 1
        return $manager;
59
    }
60
}
61