forked from thelia-modules/PopIn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PopInCampaignAction.php
52 lines (46 loc) · 1.6 KB
/
PopInCampaignAction.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<?php
namespace PopIn\Action;
use PopIn\Action\Base\PopInCampaignAction as BasePopInCampaignAction;
use PopIn\Event\PopInCampaignEvent;
use PopIn\Model\PopInCampaign;
use Thelia\Model\ContentQuery;
/**
* Pop-in campaign actions.
*/
class PopInCampaignAction extends BasePopInCampaignAction
{
protected function createOrUpdate(PopInCampaignEvent $event, PopInCampaign $model)
{
$this->validateCampaignDates($event->getStart(), $event->getEnd());
$this->validateContentSource($event->getContentSourceType(), $event->getContentSourceId());
parent::createOrUpdate($event, $model);
}
/**
* Check that a pop-in campaign dates are valid.
* @param \DateTime $start
* @param \DateTime $end
* @throws \InvalidArgumentException
*/
protected function validateCampaignDates(\DateTime $start = null, \DateTime $end = null)
{
if ($start !== null && $end !== null && $start > $end) {
throw new \InvalidArgumentException('Pop-in campaign starts after it ends.');
}
}
/**
* Check that a pop-campaign content source is valid.
* @param $contentSourceType
* @param $contentSourceId
* @throws \InvalidArgumentException
*/
protected function validateContentSource($contentSourceType, $contentSourceId)
{
switch ($contentSourceType) {
case 'content':
if (null === ContentQuery::create()->findPk($contentSourceId)) {
throw new \InvalidArgumentException('No content with id ' . $contentSourceId);
}
break;
}
}
}