1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dtc\QueueBundle\ODM; |
4
|
|
|
|
5
|
|
|
use Doctrine\ODM\MongoDB\DocumentManager; |
6
|
|
|
use Dtc\GridBundle\Grid\Column\GridColumn; |
7
|
|
|
use Dtc\GridBundle\Grid\Source\DocumentGridSource; |
8
|
|
|
use Dtc\QueueBundle\Model\BaseJob; |
9
|
|
|
|
10
|
|
|
class LiveJobsGridSource extends DocumentGridSource |
11
|
|
|
{ |
12
|
|
|
protected $jobManager; |
13
|
|
|
protected $running = false; |
14
|
|
|
|
15
|
1 |
|
public function getId() |
16
|
|
|
{ |
17
|
1 |
|
return 'dtc_queue.grid_source.jobs_'.($this->isRunning() ? 'running' : 'waiting').'.odm'; |
18
|
|
|
} |
19
|
|
|
|
20
|
7 |
|
public function setRunning($flag) |
21
|
|
|
{ |
22
|
7 |
|
$this->running = $flag; |
23
|
7 |
|
} |
24
|
|
|
|
25
|
2 |
|
public function isRunning() |
26
|
|
|
{ |
27
|
2 |
|
return $this->running; |
28
|
|
|
} |
29
|
|
|
|
30
|
1 |
|
protected function getRunningQueryBuilder() |
31
|
|
|
{ |
32
|
|
|
/** @var DocumentManager $documentManager */ |
33
|
1 |
|
$documentManager = $this->jobManager->getObjectManager(); |
34
|
1 |
|
$builder = $documentManager->createQueryBuilder($this->jobManager->getJobClass()); |
35
|
1 |
|
$builder->field('status')->equals(BaseJob::STATUS_RUNNING); |
36
|
1 |
|
$builder->sort('startedAt', 'desc'); |
37
|
1 |
|
$builder->limit($this->limit); |
38
|
1 |
|
$builder->skip($this->offset); |
39
|
|
|
|
40
|
1 |
|
return $builder; |
41
|
|
|
} |
42
|
|
|
|
43
|
7 |
|
public function __construct(JobManager $jobManager) |
44
|
|
|
{ |
45
|
7 |
|
$this->jobManager = $jobManager; |
46
|
|
|
|
47
|
|
|
/** @var DocumentManager $documentManager */ |
48
|
7 |
|
$documentManager = $jobManager->getObjectManager(); |
49
|
7 |
|
parent::__construct($documentManager, $jobManager->getJobClass()); |
50
|
7 |
|
} |
51
|
|
|
|
52
|
2 |
|
public function getColumns() |
53
|
|
|
{ |
54
|
2 |
|
if ($columns = parent::getColumns()) { |
55
|
1 |
|
return $columns; |
56
|
|
|
} |
57
|
2 |
|
$this->autoDiscoverColumns(); |
58
|
|
|
|
59
|
2 |
|
return parent::getColumns(); |
60
|
|
|
} |
61
|
|
|
|
62
|
2 |
View Code Duplication |
public function autoDiscoverColumns() |
|
|
|
|
63
|
|
|
{ |
64
|
2 |
|
parent::autoDiscoverColumns(); |
65
|
2 |
|
if ($this->columns) { |
|
|
|
|
66
|
2 |
|
foreach ($this->columns as $column) { |
67
|
2 |
|
if ($column instanceof GridColumn) { |
68
|
2 |
|
$column->setOption('sortable', false); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
} |
72
|
2 |
|
} |
73
|
|
|
|
74
|
2 |
|
public function getDefaultSort() |
75
|
|
|
{ |
76
|
2 |
|
return null; |
77
|
|
|
} |
78
|
|
|
|
79
|
1 |
View Code Duplication |
protected function getQueryBuilder() |
|
|
|
|
80
|
|
|
{ |
81
|
1 |
|
if ($this->isRunning()) { |
82
|
1 |
|
return $this->getRunningQueryBuilder(); |
83
|
|
|
} |
84
|
1 |
|
$builder = $this->jobManager->getJobQueryBuilder(); |
85
|
1 |
|
$builder->limit($this->limit); |
86
|
1 |
|
$builder->skip($this->offset); |
87
|
|
|
|
88
|
1 |
|
return $builder; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.