Passed
Push — master ( b33b8e...5acb2c )
by Gabriel
04:09
created

MappingManager   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Test Coverage

Coverage 90.32%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 26
c 1
b 0
f 0
dl 0
loc 73
ccs 28
cts 31
cp 0.9032
rs 10
wmc 9

7 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 8 1
A for() 0 8 2
A boot() 0 3 1
A repository() 0 4 1
A bootIfNeeded() 0 7 2
A __construct() 0 3 1
A __destruct() 0 3 1
1
<?php
2
3
namespace Nip\Records\Mapping;
4
5
use Nip\Records\Mapping\Configurator\DataConfigurator;
6
use Nip\Records\Mapping\MappingManager\CanCache;
7
use Nip\Records\RecordManager;
8
use Nip\Utility\Traits\SingletonTrait;
9
10
/**
11
 * Class MappingManager
12
 * @package Nip\Records\Mapping
13
 */
14
class MappingManager
15
{
16
    use SingletonTrait;
17
    use CanCache;
18
19
    protected $booted = false;
20
21
    /**
22
     * @var MappingRepository
23
     */
24
    protected $repository;
25
26
    /**
27
     * MappingManager constructor.
28
     */
29 2
    public function __construct()
30
    {
31 2
        $this->repository = new MappingRepository();
32 2
    }
33
34
    /**
35
     * @param RecordManager $manager
36
     * @return MappingData
37
     */
38 2
    public static function for(RecordManager $manager)
39
    {
40 2
        $instance = static::instance();
41 2
        $className = $manager->getClassName();
42 2
        if (!$instance->repository()->has($className)) {
43 2
            static::init($manager);
44
        }
45 2
        return $instance->repository()->get($className);
46
    }
47
48
    /**
49
     * @param RecordManager $manager
50
     */
51 2
    public static function init(RecordManager $manager)
52
    {
53 2
        $instance = static::instance();
54 2
        $data = new MappingData();
55 2
        $className = $manager->getClassName();
56 2
        DataConfigurator::wire($manager, $data);
57 2
        $instance->needsCaching(true);
58 2
        $instance->repository()->set($className, $data);
59 2
    }
60
61
    /**
62
     * @return MappingRepository
63
     */
64 3
    public function repository(): MappingRepository
65
    {
66 3
        $this->bootIfNeeded();
67 3
        return $this->repository;
68
    }
69
70
    public function __destruct()
71
    {
72
        $this->checkSaveCache();
73
    }
74
75 3
    protected function bootIfNeeded()
76
    {
77 3
        if ($this->booted === true) {
78 3
            return;
79
        }
80 2
        $this->boot();
81 2
        $this->booted = true;
82 2
    }
83
84 1
    protected function boot()
85
    {
86 1
        $this->initFromCache();
87 1
    }
88
}
89