Passed
Pull Request — master (#17)
by Matthew
02:15
created

InstanceCreator::getMapperInstanceString()   A

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 0
1
<?php
2
3
namespace Dynamic\Salsify\Traits;
4
5
use Dynamic\Salsify\Model\Fetcher;
6
use Dynamic\Salsify\Model\Mapper;
7
use SilverStripe\Core\Injector\Injector;
8
9
/**
10
 * Trait InstanceCreator
11
 */
12
trait InstanceCreator
13
{
14
    /**
15
     * @var Fetcher
16
     */
17
    private $fetcher;
18
19
    /**
20
     * @var Mapper
21
     */
22
    private $mapper;
23
24
    /**
25
     * @return string
26
     */
27
    protected abstract function getImporterKey();
28
29
    /**
30
     * @return string
31
     */
32
    protected function getMapperInstanceString()
33
    {
34
        return Mapper::class . '.' . $this->getImporterKey();
35
    }
36
37
    /**
38
     * @return string
39
     */
40
    protected function getFetcherInstanceString()
41
    {
42
        return Fetcher::class . '.' . $this->getImporterKey();
43
    }
44
45
    /**
46
     * @param $className
47
     * @return bool
48
     */
49
    protected function hasService($className)
50
    {
51
        return Injector::inst()->has($className . '.' . $this->getImporterKey());
52
    }
53
54
    protected function createServices()
55
    {
56
        if (!Injector::inst()->has($this->getMapperInstanceString())) {
57
            Injector::inst()->load([
58
                $this->getMapperInstanceString() => [
59
                    'class' => Mapper::class,
60
                ],
61
            ]);
62
        }
63
        if (!Injector::inst()->has($this->getFetcherInstanceString())) {
64
            Injector::inst()->load([
65
                $this->getFetcherInstanceString() => [
66
                    'class' => Fetcher::class,
67
                ],
68
            ]);
69
        }
70
    }
71
72
    /**
73
     * @return Fetcher
74
     * @throws \Exception
75
     */
76
    public function getFetcher()
77
    {
78
        if (!$this->fetcher) {
79
            $this->setFetcher();
80
        }
81
        return $this->fetcher;
82
    }
83
84
    /**
85
     * @throws \Exception
86
     */
87
    protected function setFetcher()
88
    {
89
        $this->fetcher = Injector::inst()->createWithArgs($this->getFetcherInstanceString(), [
90
            'importerKey' => $this->getImporterKey(),
91
            'noChannel' => property_exists($this, 'noChannel') ? $this->noChannel : false,
92
        ]);
93
    }
94
95
    /**
96
     * @return Mapper
97
     * @throws \Exception
98
     */
99
    public function getMapper()
100
    {
101
        if (!$this->mapper) {
102
            $this->setMapper();
103
        }
104
        return $this->mapper;
105
    }
106
107
    /**
108
     * @throws \Exception
109
     */
110
    protected function setMapper()
111
    {
112
        $this->mapper = Injector::inst()->createWithArgs($this->getMapperInstanceString(), [
113
            'importerKey' => $this->getImporterKey(),
114
            'file' => property_exists($this, 'file') ? $this->file : null,
115
        ]);
116
    }
117
}
118