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++) { |
|
|
|
|
123
|
|
|
$currentKey = $configKeys[$i]; |
124
|
|
|
$fetcherConfig = $this->getFetcher()->config()->get($currentKey); |
125
|
|
|
$this->mapper->config()->set($currentKey, $fetcherConfig); |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|
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: