FindObjectsQueryFactory::compute()   B
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 26
Code Lines 16

Duplication

Lines 7
Ratio 26.92 %

Importance

Changes 0
Metric Value
dl 7
loc 26
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 16
nc 4
nop 0
1
<?php
2
3
namespace Mouf\Database\TDBM\QueryFactory;
4
5
use Doctrine\DBAL\Schema\Schema;
6
use Mouf\Database\TDBM\OrderByAnalyzer;
7
use Mouf\Database\TDBM\TDBMService;
8
9
/**
10
 * This class is in charge of creating the MagicQuery SQL based on parameters passed to findObjects method.
11
 */
12
class FindObjectsQueryFactory extends AbstractQueryFactory
13
{
14
    private $mainTable;
15
    private $additionalTablesFetch;
16
    private $filterString;
17
18
    public function __construct(string $mainTable, array $additionalTablesFetch, $filterString, $orderBy, TDBMService $tdbmService, Schema $schema, OrderByAnalyzer $orderByAnalyzer)
19
    {
20
        parent::__construct($tdbmService, $schema, $orderByAnalyzer, $orderBy);
21
        $this->mainTable = $mainTable;
22
        $this->additionalTablesFetch = $additionalTablesFetch;
23
        $this->filterString = $filterString;
24
    }
25
26
    protected function compute()
27
    {
28
        list($columnDescList, $columnsList, $orderString) = $this->getColumnsList($this->mainTable, $this->additionalTablesFetch, $this->orderBy);
29
30
        $sql = 'SELECT DISTINCT '.implode(', ', $columnsList).' FROM MAGICJOIN('.$this->mainTable.')';
31
32
        $pkColumnNames = $this->schema->getTable($this->mainTable)->getPrimaryKeyColumns();
33 View Code Duplication
        $pkColumnNames = array_map(function ($pkColumn) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
34
            return $this->tdbmService->getConnection()->quoteIdentifier($this->mainTable).'.'.$this->tdbmService->getConnection()->quoteIdentifier($pkColumn);
35
        }, $pkColumnNames);
36
37
        $countSql = 'SELECT COUNT(DISTINCT '.implode(', ', $pkColumnNames).') FROM MAGICJOIN('.$this->mainTable.')';
38
39 View Code Duplication
        if (!empty($this->filterString)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
40
            $sql .= ' WHERE '.$this->filterString;
41
            $countSql .= ' WHERE '.$this->filterString;
42
        }
43
44
        if (!empty($orderString)) {
45
            $sql .= ' ORDER BY '.$orderString;
46
        }
47
48
        $this->magicSql = $sql;
49
        $this->magicSqlCount = $countSql;
50
        $this->columnDescList = $columnDescList;
51
    }
52
}
53