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
|
|
|
use SilverStripe\Security\Member; |
9
|
|
|
use SilverStripe\Security\Security; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Trait InstanceCreator |
13
|
|
|
*/ |
14
|
|
|
trait InstanceCreator |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var Fetcher |
18
|
|
|
*/ |
19
|
|
|
private $fetcher; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var Mapper |
23
|
|
|
*/ |
24
|
|
|
private $mapper; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var Member |
28
|
|
|
*/ |
29
|
|
|
private $previousUser; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @return string |
33
|
|
|
*/ |
34
|
|
|
abstract protected function getImporterKey(); |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @return string |
38
|
|
|
*/ |
39
|
|
|
protected function getMapperInstanceString() |
40
|
|
|
{ |
41
|
|
|
return Mapper::class . '.' . $this->getImporterKey(); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @return string |
46
|
|
|
*/ |
47
|
|
|
protected function getFetcherInstanceString() |
48
|
|
|
{ |
49
|
|
|
return Fetcher::class . '.' . $this->getImporterKey(); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @return \SilverStripe\ORM\DataObject|Member |
54
|
|
|
* @throws \SilverStripe\ORM\ValidationException |
55
|
|
|
*/ |
56
|
|
|
protected function findOrCreateSalsifyUser() |
57
|
|
|
{ |
58
|
|
|
if ($member = Member::get()->filter('Email', 'salsify')->first()) { |
59
|
|
|
return $member; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
$member = Member::create(); |
63
|
|
|
$member->FirstName = 'Salsify'; |
64
|
|
|
$member->Surname = 'Integration'; |
65
|
|
|
$member->Email = 'salsify'; |
66
|
|
|
$member->write(); |
67
|
|
|
|
68
|
|
|
return $member; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* |
73
|
|
|
*/ |
74
|
|
|
protected function changeToSalsifyUser() |
75
|
|
|
{ |
76
|
|
|
$this->previousUser = Security::getCurrentUser(); |
77
|
|
|
return Security::setCurrentUser($this->findOrCreateSalsifyUser()); |
|
|
|
|
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* |
82
|
|
|
*/ |
83
|
|
|
protected function changeToPreviousUser() |
84
|
|
|
{ |
85
|
|
|
Security::setCurrentUser($this->previousUser); |
86
|
|
|
$this->previousUser = null; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param $className |
91
|
|
|
* @return bool |
92
|
|
|
*/ |
93
|
|
|
protected function hasService($className) |
94
|
|
|
{ |
95
|
|
|
return Injector::inst()->has($className . '.' . $this->getImporterKey()); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Creates Injector services if they are not defined |
100
|
|
|
*/ |
101
|
|
|
public function createServices() |
102
|
|
|
{ |
103
|
|
|
if (!Injector::inst()->has($this->getMapperInstanceString())) { |
104
|
|
|
Injector::inst()->load([ |
105
|
|
|
$this->getMapperInstanceString() => [ |
106
|
|
|
'class' => Mapper::class, |
107
|
|
|
], |
108
|
|
|
]); |
109
|
|
|
} |
110
|
|
|
if (!Injector::inst()->has($this->getFetcherInstanceString())) { |
111
|
|
|
Injector::inst()->load([ |
112
|
|
|
$this->getFetcherInstanceString() => [ |
113
|
|
|
'class' => Fetcher::class, |
114
|
|
|
], |
115
|
|
|
]); |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @return Fetcher |
121
|
|
|
* @throws \Exception |
122
|
|
|
*/ |
123
|
|
|
public function getFetcher() |
124
|
|
|
{ |
125
|
|
|
if (!$this->fetcher) { |
126
|
|
|
$this->setFetcher(); |
127
|
|
|
} |
128
|
|
|
return $this->fetcher; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @throws \Exception |
133
|
|
|
*/ |
134
|
|
|
protected function setFetcher() |
135
|
|
|
{ |
136
|
|
|
$this->fetcher = Injector::inst()->createWithArgs($this->getFetcherInstanceString(), [ |
137
|
|
|
'importerKey' => $this->getImporterKey(), |
138
|
|
|
'noChannel' => property_exists($this, 'noChannel') ? $this->noChannel : false, |
139
|
|
|
]); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @return Mapper |
144
|
|
|
* @throws \Exception |
145
|
|
|
*/ |
146
|
|
|
public function getMapper() |
147
|
|
|
{ |
148
|
|
|
if (!$this->mapper) { |
149
|
|
|
$this->setMapper(); |
150
|
|
|
} |
151
|
|
|
return $this->mapper; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* @throws \Exception |
156
|
|
|
*/ |
157
|
|
|
protected function setMapper() |
158
|
|
|
{ |
159
|
|
|
$this->mapper = Injector::inst()->createWithArgs($this->getMapperInstanceString(), [ |
160
|
|
|
'importerKey' => $this->getImporterKey(), |
161
|
|
|
'file' => property_exists($this, 'file') ? $this->file : null, |
162
|
|
|
]); |
163
|
|
|
|
164
|
|
|
$configKeys = [ |
165
|
|
|
'apiKey', |
166
|
|
|
'timeout', |
167
|
|
|
'organizationID', |
168
|
|
|
]; |
169
|
|
|
for ($i = 0; $i < count($configKeys); $i++) { |
|
|
|
|
170
|
|
|
$currentKey = $configKeys[$i]; |
171
|
|
|
$fetcherConfig = $this->getFetcher()->config()->get($currentKey); |
172
|
|
|
$this->mapper->config()->set($currentKey, $fetcherConfig); |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
} |
176
|
|
|
|
This check looks for function or method calls that always return null and whose return value is used.
The method
getObject()
can return nothing but null, so it makes no sense to use the return value.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.