1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @package: chapi |
4
|
|
|
* |
5
|
|
|
* @author: msiebeneicher |
6
|
|
|
* @since: 2016-11-10 |
7
|
|
|
* |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Chapi\Service\JobValidator\PropertyValidator; |
11
|
|
|
|
12
|
|
|
use Chapi\Component\DatePeriod\DatePeriodFactoryInterface; |
13
|
|
|
use Chapi\Entity\Chronos\ChronosJobEntity; |
14
|
|
|
use Chapi\Entity\Chronos\JobEntity; |
15
|
|
|
use Chapi\Entity\JobEntityInterface; |
16
|
|
|
use Chapi\Service\JobValidator\PropertyValidatorInterface; |
17
|
|
|
|
18
|
|
|
class Schedule extends AbstractPropertyValidator implements PropertyValidatorInterface |
19
|
|
|
{ |
20
|
|
|
const DIC_NAME = 'ScheduleValidator'; |
21
|
|
|
const MESSAGE_TEMPLATE = '"%s" is not a valid ISO8601 string and/or DatePeriodFactory is not able to create a valid DatePeriod'; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var DatePeriodFactoryInterface |
25
|
|
|
*/ |
26
|
|
|
private $datePeriodFactory; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Epsilon constructor. |
30
|
|
|
* @param DatePeriodFactoryInterface $datePeriodFactory |
31
|
|
|
*/ |
32
|
5 |
|
public function __construct(DatePeriodFactoryInterface $datePeriodFactory) |
33
|
|
|
{ |
34
|
5 |
|
$this->datePeriodFactory = $datePeriodFactory; |
35
|
5 |
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @inheritDoc |
39
|
|
|
*/ |
40
|
5 |
|
public function isValid($property, JobEntityInterface $jobEntity) |
41
|
|
|
{ |
42
|
5 |
|
return $this->returnIsValidHelper( |
43
|
5 |
|
$this->isSchedulePropertyValid($jobEntity), |
44
|
|
|
$property, |
45
|
5 |
|
self::MESSAGE_TEMPLATE |
46
|
|
|
); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param JobEntityInterface $jobEntity |
51
|
|
|
* @return bool |
52
|
|
|
*/ |
53
|
5 |
|
private function isSchedulePropertyValid(JobEntityInterface $jobEntity) |
54
|
|
|
{ |
55
|
5 |
|
if (!$jobEntity instanceof ChronosJobEntity) { |
56
|
|
|
throw new \RuntimeException('Required ChronosJobEntity. Something else found.'); |
57
|
|
|
} |
58
|
|
|
|
59
|
5 |
|
if (empty($jobEntity->schedule) && !empty($jobEntity->parents)) { |
60
|
1 |
|
return true; |
61
|
|
|
} |
62
|
|
|
|
63
|
5 |
|
if (!empty($jobEntity->schedule) && empty($jobEntity->parents)) { |
64
|
|
|
try { |
65
|
4 |
|
$datePeriod = $this->datePeriodFactory->createDatePeriod($jobEntity->schedule, $jobEntity->scheduleTimeZone); |
66
|
3 |
|
return (false !== $datePeriod); |
67
|
1 |
|
} catch (\Exception $exception) { |
68
|
|
|
// invalid: Iso8601 is not valid and/or DatePeriodFactory is able to create a valid DatePeriod |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
4 |
|
return false; |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|