File size: 2,418 Bytes
d2897cd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
<?php

namespace Mautic\LeadBundle\Entity;

use Mautic\CoreBundle\Entity\CommonRepository;

/**
 * @extends CommonRepository<Import>
 */
class ImportRepository extends CommonRepository
{
    /**
     * Count how many imports with the status is there.
     *
     * @param float $ghostDelay when is the import ghost? In hours
     * @param int   $limit
     *
     * @return array
     */
    public function getGhostImports($ghostDelay = 2, $limit = null)
    {
        $q = $this->getQueryForStatuses([Import::IN_PROGRESS]);
        $q->select($this->getTableAlias())
            ->andWhere($q->expr()->lt($this->getTableAlias().'.dateModified', ':delay'))
            ->setParameter('delay', (new \DateTime())->modify('-'.$ghostDelay.' hours'));

        if (null !== $limit) {
            $q->setFirstResult(0)
                ->setMaxResults($limit);
        }

        return $q->getQuery()->getResult();
    }

    /**
     * Count how many imports with the status is there.
     *
     * @param int $limit
     *
     * @return array
     */
    public function getImportsWithStatuses(array $statuses, $limit = null)
    {
        $q = $this->getQueryForStatuses($statuses);
        $q->select($this->getTableAlias())
            ->orderBy($this->getTableAlias().'.priority', 'ASC')
            ->addOrderBy($this->getTableAlias().'.dateAdded', 'DESC');

        if (null !== $limit) {
            $q->setFirstResult(0)
                ->setMaxResults($limit);
        }

        return $q->getQuery()->getResult();
    }

    /**
     * Count how many imports with the status is there.
     */
    public function countImportsWithStatuses(array $statuses): int
    {
        $q = $this->getQueryForStatuses($statuses);
        $q->select('COUNT(DISTINCT '.$this->getTableAlias().'.id) as theCount');

        $results = $q->getQuery()->getSingleResult();

        if (isset($results['theCount'])) {
            return (int) $results['theCount'];
        }

        return 0;
    }

    public function countImportsInProgress(): int
    {
        return $this->countImportsWithStatuses([Import::IN_PROGRESS]);
    }

    public function getQueryForStatuses($statuses)
    {
        $q = $this->createQueryBuilder($this->getTableAlias());

        return $q->where($q->expr()->in($this->getTableAlias().'.status', $statuses));
    }

    public function getTableAlias(): string
    {
        return 'i';
    }
}