Passed
Pull Request — master (#172)
by David
06:43
created

ManyToOnePartialQuery   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
eloc 23
c 1
b 0
f 0
dl 0
loc 81
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getStorageNode() 0 3 1
A registerDataLoader() 0 10 2
A getKey() 0 3 1
A getQueryFrom() 0 3 1
A getMainTable() 0 3 1
A __construct() 0 11 1
1
<?php
2
3
4
namespace TheCodingMachine\TDBM\QueryFactory\SmartEagerLoad\Query;
5
6
7
use Doctrine\DBAL\Connection;
8
use Doctrine\DBAL\Platforms\MySqlPlatform;
9
use TheCodingMachine\TDBM\QueryFactory\SmartEagerLoad\ManyToOneDataLoader;
10
use TheCodingMachine\TDBM\QueryFactory\SmartEagerLoad\StorageNode;
11
12
class ManyToOnePartialQuery implements PartialQuery
13
{
14
    /**
15
     * @var string
16
     */
17
    private $queryFrom;
18
    /**
19
     * @var string
20
     */
21
    private $mainTable;
22
    /**
23
     * @var StorageNode
24
     */
25
    private $storageNode;
26
    /**
27
     * @var string
28
     */
29
    private $key;
30
    /**
31
     * @var string
32
     */
33
    private $pk;
34
35
    public function __construct(PartialQuery $partialQuery, string $originTableName, string $tableName, string $pk, string $columnName)
36
    {
37
        // TODO: move this in a separate function. The constructor is called for every bean.
38
        $mysqlPlatform = new MySqlPlatform();
39
        $this->queryFrom = 'FROM ' .$mysqlPlatform->quoteIdentifier($tableName).
40
            ' WHERE ' .$mysqlPlatform->quoteIdentifier($tableName).'.'.$mysqlPlatform->quoteIdentifier($pk).' IN '.
41
            '(SELECT '.$mysqlPlatform->quoteIdentifier($originTableName).'.'.$mysqlPlatform->quoteIdentifier($columnName).' '.$partialQuery->getQueryFrom().')';
42
        $this->mainTable = $tableName;
43
        $this->storageNode = $partialQuery->getStorageNode();
44
        $this->key = $partialQuery->getKey().'__'.$columnName;
45
        $this->pk = $pk;
46
    }
47
48
    /**
49
     * Returns the SQL of the query, starting at the FROM keyword.
50
     */
51
    public function getQueryFrom(): string
52
    {
53
        return $this->queryFrom;
54
    }
55
56
    /**
57
     * Returns the name of the main table (main objects returned by this query)
58
     */
59
    public function getMainTable(): string
60
    {
61
        return $this->mainTable;
62
    }
63
64
    /**
65
     * Returns the object in charge of storing the dataloader associated to this query.
66
     */
67
    public function getStorageNode(): StorageNode
68
    {
69
        return $this->storageNode;
70
    }
71
72
    /**
73
     * Returns a key representing the "path" to this query. This is meant to be used as a cache key.
74
     */
75
    public function getKey(): string
76
    {
77
        return $this->key;
78
    }
79
80
    /**
81
     * Registers a dataloader for this query, if needed.
82
     */
83
    public function registerDataLoader(Connection $connection): void
84
    {
85
        if ($this->storageNode->hasManyToOneDataLoader($this->key)) {
86
            return;
87
        }
88
89
        $mysqlPlatform = new MySqlPlatform();
90
        $sql = 'SELECT DISTINCT ' .$mysqlPlatform->quoteIdentifier($this->mainTable).'.* '.$this->queryFrom;
91
92
        $this->storageNode->setManyToOneDataLoader($this->key, new ManyToOneDataLoader($connection, $sql, $this->pk));
93
    }
94
}
95