|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Validator; |
|
4
|
|
|
|
|
5
|
|
|
use App\Entity\PerformanceEvent; |
|
6
|
|
|
use App\Repository\PerformanceEventRepository; |
|
7
|
|
|
use Symfony\Component\Validator\ConstraintValidator; |
|
8
|
|
|
use Symfony\Component\Validator\Constraint; |
|
9
|
|
|
|
|
10
|
|
|
class TwoPerformanceEventsPerDayValidator extends ConstraintValidator |
|
11
|
|
|
{ |
|
12
|
|
|
const MAX_PERFORMANCE_EVENTS_PER_ONE_DAY = 2; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @var PerformanceEventRepository |
|
16
|
|
|
*/ |
|
17
|
|
|
private $repository; |
|
18
|
|
|
|
|
19
|
8 |
|
public function __construct(PerformanceEventRepository $repository) |
|
20
|
|
|
{ |
|
21
|
8 |
|
$this->repository = $repository; |
|
22
|
8 |
|
} |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @param PerformanceEvent $performanceEvent |
|
26
|
|
|
* @param Constraint $constraint |
|
27
|
|
|
*/ |
|
28
|
8 |
|
public function validate($performanceEvent, Constraint $constraint) |
|
29
|
|
|
{ |
|
30
|
8 |
|
if (false === is_object($performanceEvent->getDateTime())) { |
|
31
|
1 |
|
$this->context->buildViolation($constraint->performance_must_have_a_date) |
|
32
|
1 |
|
->atPath('dateTime') |
|
33
|
1 |
|
->addViolation() |
|
34
|
|
|
; |
|
35
|
|
|
|
|
36
|
1 |
|
return; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
7 |
|
if ($this->isMoreThanMax($performanceEvent)) { |
|
40
|
5 |
|
$this->context->buildViolation($constraint->max_performances_per_day) |
|
41
|
5 |
|
->atPath('dateTime') |
|
42
|
5 |
|
->setParameter('{{count}}', self::MAX_PERFORMANCE_EVENTS_PER_ONE_DAY) |
|
43
|
5 |
|
->addViolation() |
|
44
|
|
|
; |
|
45
|
|
|
} |
|
46
|
7 |
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @param PerformanceEvent $performanceEvent |
|
50
|
|
|
* @return bool |
|
51
|
|
|
*/ |
|
52
|
7 |
|
protected function isMoreThanMax(PerformanceEvent $performanceEvent) |
|
53
|
|
|
{ |
|
54
|
7 |
|
$from = clone $performanceEvent->getDateTime(); |
|
55
|
7 |
|
$from->setTime(00, 00, 00); |
|
56
|
|
|
|
|
57
|
7 |
|
$to = clone $performanceEvent->getDateTime(); |
|
58
|
7 |
|
$to->setTime(23, 59, 59); |
|
59
|
|
|
|
|
60
|
7 |
|
$countPerformanceEventsPerDate = count($this->repository->findByDateRangeAndSlug($from, $to)); |
|
61
|
|
|
|
|
62
|
7 |
|
if ($performanceEvent->getId()) { |
|
63
|
|
|
return $countPerformanceEventsPerDate >= self::MAX_PERFORMANCE_EVENTS_PER_ONE_DAY +1; |
|
64
|
|
|
} else { |
|
65
|
7 |
|
return $countPerformanceEventsPerDate >= self::MAX_PERFORMANCE_EVENTS_PER_ONE_DAY; |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
|