Passed
Pull Request — master (#146)
by David
03:00
created

ManyToManyRelationshipPathDescriptor   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 10
eloc 27
c 2
b 1
f 0
dl 0
loc 89
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A generateModelKey() 0 3 1
A getPivotWhere() 0 7 2
A __construct() 0 7 1
A getPivotParams() 0 7 2
A getTargetName() 0 3 1
A getPivotName() 0 3 1
A getPivotFrom() 0 11 2
1
<?php
2
3
namespace TheCodingMachine\TDBM\Utils;
4
5
use Doctrine\DBAL\Schema\ForeignKeyConstraint;
6
use function var_export;
7
8
class ManyToManyRelationshipPathDescriptor
9
{
10
11
    /**
12
     * @var string
13
     */
14
    private $targetTable;
15
    /**
16
     * @var string
17
     */
18
    private $pivotTable;
19
    /**
20
     * @var array
21
     */
22
    private $joinForeignKeys;
23
    /**
24
     * @var array
25
     */
26
    private $joinLocalKeys;
27
    /**
28
     * @var array
29
     */
30
    private $whereKeys;
31
32
    /**
33
     * ManyToManyRelationshipPathDescriptor constructor.
34
     * @param string $targetTable
35
     * @param string $pivotTable
36
     * @param string[] $joinForeignKeys
37
     * @param string[] $joinLocalKeys
38
     * @param string[] $whereKeys
39
     */
40
    public function __construct(string $targetTable, string $pivotTable, array $joinForeignKeys, array $joinLocalKeys, array $whereKeys)
41
    {
42
        $this->targetTable = $targetTable;
43
        $this->pivotTable = $pivotTable;
44
        $this->joinForeignKeys = $joinForeignKeys;
45
        $this->joinLocalKeys = $joinLocalKeys;
46
        $this->whereKeys = $whereKeys;
47
    }
48
49
    public static function generateModelKey(ForeignKeyConstraint $remoteFk, ForeignKeyConstraint $localFk): string
50
    {
51
        return $remoteFk->getLocalTableName().".".implode("__", $localFk->getUnquotedLocalColumns());
52
    }
53
54
    public function getPivotName(): string
55
    {
56
        return $this->pivotTable;
57
    }
58
59
    public function getTargetName(): string
60
    {
61
        return $this->targetTable;
62
    }
63
64
    public function getPivotFrom(): string
65
    {
66
        $mainTable = $this->targetTable;
67
        $pivotTable = $this->pivotTable;
68
69
        $join = [];
70
        foreach ($this->joinForeignKeys as $key => $column) {
71
            $join[] = $mainTable.'.'.$column.' = pivot.'.$this->joinLocalKeys[$key];
72
        }
73
74
        return $mainTable.' JOIN '.$pivotTable.' pivot ON '.implode(' AND ', $join);
75
    }
76
77
    public function getPivotWhere(): string
78
    {
79
        $paramList = [];
80
        foreach ($this->whereKeys as $key => $column) {
81
            $paramList[] = ' pivot.'.$column." = :param$key";
82
        }
83
        return implode(" AND ", $paramList);
84
    }
85
86
    /**
87
     * @param string[] $primaryKeys
88
     * @return string[]
89
     */
90
    public function getPivotParams(array $primaryKeys): array
91
    {
92
        $params = [];
93
        foreach ($primaryKeys as $key => $primaryKeyValue) {
94
            $params["param$key"] = $primaryKeyValue;
95
        }
96
        return $params;
97
    }
98
}
99