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 $jobIndexService; |
23
|
|
|
|
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var JobComparisonInterface |
27
|
|
|
*/ |
28
|
|
|
protected $jobComparisonBusinessCase; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var LoggerInterface |
32
|
|
|
*/ |
33
|
|
|
protected $logger; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var JobRepositoryInterface |
37
|
|
|
*/ |
38
|
|
|
protected $jobRepositoryRemote; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var JobRepositoryInterface |
42
|
|
|
*/ |
43
|
|
|
protected $jobRepositoryLocal; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @inheritdoc |
47
|
|
|
*/ |
48
|
7 |
|
public function storeJobsToLocalRepository(array $entityNames = [], $forceOverwrite = false) |
49
|
|
|
{ |
50
|
7 |
|
if (empty($entityNames)) { |
51
|
3 |
|
$remoteEntities = $this->jobRepositoryRemote->getJobs(); |
52
|
|
|
} else { |
53
|
4 |
|
$remoteEntities = []; |
54
|
4 |
|
foreach ($entityNames as $jobName) { |
55
|
4 |
|
$remoteEntities[] = $this->jobRepositoryRemote->getJob($jobName); |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** @var JobEntityInterface $remoteEntity */ |
60
|
7 |
|
foreach ($remoteEntities as $remoteEntity) { |
61
|
7 |
|
$localEntity = $this->jobRepositoryLocal->getJob($remoteEntity->getKey()); |
62
|
|
|
// new job |
63
|
7 |
|
if (null == $localEntity) { |
64
|
3 |
|
$this->addJobInLocalRepository($remoteEntity); |
65
|
|
|
} else { |
66
|
|
|
//update |
67
|
7 |
|
$this->updateJobInLocalRepository($remoteEntity, $forceOverwrite); |
68
|
|
|
} |
69
|
|
|
} |
70
|
5 |
|
} |
71
|
|
|
|
72
|
3 |
|
protected function addJobInLocalRepository(JobEntityInterface $appRemote) |
73
|
|
|
{ |
74
|
3 |
|
if ($this->jobRepositoryLocal->addJob($appRemote)) { |
75
|
2 |
|
$this->logger->notice(sprintf( |
76
|
2 |
|
'Entity %s stored in local repository', |
77
|
2 |
|
$appRemote->getKey() |
78
|
|
|
)); |
79
|
|
|
} else { |
80
|
1 |
|
$this->logger->error(sprintf( |
81
|
1 |
|
'Failed to store %s in local repository', |
82
|
1 |
|
$appRemote->getKey() |
83
|
|
|
)); |
84
|
|
|
} |
85
|
3 |
|
} |
86
|
|
|
|
87
|
|
|
|
88
|
7 |
|
protected function updateJobInLocalRepository(JobEntityInterface $appRemote, $forceOverwrite) |
89
|
|
|
{ |
90
|
7 |
|
$diff = $this->jobComparisonBusinessCase->getJobDiff($appRemote->getKey()); |
91
|
7 |
|
if (!empty($diff)) { |
92
|
5 |
|
if (!$forceOverwrite) { |
93
|
2 |
|
throw new \InvalidArgumentException( |
94
|
2 |
|
sprintf( |
95
|
2 |
|
'The entity "%s" already exist in your local repository. Use the "force" option to overwrite the job', |
96
|
2 |
|
$appRemote->getKey() |
97
|
|
|
) |
98
|
|
|
); |
99
|
|
|
} |
100
|
|
|
|
101
|
3 |
|
if ($this->jobRepositoryLocal->updateJob($appRemote)) { |
102
|
2 |
|
$this->logger->notice(sprintf( |
103
|
2 |
|
'Entity %s is updated in local repository', |
104
|
2 |
|
$appRemote->getKey() |
105
|
|
|
)); |
106
|
|
|
} else { |
107
|
1 |
|
$this->logger->error(sprintf( |
108
|
1 |
|
'Failed to update app %s in local repository', |
109
|
1 |
|
$appRemote->getKey() |
110
|
|
|
)); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
// remove job from index in case off added in the past |
114
|
3 |
|
$this->jobIndexService->removeJob($appRemote->getKey()); |
115
|
|
|
} |
116
|
6 |
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @inheritdoc |
120
|
|
|
*/ |
121
|
|
|
public function isJobAvailable($jobName) |
122
|
|
|
{ |
123
|
|
|
$locallyAvailable = $this->jobRepositoryLocal->getJob($jobName) ? true : false; |
124
|
|
|
$remotelyAvailable = $this->jobRepositoryRemote->getJob($jobName) ? true : false; |
125
|
|
|
return $locallyAvailable || $remotelyAvailable; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @inheritdoc |
131
|
|
|
*/ |
132
|
|
|
abstract public function storeIndexedJobs(); |
133
|
|
|
} |
134
|
|
|
|