Passed
Pull Request — master (#181)
by
unknown
05:45
created

getResultIteratorClass()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
namespace TheCodingMachine\TDBM\Utils;
4
5
use Doctrine\DBAL\Schema\ForeignKeyConstraint;
6
use TheCodingMachine\TDBM\ResultIterator;
7
use TheCodingMachine\TDBM\TDBMInvalidArgumentException;
8
use function var_export;
9
10
class ManyToManyRelationshipPathDescriptor
11
{
12
13
    /**
14
     * @var string
15
     */
16
    private $targetTable;
17
    /**
18
     * @var string
19
     */
20
    private $pivotTable;
21
    /**
22
     * @var array
23
     */
24
    private $joinForeignKeys;
25
    /**
26
     * @var array
27
     */
28
    private $joinLocalKeys;
29
    /**
30
     * @var array
31
     */
32
    private $whereKeys;
33
    /**
34
     * @var string
35
     */
36
    private $resultIteratorClass;
37
38
    /**
39
     * ManyToManyRelationshipPathDescriptor constructor.
40
     * @param string $targetTable
41
     * @param string $pivotTable
42
     * @param string[] $joinForeignKeys
43
     * @param string[] $joinLocalKeys
44
     * @param string[] $whereKeys
45
     */
46
    public function __construct(string $targetTable, string $pivotTable, array $joinForeignKeys, array $joinLocalKeys, array $whereKeys, string $resultIteratorClass)
47
    {
48
        assert(is_a($resultIteratorClass, ResultIterator::class, true), new TDBMInvalidArgumentException('$resultIteratorClass should be a `'. ResultIterator::class. '`. `' . $resultIteratorClass . '` provided.'));
49
        $this->targetTable = $targetTable;
50
        $this->pivotTable = $pivotTable;
51
        $this->joinForeignKeys = $joinForeignKeys;
52
        $this->joinLocalKeys = $joinLocalKeys;
53
        $this->whereKeys = $whereKeys;
54
        $this->resultIteratorClass = $resultIteratorClass;
55
    }
56
57
    public static function generateModelKey(ForeignKeyConstraint $remoteFk, ForeignKeyConstraint $localFk): string
58
    {
59
        return $remoteFk->getLocalTableName().".".implode("__", $localFk->getUnquotedLocalColumns());
60
    }
61
62
    public function getPivotName(): string
63
    {
64
        return $this->pivotTable;
65
    }
66
67
    public function getTargetName(): string
68
    {
69
        return $this->targetTable;
70
    }
71
72
    public function getPivotFrom(): string
73
    {
74
        $mainTable = $this->targetTable;
75
        $pivotTable = $this->pivotTable;
76
77
        $join = [];
78
        foreach ($this->joinForeignKeys as $key => $column) {
79
            $join[] = $mainTable.'.'.$column.' = pivot.'.$this->joinLocalKeys[$key];
80
        }
81
82
        return $mainTable.' JOIN '.$pivotTable.' pivot ON '.implode(' AND ', $join);
83
    }
84
85
    public function getPivotWhere(): string
86
    {
87
        $paramList = [];
88
        foreach ($this->whereKeys as $key => $column) {
89
            $paramList[] = ' pivot.'.$column." = :param$key";
90
        }
91
        return implode(" AND ", $paramList);
92
    }
93
94
    /**
95
     * @param string[] $primaryKeys
96
     * @return string[]
97
     */
98
    public function getPivotParams(array $primaryKeys): array
99
    {
100
        $params = [];
101
        foreach ($primaryKeys as $key => $primaryKeyValue) {
102
            $params["param$key"] = $primaryKeyValue;
103
        }
104
        return $params;
105
    }
106
107
    public function getResultIteratorClass(): string
108
    {
109
        return $this->resultIteratorClass;
110
    }
111
}
112