|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @package: chapi |
|
4
|
|
|
* |
|
5
|
|
|
* @author: bthapaliya |
|
6
|
|
|
* @since: 2017-01-18 |
|
7
|
|
|
* |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace Chapi\BusinessCase\Comparison; |
|
11
|
|
|
|
|
12
|
|
|
use Chapi\Component\Comparison\DiffCompareInterface; |
|
13
|
|
|
use Chapi\Component\DatePeriod\DatePeriodFactoryInterface; |
|
14
|
|
|
use Chapi\Entity\Chronos\JobCollection; |
|
15
|
|
|
use Chapi\Entity\Chronos\ChronosJobEntity; |
|
16
|
|
|
use Chapi\Entity\JobEntityInterface; |
|
17
|
|
|
use Chapi\Service\JobRepository\JobRepositoryInterface; |
|
18
|
|
|
use Psr\Log\LoggerInterface; |
|
19
|
|
|
use Symfony\Component\Config\Definition\Exception\Exception; |
|
20
|
|
|
|
|
21
|
|
|
class CompositeJobComparisonBusinessCase implements JobComparisonInterface |
|
22
|
|
|
{ |
|
23
|
|
|
/** @var JobComparisonInterface[] */ |
|
24
|
|
|
private $composites = []; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @param JobComparisonInterface $comparer |
|
28
|
|
|
*/ |
|
29
|
4 |
|
public function addComparisonCases(JobComparisonInterface $comparer) |
|
30
|
|
|
{ |
|
31
|
4 |
|
$this->composites[] = $comparer; |
|
32
|
4 |
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @return array |
|
36
|
|
|
*/ |
|
37
|
1 |
|
public function getLocalMissingJobs() |
|
38
|
|
|
{ |
|
39
|
1 |
|
$missingJobs = array(); |
|
40
|
1 |
|
foreach ($this->composites as $jobComparers) { |
|
41
|
1 |
|
$missing = $jobComparers->getLocalMissingJobs(); |
|
42
|
1 |
|
$missingJobs = array_merge($missingJobs, $missing); |
|
43
|
|
|
} |
|
44
|
1 |
|
return $missingJobs; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @return array |
|
49
|
|
|
*/ |
|
50
|
1 |
|
public function getRemoteMissingJobs() |
|
51
|
|
|
{ |
|
52
|
1 |
|
$chronosMissingJobs = array(); |
|
53
|
1 |
|
foreach ($this->composites as $jobComparers) { |
|
54
|
1 |
|
$chronosMissingJobs = array_merge($chronosMissingJobs, $jobComparers->getRemoteMissingJobs()); |
|
55
|
|
|
} |
|
56
|
1 |
|
return $chronosMissingJobs; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @return array |
|
61
|
|
|
*/ |
|
62
|
1 |
|
public function getLocalJobUpdates() |
|
63
|
|
|
{ |
|
64
|
1 |
|
$localJobUpdates = array(); |
|
65
|
1 |
|
foreach ($this->composites as $jobComparers) { |
|
66
|
1 |
|
$localJobUpdates = array_merge($localJobUpdates, $jobComparers->getLocalJobUpdates()); |
|
67
|
|
|
} |
|
68
|
1 |
|
return $localJobUpdates; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @param string $jobName |
|
73
|
|
|
* @return array |
|
74
|
|
|
*/ |
|
75
|
1 |
|
public function getJobDiff($jobName) |
|
76
|
|
|
{ |
|
77
|
1 |
|
$jobDiffs = array(); |
|
78
|
1 |
|
foreach ($this->composites as $jobComparers) { |
|
79
|
|
|
// assuming same name won't be in all subsystems. |
|
80
|
|
|
// TODO: add support to handle the duplicate names in different subsystems |
|
81
|
1 |
|
if ($jobComparers->isJobAvailable($jobName)) { |
|
82
|
1 |
|
$jobDiffs = $jobComparers->getJobDiff($jobName); |
|
83
|
1 |
|
break; |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
1 |
|
return $jobDiffs; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* @param JobEntityInterface|ChronosJobEntity $jobEntityA |
|
91
|
|
|
* @param JobEntityInterface|ChronosJobEntity $jobEntityB |
|
92
|
|
|
* @return bool |
|
93
|
|
|
*/ |
|
94
|
|
|
public function hasSameJobType(JobEntityInterface $jobEntityA, JobEntityInterface $jobEntityB) |
|
95
|
|
|
{ |
|
96
|
|
|
if ($jobEntityA->getEntityType() != $jobEntityB->getEntityType()) { |
|
97
|
|
|
throw new Exception('type compared for different entity types.'); |
|
98
|
|
|
} |
|
99
|
|
|
/** @var JobComparisonInterface $comparer */ |
|
100
|
|
|
$comparer = null; |
|
101
|
|
|
foreach ($this->composites as $child) { |
|
102
|
|
|
if ($child->isJobAvailable($jobEntityA->getKey())) { |
|
103
|
|
|
$comparer = $child; |
|
104
|
|
|
break; |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
if ($comparer == null) { |
|
109
|
|
|
throw new Exception(sprintf('could not find appropriate comparision businesscase to operate', $jobEntityA->getKey())); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
return $comparer->hasSameJobType($jobEntityA, $jobEntityB); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* @param $jobName |
|
118
|
|
|
* @return bool |
|
119
|
|
|
*/ |
|
120
|
|
|
public function isJobAvailable($jobName) |
|
121
|
|
|
{ |
|
122
|
|
|
foreach ($this->composites as $child) { |
|
123
|
|
|
if ($child->isJobAvailable($jobName)) { |
|
124
|
|
|
return true; |
|
125
|
|
|
} |
|
126
|
|
|
} |
|
127
|
|
|
return false; |
|
128
|
|
|
} |
|
129
|
|
|
} |
|
130
|
|
|
|