1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: bthapaliya |
5
|
|
|
* Date: 15/02/17 |
6
|
|
|
* Time: 13:17 |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Chapi\BusinessCase\JobManagement; |
10
|
|
|
|
11
|
|
|
use Chapi\BusinessCase\Comparison\JobComparisonInterface; |
12
|
|
|
use Chapi\Entity\JobEntityInterface; |
13
|
|
|
use Chapi\Service\JobIndex\JobIndexServiceInterface; |
14
|
|
|
use Chapi\Service\JobRepository\JobRepositoryInterface; |
15
|
|
|
use Psr\Log\LoggerInterface; |
16
|
|
|
|
17
|
|
|
abstract class AbstractStoreJobBusinessCase implements StoreJobBusinessCaseInterface |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var JobIndexServiceInterface |
21
|
|
|
*/ |
22
|
|
|
protected $oJobIndexService; |
23
|
|
|
|
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var JobComparisonInterface |
27
|
|
|
*/ |
28
|
|
|
protected $oJobComparisonBusinessCase; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var LoggerInterface |
32
|
|
|
*/ |
33
|
|
|
protected $oLogger; |
34
|
|
|
|
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var JobRepositoryInterface |
38
|
|
|
*/ |
39
|
|
|
protected $oJobRepositoryRemote; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var JobRepositoryInterface |
43
|
|
|
*/ |
44
|
|
|
protected $oJobRepositoryLocal; |
45
|
|
|
|
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @inheritdoc |
49
|
|
|
*/ |
50
|
7 |
|
public function storeJobsToLocalRepository(array $aEntityNames = [], $bForceOverwrite = false) |
51
|
|
|
{ |
52
|
7 |
|
if (empty($aEntityNames)) |
53
|
7 |
|
{ |
54
|
3 |
|
$_aRemoteEntities = $this->oJobRepositoryRemote->getJobs(); |
55
|
3 |
|
} |
56
|
|
|
else |
57
|
|
|
{ |
58
|
4 |
|
$_aRemoteEntities = []; |
59
|
4 |
|
foreach ($aEntityNames as $_sJobName) |
60
|
|
|
{ |
61
|
4 |
|
$_aRemoteEntities[] = $this->oJobRepositoryRemote->getJob($_sJobName); |
62
|
4 |
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** @var JobEntityInterface $_oRemoteEntity */ |
66
|
7 |
|
foreach ($_aRemoteEntities as $_oRemoteEntity) |
67
|
|
|
{ |
68
|
7 |
|
$_oLocalEntity = $this->oJobRepositoryLocal->getJob($_oRemoteEntity->getKey()); |
69
|
|
|
// new job |
70
|
7 |
|
if (null == $_oLocalEntity) |
71
|
7 |
|
{ |
72
|
3 |
|
$this->addJobInLocalRepository($_oRemoteEntity); |
73
|
3 |
|
} else { |
74
|
|
|
//update |
75
|
7 |
|
$this->updateJobInLocalRepository($_oRemoteEntity, $bForceOverwrite); |
76
|
|
|
} |
77
|
6 |
|
} |
78
|
5 |
|
} |
79
|
|
|
|
80
|
3 |
|
protected function addJobInLocalRepository(JobEntityInterface $oAppRemote) |
81
|
|
|
{ |
82
|
3 |
|
if ($this->oJobRepositoryLocal->addJob($oAppRemote)) |
83
|
3 |
|
{ |
84
|
2 |
|
$this->oLogger->notice(sprintf( |
85
|
2 |
|
'Entity %s stored in local repository', |
86
|
2 |
|
$oAppRemote->getKey() |
87
|
2 |
|
)); |
88
|
2 |
|
} |
89
|
|
|
else { |
90
|
1 |
|
$this->oLogger->error(sprintf( |
91
|
1 |
|
'Failed to store %s in local repository', |
92
|
1 |
|
$oAppRemote->getKey() |
93
|
1 |
|
)); |
94
|
|
|
} |
95
|
3 |
|
} |
96
|
|
|
|
97
|
|
|
|
98
|
7 |
|
protected function updateJobInLocalRepository(JobEntityInterface $oAppRemote, $bForceOverwrite) |
99
|
|
|
{ |
100
|
7 |
|
$_aDiff = $this->oJobComparisonBusinessCase->getJobDiff($oAppRemote->getKey()); |
101
|
7 |
|
if (!empty($_aDiff)) |
102
|
7 |
|
{ |
103
|
5 |
|
if (!$bForceOverwrite) |
104
|
5 |
|
{ |
105
|
2 |
|
throw new \InvalidArgumentException( |
106
|
2 |
|
sprintf( |
107
|
2 |
|
'The entity "%s" already exist in your local repository. Use the "force" option to overwrite the job', |
108
|
2 |
|
$oAppRemote->getKey() |
109
|
2 |
|
) |
110
|
2 |
|
); |
111
|
|
|
} |
112
|
|
|
|
113
|
3 |
|
if ($this->oJobRepositoryLocal->updateJob($oAppRemote)) |
114
|
3 |
|
{ |
115
|
2 |
|
$this->oLogger->notice(sprintf( |
116
|
2 |
|
'Entity %s is updated in local repository', |
117
|
2 |
|
$oAppRemote->getKey() |
118
|
2 |
|
)); |
119
|
2 |
|
} |
120
|
|
|
else { |
121
|
1 |
|
$this->oLogger->error(sprintf( |
122
|
1 |
|
'Failed to update app %s in local repository', |
123
|
1 |
|
$oAppRemote->getKey() |
124
|
1 |
|
)); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
// remove job from index in case off added in the past |
128
|
3 |
|
$this->oJobIndexService->removeJob($oAppRemote->getKey()); |
129
|
3 |
|
} |
130
|
6 |
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @inheritdoc |
134
|
|
|
*/ |
135
|
|
|
public function isJobAvailable($sJobName) |
136
|
|
|
{ |
137
|
|
|
$_bLocallyAvailable = $this->oJobRepositoryLocal->getJob($sJobName) ? true : false; |
138
|
|
|
$_bRemotelyAvailable = $this->oJobRepositoryRemote->getJob($sJobName) ? true : false; |
139
|
|
|
return $_bLocallyAvailable || $_bRemotelyAvailable; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @inheritdoc |
145
|
|
|
*/ |
146
|
|
|
abstract public function storeIndexedJobs(); |
147
|
|
|
} |