1 | <?php |
||||
2 | |||||
3 | namespace Dtc\QueueBundle\ORM; |
||||
4 | |||||
5 | use Doctrine\ORM\EntityManager; |
||||
6 | use Dtc\GridBundle\Grid\Column\GridColumn; |
||||
7 | use Dtc\GridBundle\Grid\Source\EntityGridSource; |
||||
8 | use Dtc\QueueBundle\Model\BaseJob; |
||||
9 | |||||
10 | class LiveJobsGridSource extends EntityGridSource |
||||
11 | { |
||||
12 | protected $jobManager; |
||||
13 | protected $running = false; |
||||
14 | |||||
15 | 2 | public function getId() |
|||
16 | { |
||||
17 | 2 | return 'dtc_queue.grid_source.jobs_'.($this->isRunning() ? 'running' : 'waiting').'.orm'; |
|||
18 | } |
||||
19 | |||||
20 | 10 | public function __construct(JobManager $jobManager) |
|||
21 | { |
||||
22 | 10 | $this->jobManager = $jobManager; |
|||
23 | /** @var EntityManager $entityManager */ |
||||
24 | 10 | $entityManager = $jobManager->getObjectManager(); |
|||
25 | 10 | parent::__construct($entityManager, $jobManager->getJobClass()); |
|||
26 | 10 | } |
|||
27 | |||||
28 | 3 | public function getColumns() |
|||
29 | { |
||||
30 | 3 | if ($columns = parent::getColumns()) { |
|||
31 | 2 | return $columns; |
|||
32 | } |
||||
33 | 3 | $this->autoDiscoverColumns(); |
|||
34 | |||||
35 | 3 | return parent::getColumns(); |
|||
36 | } |
||||
37 | |||||
38 | /** |
||||
39 | * @param bool $flag |
||||
40 | */ |
||||
41 | 10 | public function setRunning($flag) |
|||
42 | { |
||||
43 | 10 | $this->running = $flag; |
|||
44 | 10 | } |
|||
45 | |||||
46 | 3 | public function isRunning() |
|||
47 | { |
||||
48 | 3 | return $this->running; |
|||
49 | } |
||||
50 | |||||
51 | 1 | protected function getRunningQueryBuilder() |
|||
52 | { |
||||
53 | /** @var EntityManager $entityManager */ |
||||
54 | 1 | $entityManager = $this->jobManager->getObjectManager(); |
|||
55 | 1 | $queryBuilder = $entityManager->createQueryBuilder()->add('select', 'j'); |
|||
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||
56 | 1 | $queryBuilder->from($this->jobManager->getJobClass(), 'j'); |
|||
57 | 1 | $queryBuilder->where('j.status = :status')->setParameter(':status', BaseJob::STATUS_RUNNING); |
|||
58 | 1 | $queryBuilder->orderBy('j.startedAt', 'DESC'); |
|||
59 | 1 | $queryBuilder->setFirstResult($this->offset) |
|||
60 | 1 | ->setMaxResults($this->limit); |
|||
61 | |||||
62 | 1 | return $queryBuilder; |
|||
63 | } |
||||
64 | |||||
65 | 1 | protected function getQueryBuilder() |
|||
66 | { |
||||
67 | 1 | if ($this->isRunning()) { |
|||
68 | 1 | return $this->getRunningQueryBuilder(); |
|||
69 | } |
||||
70 | |||||
71 | 1 | $queryBuilder = $this->jobManager->getJobQueryBuilder(); |
|||
72 | 1 | $queryBuilder->add('select', 'j'); |
|||
0 ignored issues
–
show
'j' of type string is incompatible with the type array|object expected by parameter $dqlPart of Doctrine\ORM\QueryBuilder::add() .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
73 | 1 | $queryBuilder->setFirstResult($this->offset) |
|||
74 | 1 | ->setMaxResults($this->limit); |
|||
75 | |||||
76 | 1 | return $queryBuilder; |
|||
77 | } |
||||
78 | |||||
79 | 3 | public function autoDiscoverColumns() |
|||
80 | { |
||||
81 | 3 | parent::autoDiscoverColumns(); |
|||
82 | 3 | if ($this->columns) { |
|||
83 | 3 | foreach ($this->columns as $column) { |
|||
84 | 3 | if ($column instanceof GridColumn) { |
|||
85 | 3 | $column->setOption('sortable', false); |
|||
86 | } |
||||
87 | } |
||||
88 | } |
||||
89 | 3 | } |
|||
90 | |||||
91 | 3 | public function getDefaultSort() |
|||
92 | { |
||||
93 | 3 | return null; |
|||
94 | } |
||||
95 | |||||
96 | 1 | public function getCount() |
|||
97 | { |
||||
98 | 1 | $qb = $this->getQueryBuilder(); |
|||
99 | 1 | $qb->resetDQLPart('orderBy'); |
|||
100 | 1 | $qb->add('select', 'count(j)') |
|||
0 ignored issues
–
show
'count(j)' of type string is incompatible with the type array|object expected by parameter $dqlPart of Doctrine\ORM\QueryBuilder::add() .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
101 | 1 | ->setFirstResult(null) |
|||
102 | 1 | ->setMaxResults(null); |
|||
103 | |||||
104 | 1 | return $qb->getQuery() |
|||
105 | 1 | ->getSingleScalarResult(); |
|||
106 | } |
||||
107 | } |
||||
108 |