Completed
Push — master ( 836114...b3a960 )
by Matthew
08:40
created

LiveJobsGridSource::getRunningQueryBuilder()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 8
cts 8
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 8
nc 1
nop 0
crap 1
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()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
63
    {
64 2
        parent::autoDiscoverColumns();
65 2
        if ($this->columns) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->columns of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
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()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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