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