1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @package: chapi |
4
|
|
|
* |
5
|
|
|
* @author: msiebeneicher |
6
|
|
|
* @since: 2015-07-28 |
7
|
|
|
* |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Chapi\Service\JobRepository; |
11
|
|
|
|
12
|
|
|
use Chapi\Component\Cache\CacheInterface; |
13
|
|
|
use Chapi\Component\RemoteClients\ApiClientInterface; |
14
|
|
|
use Chapi\Entity\Chronos\ChronosJobEntity; |
15
|
|
|
use Chapi\Entity\JobEntityInterface; |
16
|
|
|
use Chapi\Service\JobValidator\JobValidatorServiceInterface; |
17
|
|
|
use Psr\Log\LoggerInterface; |
18
|
|
|
|
19
|
|
|
class BridgeChronos implements BridgeInterface |
20
|
|
|
{ |
21
|
|
|
const CACHE_TIME_JOB_LIST = 60; |
22
|
|
|
|
23
|
|
|
const CACHE_KEY_JOB_LIST = 'jobs.list'; |
24
|
|
|
|
25
|
|
|
const API_CALL_ADD = 'addingJob'; |
26
|
|
|
|
27
|
|
|
const API_CALL_UPDATE = 'updatingJob'; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var ApiClientInterface |
31
|
|
|
*/ |
32
|
|
|
private $oApiClient; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var CacheInterface |
36
|
|
|
*/ |
37
|
|
|
private $oCache; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var JobValidatorServiceInterface |
41
|
|
|
*/ |
42
|
|
|
private $oJobEntityValidatorService; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var bool |
46
|
|
|
*/ |
47
|
|
|
private $bCacheHasToDelete = false; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var LoggerInterface |
51
|
|
|
*/ |
52
|
|
|
private $oLogger; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param \Chapi\Component\RemoteClients\ApiClientInterface $oApiClient |
56
|
|
|
* @param CacheInterface $oCache |
57
|
|
|
* @param JobValidatorServiceInterface $oJobEntityValidatorService |
58
|
|
|
* @param LoggerInterface $oLogger |
59
|
|
|
*/ |
60
|
8 |
View Code Duplication |
public function __construct( |
|
|
|
|
61
|
|
|
ApiClientInterface $oApiClient, |
62
|
|
|
CacheInterface $oCache, |
63
|
|
|
JobValidatorServiceInterface $oJobEntityValidatorService, |
64
|
|
|
LoggerInterface $oLogger |
65
|
|
|
) |
66
|
|
|
{ |
67
|
8 |
|
$this->oApiClient = $oApiClient; |
68
|
8 |
|
$this->oCache = $oCache; |
69
|
8 |
|
$this->oJobEntityValidatorService = $oJobEntityValidatorService; |
70
|
8 |
|
$this->oLogger = $oLogger; |
71
|
8 |
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* delete cache job.list if a job was removed |
75
|
|
|
*/ |
76
|
8 |
|
public function __destruct() |
77
|
|
|
{ |
78
|
8 |
|
if ($this->bCacheHasToDelete) |
79
|
|
|
{ |
80
|
3 |
|
$this->oCache->delete(self::CACHE_KEY_JOB_LIST); |
81
|
|
|
} |
82
|
8 |
|
} |
83
|
|
|
|
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @return ChronosJobEntity[] |
87
|
|
|
*/ |
88
|
2 |
|
public function getJobs() |
89
|
|
|
{ |
90
|
2 |
|
$_aReturn = []; |
91
|
2 |
|
$_aJobList = $this->getJobList(); |
92
|
|
|
|
93
|
2 |
|
if (!empty($_aJobList)) |
94
|
|
|
{ |
95
|
|
|
// prepare return value |
96
|
2 |
|
foreach ($_aJobList as $_aJobData) |
97
|
|
|
{ |
98
|
2 |
|
$_aReturn[] = new ChronosJobEntity($_aJobData); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
102
|
2 |
|
return $_aReturn; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @param ChronosJobEntity|JobEntityInterface $oJobEntity |
107
|
|
|
* @return bool |
108
|
|
|
*/ |
109
|
2 |
|
public function addJob(JobEntityInterface $oJobEntity) |
110
|
|
|
{ |
111
|
2 |
|
return $this->hasAddOrUpdateJob(self::API_CALL_ADD, $oJobEntity); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @param ChronosJobEntity|JobEntityInterface $oJobEntity |
116
|
|
|
* @return bool |
117
|
|
|
*/ |
118
|
2 |
|
public function updateJob(JobEntityInterface $oJobEntity) |
119
|
|
|
{ |
120
|
2 |
|
return $this->hasAddOrUpdateJob(self::API_CALL_UPDATE, $oJobEntity); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @param ChronosJobEntity|JobEntityInterface $oJobEntity |
126
|
|
|
* @return bool |
127
|
|
|
*/ |
128
|
2 |
View Code Duplication |
public function removeJob(JobEntityInterface $oJobEntity) |
|
|
|
|
129
|
|
|
{ |
130
|
2 |
|
if ($this->oApiClient->removeJob($oJobEntity->getKey())) |
131
|
|
|
{ |
132
|
1 |
|
$this->bCacheHasToDelete = true; |
133
|
1 |
|
return true; |
134
|
|
|
} |
135
|
|
|
|
136
|
2 |
|
return false; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @param JobEntityInterface $oJobEntity |
141
|
|
|
* @return bool |
142
|
|
|
*/ |
143
|
4 |
|
private function hasValidate(JobEntityInterface $oJobEntity) |
144
|
|
|
{ |
145
|
|
|
|
146
|
4 |
|
$_aInvalidProperties = $this->oJobEntityValidatorService->getInvalidProperties($oJobEntity); |
147
|
4 |
|
if (empty($_aInvalidProperties)) |
148
|
|
|
{ |
149
|
2 |
|
return true; |
150
|
|
|
} |
151
|
|
|
|
152
|
2 |
|
$this->oLogger->warning( |
153
|
|
|
sprintf( |
154
|
2 |
|
"Can't update job '%s'", |
155
|
2 |
|
$oJobEntity->getKey() |
156
|
|
|
) |
157
|
|
|
); |
158
|
2 |
|
$this->oLogger->warning( |
159
|
|
|
sprintf( |
160
|
2 |
|
"The following job entity properties are not valid:\n%s", |
161
|
2 |
|
implode(', ', $_aInvalidProperties) |
162
|
|
|
) |
163
|
|
|
); |
164
|
|
|
|
165
|
2 |
|
return false; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* @return array |
170
|
|
|
*/ |
171
|
2 |
|
private function getJobList() |
172
|
|
|
{ |
173
|
2 |
|
$_aResult = $this->oCache->get(self::CACHE_KEY_JOB_LIST); |
174
|
|
|
|
175
|
2 |
|
if (is_array($_aResult)) |
176
|
|
|
{ |
177
|
|
|
// return list from cache |
178
|
1 |
|
return $_aResult; |
179
|
|
|
} |
180
|
|
|
|
181
|
1 |
|
$_aResult = $this->oApiClient->listingJobs(); |
182
|
1 |
|
if (!empty($_aResult)) |
183
|
|
|
{ |
184
|
|
|
// set result to cache |
185
|
1 |
|
$this->oCache->set(self::CACHE_KEY_JOB_LIST, $_aResult, self::CACHE_TIME_JOB_LIST); |
186
|
|
|
} |
187
|
|
|
|
188
|
1 |
|
return $_aResult; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* @param string $sApiMethod |
193
|
|
|
* @param JobEntityInterface $oJobEntity |
194
|
|
|
* @return bool |
195
|
|
|
*/ |
196
|
4 |
|
private function hasAddOrUpdateJob($sApiMethod, JobEntityInterface $oJobEntity) |
197
|
|
|
{ |
198
|
4 |
|
if ($this->hasValidate($oJobEntity)) |
199
|
|
|
{ |
200
|
2 |
|
if ($this->oApiClient->{$sApiMethod}($oJobEntity)) |
201
|
|
|
{ |
202
|
2 |
|
$this->bCacheHasToDelete = true; |
203
|
2 |
|
return true; |
204
|
|
|
} |
205
|
|
|
} |
206
|
|
|
|
207
|
2 |
|
return false; |
208
|
|
|
} |
209
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.