Passed
Push — master ( 606bf7...dde9c0 )
by Matthew
01:54
created

InstanceCreator   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 114
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 15
eloc 34
c 1
b 0
f 0
dl 0
loc 114
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getFetcher() 0 6 2
A getMapper() 0 6 2
A getMapperInstanceString() 0 3 1
A hasService() 0 3 1
A createServices() 0 13 3
A getFetcherInstanceString() 0 3 1
A setFetcher() 0 5 2
A setMapper() 0 16 3
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
    abstract protected 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
    public 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
        $configKeys = [
118
            'apiKey',
119
            'timeout',
120
            'organizationID',
121
        ];
122
        for ($i = 0; $i < count($configKeys); $i++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
123
            $currentKey = $configKeys[$i];
124
            $fetcherConfig = $this->getFetcher()->config()->get($currentKey);
125
            $this->mapper->config()->set($currentKey, $fetcherConfig);
126
        }
127
    }
128
}
129