Passed
Push — master ( 10a888...e30075 )
by Matthew
06:51
created

LiveJobsGridSource::setColumnSource()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
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
19 2
    public function getId()
20
    {
21 2
        return 'dtc_queue.grid_source.jobs_'.($this->isRunning() ? 'running' : 'waiting').'.orm';
22
    }
23
24 10
    public function __construct(JobManager $jobManager)
25
    {
26 10
        $this->jobManager = $jobManager;
27
        /** @var EntityManager $entityManager */
28 10
        $entityManager = $jobManager->getObjectManager();
29 10
        parent::__construct($entityManager, $jobManager->getJobClass());
30 10
    }
31
32
    /**
33
     * @param bool $flag
34
     */
35 10
    public function setRunning($flag)
36
    {
37 10
        $this->running = $flag;
38 10
    }
39
40 3
    public function isRunning()
41
    {
42 3
        return $this->running;
43
    }
44
45
    public function setColumnSource(\Dtc\GridBundle\Grid\Source\ColumnSource $columnSource)
46
    {
47
        $this->columnSource = $columnSource;
48
    }
49
50 1
    protected function getRunningQueryBuilder()
51
    {
52
        /** @var EntityManager $entityManager */
53 1
        $entityManager = $this->jobManager->getObjectManager();
54 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

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

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

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