1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @package: chapi |
4
|
|
|
* |
5
|
|
|
* @author: bthapaliya |
6
|
|
|
* @since: 2016-12-02 |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Chapi\Service\JobRepository; |
10
|
|
|
|
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\Entity\Marathon\MarathonAppEntity; |
17
|
|
|
use Chapi\Service\JobValidator\JobValidatorServiceInterface; |
18
|
|
|
use Psr\Log\LoggerInterface; |
19
|
|
|
|
20
|
|
|
class BridgeMarathon implements BridgeInterface |
21
|
|
|
{ |
22
|
|
|
const CACHE_TIME_JOB_LIST = 60; |
23
|
|
|
|
24
|
|
|
const CACHE_KEY_APP_LIST = 'marathon.app.list'; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var \Chapi\Component\RemoteClients\ApiClientInterface |
28
|
|
|
*/ |
29
|
|
|
private $oApiClient; |
30
|
|
|
/** |
31
|
|
|
* @var JobValidatorServiceInterface |
32
|
|
|
*/ |
33
|
|
|
private $oJobEntityValidatorService; |
34
|
|
|
/** |
35
|
|
|
* @var CacheInterface |
36
|
|
|
*/ |
37
|
|
|
private $oCache; |
38
|
|
|
/** |
39
|
|
|
* @var LoggerInterface |
40
|
|
|
*/ |
41
|
|
|
private $oLogger; |
42
|
|
|
|
43
|
|
|
private $bCacheHasToDelete = false; |
44
|
|
|
|
45
|
5 |
View Code Duplication |
public function __construct( |
|
|
|
|
46
|
|
|
ApiClientInterface $oApiClient, |
47
|
|
|
CacheInterface $oCache, |
48
|
|
|
JobValidatorServiceInterface $oJobEntityValidatorService, |
49
|
|
|
LoggerInterface $oLogger |
50
|
|
|
) |
51
|
|
|
{ |
52
|
|
|
|
53
|
5 |
|
$this->oApiClient = $oApiClient; |
54
|
5 |
|
$this->oJobEntityValidatorService = $oJobEntityValidatorService; |
55
|
5 |
|
$this->oCache = $oCache; |
56
|
5 |
|
$this->oLogger = $oLogger; |
57
|
5 |
|
} |
58
|
|
|
|
59
|
5 |
|
public function __destruct() |
60
|
|
|
{ |
61
|
5 |
|
if ($this->bCacheHasToDelete) |
62
|
5 |
|
{ |
63
|
2 |
|
$this->oCache->delete(self::CACHE_KEY_APP_LIST); |
64
|
2 |
|
} |
65
|
5 |
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @return JobEntityInterface[] |
69
|
|
|
*/ |
70
|
1 |
|
public function getJobs() |
71
|
|
|
{ |
72
|
1 |
|
$_aApps = []; |
73
|
1 |
|
$_aJobsList = $this->getJobList(); |
74
|
|
|
|
75
|
1 |
|
if (!empty($_aJobsList)) |
76
|
1 |
|
{ |
77
|
|
|
foreach ($_aJobsList as $_aJobData) |
78
|
|
|
{ |
79
|
|
|
$_aApps[] = new MarathonAppEntity($_aJobData); |
80
|
|
|
} |
81
|
|
|
} |
82
|
1 |
|
return $_aApps; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param JobEntityInterface $oJobEntity |
87
|
|
|
* @return bool |
88
|
|
|
*/ |
89
|
2 |
View Code Duplication |
public function addJob(JobEntityInterface $oJobEntity) |
|
|
|
|
90
|
|
|
{ |
91
|
2 |
|
if ($this->oApiClient->addingJob($oJobEntity)) |
92
|
2 |
|
{ |
93
|
1 |
|
$this->bCacheHasToDelete = true; |
94
|
1 |
|
return true; |
95
|
|
|
} |
96
|
1 |
|
return false; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @param JobEntityInterface $oJobEntity |
101
|
|
|
* @return bool |
102
|
|
|
*/ |
103
|
|
View Code Duplication |
public function updateJob(JobEntityInterface $oJobEntity) |
|
|
|
|
104
|
|
|
{ |
105
|
|
|
if ($this->oApiClient->updatingJob($oJobEntity)) |
106
|
|
|
{ |
107
|
|
|
$this->bCacheHasToDelete = true; |
108
|
|
|
return true; |
109
|
|
|
} |
110
|
|
|
return false; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @param JobEntityInterface $oJobEntity |
115
|
|
|
* @return bool |
116
|
|
|
*/ |
117
|
2 |
View Code Duplication |
public function removeJob(JobEntityInterface $oJobEntity) |
|
|
|
|
118
|
|
|
{ |
119
|
2 |
|
if ($this->oApiClient->removeJob($oJobEntity->getKey())) |
120
|
2 |
|
{ |
121
|
1 |
|
$this->bCacheHasToDelete = true; |
122
|
1 |
|
return true; |
123
|
|
|
} |
124
|
1 |
|
return false; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @return array|mixed |
129
|
|
|
*/ |
130
|
1 |
|
private function getJobList() |
131
|
|
|
{ |
132
|
1 |
|
$_aResult = $this->oCache->get(self::CACHE_KEY_APP_LIST); |
133
|
|
|
|
134
|
1 |
|
if (is_array($_aResult)) |
135
|
1 |
|
{ |
136
|
1 |
|
return $_aResult; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
$_aResult = $this->oApiClient->listingJobs(); |
140
|
|
|
|
141
|
|
|
if (!empty($_aResult['apps'])) |
142
|
|
|
{ |
143
|
|
|
$this->oCache->set(self::CACHE_KEY_APP_LIST, $_aResult['apps'], self::CACHE_TIME_JOB_LIST); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
return $_aResult['apps']; |
147
|
|
|
|
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
} |
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.