Completed
Pull Request — master (#127)
by Matthew
18:43
created

LiveJobsGridSource   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 112
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 3 Features 0
Metric Value
eloc 50
c 3
b 3
f 0
dl 0
loc 112
ccs 51
cts 51
cp 1
rs 10
wmc 18

11 Methods

Rating   Name   Duplication   Size   Complexity  
A setColumns() 0 11 4
A setRunning() 0 3 1
A getId() 0 3 2
A getColumns() 0 16 3
A getCount() 0 10 1
A getRunningQueryBuilder() 0 12 1
A __construct() 0 6 1
A getQueryBuilder() 0 12 2
A getDefaultSort() 0 3 1
A isRunning() 0 3 1
A setColumnSource() 0 2 1
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\ColumnExtractionTrait;
8
use Dtc\GridBundle\Grid\Source\EntityGridSource;
9
use Dtc\QueueBundle\Model\BaseJob;
10
11
class LiveJobsGridSource extends EntityGridSource
12
{
13
    protected $jobManager;
14
    protected $running = false;
15
    private $columnSource;
16
17
    use ColumnExtractionTrait;
0 ignored issues
show
introduced by
The trait Dtc\GridBundle\Grid\Source\ColumnExtractionTrait requires some properties which are not provided by Dtc\QueueBundle\ORM\LiveJobsGridSource: $sortable, $label, $direction, $searchable, $order, $actions, $column, $route, $sort
Loading history...
Deprecated Code introduced by
The trait Dtc\GridBundle\Grid\Source\ColumnExtractionTrait has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

17
    use /** @scrutinizer ignore-deprecated */ ColumnExtractionTrait;

This trait has been deprecated. The supplier of the trait has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the trait will be removed and what other trait to use instead.

Loading history...
18 2
19
    public function getId()
20 2
    {
21
        return 'dtc_queue.grid_source.jobs_'.($this->isRunning() ? 'running' : 'waiting').'.orm';
22
    }
23 10
24
    public function __construct(JobManager $jobManager)
25 10
    {
26
        $this->jobManager = $jobManager;
27 10
        /** @var EntityManager $entityManager */
28 10
        $entityManager = $jobManager->getObjectManager();
29 10
        parent::__construct($entityManager, $jobManager->getJobClass());
30
    }
31
32
    /**
33
     * @param bool $flag
34 10
     */
35
    public function setRunning($flag)
36 10
    {
37 10
        $this->running = $flag;
38
    }
39 3
40
    public function isRunning()
41 3
    {
42
        return $this->running;
43
    }
44 1
45
    public function setColumnSource(\Dtc\GridBundle\Grid\Source\ColumnSource $columnSource) {
46
        $this->columnSource = $columnSource;
47 1
    }
48 1
49 1
    protected function getRunningQueryBuilder()
50 1
    {
51 1
        /** @var EntityManager $entityManager */
52 1
        $entityManager = $this->jobManager->getObjectManager();
53 1
        $queryBuilder = $entityManager->createQueryBuilder()->add('select', 'j');
0 ignored issues
show
Bug introduced by
'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 ignore-type  annotation

53
        $queryBuilder = $entityManager->createQueryBuilder()->add('select', /** @scrutinizer ignore-type */ 'j');
Loading history...
54
        $queryBuilder->from($this->jobManager->getJobClass(), 'j');
55 1
        $queryBuilder->where('j.status = :status')->setParameter(':status', BaseJob::STATUS_RUNNING);
56
        $queryBuilder->orderBy('j.startedAt', 'DESC');
57
        $queryBuilder->setFirstResult($this->offset)
58 1
                        ->setMaxResults($this->limit);
59
60 1
        return $queryBuilder;
61 1
    }
62
63
    protected function getQueryBuilder()
64 1
    {
65 1
        if ($this->isRunning()) {
66 1
            return $this->getRunningQueryBuilder();
67 1
        }
68
69 1
        $queryBuilder = $this->jobManager->getJobQueryBuilder();
70
        $queryBuilder->add('select', 'j');
0 ignored issues
show
Bug introduced by
'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 ignore-type  annotation

70
        $queryBuilder->add('select', /** @scrutinizer ignore-type */ 'j');
Loading history...
71
        $queryBuilder->setFirstResult($this->offset)
72 3
                        ->setMaxResults($this->limit);
73
74 3
        return $queryBuilder;
75 3
    }
76 3
77 3
    public function setColumns($columns)
78
    {
79
        if ($columns) {
80
            foreach ($columns as $column) {
81
                if ($column instanceof GridColumn) {
82 3
                    $column->setOption('sortable', false);
83 3
                }
84
            }
85 3
        }
86
87 3
        parent::setColumns($columns);
88 2
    }
89
90 3
    public function getColumns()
91
    {
92 3
        if ($columns = parent::getColumns()) {
93
            return $columns;
94
        }
95 3
96
        if (!$this->columnSource) {
97 3
            $this->autoDiscoverColumns();
98
            return parent::getColumns();
99
        }
100 1
101
        $columnSourceInfo = $this->columnSource->getColumnSourceInfo($this->objectManager, 'Dtc\QueueBundle\Entity\Job', false);
102 1
        $this->setColumns($columnSourceInfo->columns);
103 1
        $this->setDefaultSort($columnSourceInfo->sort);
104 1
        $this->setIdColumn($columnSourceInfo->idColumn);
105 1
        return parent::getColumns();
106 1
    }
107
108 1
    public function getDefaultSort()
109 1
    {
110
        return null;
111
    }
112
113
    public function getCount()
114
    {
115
        $qb = $this->getQueryBuilder();
116
        $qb->resetDQLPart('orderBy');
117
        $qb->add('select', 'count(j)')
0 ignored issues
show
Bug introduced by
'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 ignore-type  annotation

117
        $qb->add('select', /** @scrutinizer ignore-type */ 'count(j)')
Loading history...
118
            ->setFirstResult(null)
119
            ->setMaxResults(null);
120
121
        return $qb->getQuery()
122
            ->getSingleScalarResult();
123
    }
124
}
125