Completed
Pull Request — master (#9)
by David
07:01
created

DirectForeignKeyMethodDescriptor::getCode()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 30
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 30
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 14
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace TheCodingMachine\TDBM\Utils;
6
7
use Doctrine\DBAL\Schema\ForeignKeyConstraint;
8
use Doctrine\DBAL\Schema\Table;
9
10
/**
11
 * Represents a method to get a list of beans from a direct foreign key pointing to our bean.
12
 */
13
class DirectForeignKeyMethodDescriptor implements MethodDescriptorInterface
14
{
15
    /**
16
     * @var ForeignKeyConstraint
17
     */
18
    private $fk;
19
20
    private $useAlternateName = false;
21
    /**
22
     * @var Table
23
     */
24
    private $mainTable;
25
    /**
26
     * @var NamingStrategyInterface
27
     */
28
    private $namingStrategy;
29
30
    /**
31
     * @param ForeignKeyConstraint $fk The foreign key pointing to our bean
32
     * @param Table $mainTable The main table that is pointed to
33
     * @param NamingStrategyInterface $namingStrategy
34
     */
35
    public function __construct(ForeignKeyConstraint $fk, Table $mainTable, NamingStrategyInterface $namingStrategy)
36
    {
37
        $this->fk = $fk;
38
        $this->mainTable = $mainTable;
39
        $this->namingStrategy = $namingStrategy;
40
    }
41
42
    /**
43
     * Returns the name of the method to be generated.
44
     *
45
     * @return string
46
     */
47
    public function getName() : string
48
    {
49
        if (!$this->useAlternateName) {
50
            return 'get'.TDBMDaoGenerator::toCamelCase($this->fk->getLocalTableName());
51
        } else {
52
            $methodName = 'get'.TDBMDaoGenerator::toCamelCase($this->fk->getLocalTableName()).'By';
53
54
            $camelizedColumns = array_map([TDBMDaoGenerator::class, 'toCamelCase'], $this->fk->getLocalColumns());
55
56
            $methodName .= implode('And', $camelizedColumns);
57
58
            return $methodName;
59
        }
60
    }
61
62
    /**
63
     * Returns the name of the class that will be returned by the getter (short name).
64
     *
65
     * @return string
66
     */
67
    public function getBeanClassName(): string
68
    {
69
        return $this->namingStrategy->getBeanClassName($this->fk->getLocalTableName());
70
    }
71
72
    /**
73
     * Requests the use of an alternative name for this method.
74
     */
75
    public function useAlternativeName()
76
    {
77
        $this->useAlternateName = true;
78
    }
79
80
    /**
81
     * Returns the code of the method.
82
     *
83
     * @return string
84
     */
85
    public function getCode() : string
86
    {
87
        $code = '';
88
89
        $getterCode = '    /**
90
     * Returns the list of %s pointing to this bean via the %s column.
91
     *
92
     * @return %s[]|AlterableResultIterator
93
     */
94
    public function %s() : AlterableResultIterator
95
    {
96
        return $this->retrieveManyToOneRelationshipsStorage(%s, %s, %s, %s);
97
    }
98
99
';
100
101
        $beanClass = $this->getBeanClassName();
102
        $code .= sprintf($getterCode,
103
            $beanClass,
104
            implode(', ', $this->fk->getColumns()),
105
            $beanClass,
106
            $this->getName(),
107
            var_export($this->fk->getLocalTableName(), true),
108
            var_export($this->fk->getName(), true),
109
            var_export($this->fk->getLocalTableName(), true),
110
            $this->getFilters($this->fk)
111
        );
112
113
        return $code;
114
    }
115
116
    private function getFilters(ForeignKeyConstraint $fk) : string
117
    {
118
        $counter = 0;
119
        $parameters = [];
120
121
        $pkColumns = $this->mainTable->getPrimaryKeyColumns();
122
123
        foreach ($fk->getLocalColumns() as $columnName) {
124
            $pkColumn = $pkColumns[$counter];
125
            $parameters[] = sprintf('%s => $this->get(%s, %s)', var_export($fk->getLocalTableName().'.'.$columnName, true), var_export($pkColumn, true), var_export($this->fk->getForeignTableName(), true));
126
            ++$counter;
127
        }
128
        $parametersCode = '['.implode(', ', $parameters).']';
129
130
        return $parametersCode;
131
    }
132
133
    /**
134
     * Returns an array of classes that needs a "use" for this method.
135
     *
136
     * @return string[]
137
     */
138
    public function getUsedClasses() : array
139
    {
140
        return [$this->getBeanClassName()];
141
    }
142
143
    /**
144
     * Returns the code to past in jsonSerialize.
145
     *
146
     * @return string
147
     */
148
    public function getJsonSerializeCode() : string
149
    {
150
        return '';
151
    }
152
}
153