1 | <?php |
||
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 | { |
||
54 | 3 | $_aRemoteEntities = $this->oJobRepositoryRemote->getJobs(); |
|
55 | } |
||
56 | else |
||
57 | { |
||
58 | 4 | $_aRemoteEntities = []; |
|
59 | 4 | foreach ($aEntityNames as $_sJobName) |
|
60 | { |
||
61 | 4 | $_aRemoteEntities[] = $this->oJobRepositoryRemote->getJob($_sJobName); |
|
62 | } |
||
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 | { |
||
72 | 3 | $this->addJobInLocalRepository($_oRemoteEntity); |
|
73 | } else { |
||
74 | //update |
||
75 | 7 | $this->updateJobInLocalRepository($_oRemoteEntity, $bForceOverwrite); |
|
76 | } |
||
77 | } |
||
78 | 5 | } |
|
79 | |||
80 | 3 | protected function addJobInLocalRepository(JobEntityInterface $oAppRemote) |
|
81 | { |
||
82 | 3 | if ($this->oJobRepositoryLocal->addJob($oAppRemote)) |
|
83 | { |
||
84 | 2 | $this->oLogger->notice(sprintf( |
|
85 | 2 | 'Entity %s stored in local repository', |
|
86 | 2 | $oAppRemote->getKey() |
|
87 | )); |
||
88 | } |
||
89 | else { |
||
90 | 1 | $this->oLogger->error(sprintf( |
|
91 | 1 | 'Failed to store %s in local repository', |
|
92 | 1 | $oAppRemote->getKey() |
|
93 | )); |
||
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 | { |
||
103 | 5 | if (!$bForceOverwrite) |
|
104 | { |
||
105 | 2 | throw new \InvalidArgumentException( |
|
106 | 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 | ) |
||
110 | ); |
||
111 | } |
||
112 | |||
113 | 3 | if ($this->oJobRepositoryLocal->updateJob($oAppRemote)) |
|
114 | { |
||
115 | 2 | $this->oLogger->notice(sprintf( |
|
116 | 2 | 'Entity %s is updated in local repository', |
|
117 | 2 | $oAppRemote->getKey() |
|
118 | )); |
||
119 | } |
||
120 | else { |
||
121 | 1 | $this->oLogger->error(sprintf( |
|
122 | 1 | 'Failed to update app %s in local repository', |
|
123 | 1 | $oAppRemote->getKey() |
|
124 | )); |
||
125 | } |
||
126 | |||
127 | // remove job from index in case off added in the past |
||
128 | 3 | $this->oJobIndexService->removeJob($oAppRemote->getKey()); |
|
129 | } |
||
130 | 6 | } |
|
131 | |||
132 | /** |
||
133 | * @inheritdoc |
||
134 | */ |
||
135 | public function isJobAvailable($sJobName) |
||
141 | |||
142 | |||
143 | /** |
||
144 | * @inheritdoc |
||
145 | */ |
||
146 | abstract public function storeIndexedJobs(); |
||
147 | } |