Completed
Push — develop ( 9659b8...659b85 )
by Mathias
13:02
created

LoadExpiredJobsToPurge::onFetchList()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.9
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
/**
3
 * YAWIK
4
 *
5
 * @filesource
6
 * @license MIT
7
 * @copyright  2013 - 2018 Cross Solution <http://cross-solution.de>
8
 */
9
  
10
/** */
11
namespace Jobs\Listener;
12
13
use Core\Service\EntityEraser\LoadEvent;
14
use Jobs\Entity\StatusInterface;
15
16
/**
17
 * ${CARET}
18
 * 
19
 * @author Mathias Gelhausen <[email protected]>
20
 * @todo write test 
21
 */
22
class LoadExpiredJobsToPurge 
23
{
24
    const EVENT_NAME = 'expired-jobs';
25
26
    public function __invoke(LoadEvent $event)
27
    {
28
        /* @var \Jobs\Repository\Job $repository */
29
        $days = $event->getParam('days', 80);
30
        $date = new \DateTime("- $days days");
31
        $repository = $event->getRepository('Jobs');
32
        $qb = $repository->createQueryBuilder();
33
        $qb->field('status.name')->notEqual(StatusInterface::ACTIVE);
34
        $qb->addOr(
35
            $qb->expr()->addAnd(
36
                $qb->expr()->field('datePublishEnd')->exists(true),
37
                $qb->expr()->field('datePublishEnd.date')->lt($date)
38
            ),
39
            $qb->expr()->addAnd(
40
                $qb->expr()->field('datePublishEnd')->exists(false),
41
                $qb->expr()->field('dateCreated.date')->lt($date)
42
            )
43
        )->limit($event->getParam('limit', 0));
44
        $entities = $qb->getQuery()->execute()->toArray();
45
46
        return $entities;
47
48
    }
49
50
    public function onFetchList(LoadEvent $event)
51
    {
52
        return [
53
            'key' => self::EVENT_NAME,
54
            'options' => [
55
                'days' => 'Amount of days that must have passed beyond the publishEndDate (default 80)',
56
                'limit' => 'Maximum amount of jobs to process.'
57
            ],
58
            'description' => 'Purges all expired jobs older than a specified range.'
59
        ];
60
    }
61
}
62