Completed
Pull Request — 4.0 (#59)
by David
05:43
created

ObjectBeanPropertyDescriptor   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 165
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 16
c 4
b 0
f 0
lcom 1
cbo 5
dl 0
loc 165
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getForeignKey() 0 4 1
A getClassName() 0 4 1
A getParamAnnotation() 0 6 1
B getUpperCamelCaseName() 0 28 6
A isCompulsory() 0 14 3
A isPrimaryKey() 0 10 1
B getGetterSetterCode() 0 30 1
A getJsonSerializeCode() 0 7 1
1
<?php
2
3
namespace Mouf\Database\TDBM\Utils;
4
5
use Doctrine\DBAL\Schema\Column;
6
use Doctrine\DBAL\Schema\Table;
7
use Doctrine\DBAL\Schema\ForeignKeyConstraint;
8
use Mouf\Database\SchemaAnalyzer\SchemaAnalyzer;
9
10
/**
11
 * This class represent a property in a bean that points to another table.
12
 */
13
class ObjectBeanPropertyDescriptor extends AbstractBeanPropertyDescriptor
14
{
15
    /**
16
     * @var ForeignKeyConstraint
17
     */
18
    private $foreignKey;
19
20
    /**
21
     * @var SchemaAnalyzer
22
     */
23
    private $schemaAnalyzer;
24
25
    public function __construct(Table $table, ForeignKeyConstraint $foreignKey, SchemaAnalyzer $schemaAnalyzer)
26
    {
27
        parent::__construct($table);
28
        $this->foreignKey = $foreignKey;
29
        $this->schemaAnalyzer = $schemaAnalyzer;
30
    }
31
32
    /**
33
     * Returns the foreignkey the column is part of, if any. null otherwise.
34
     *
35
     * @return ForeignKeyConstraint|null
36
     */
37
    public function getForeignKey()
38
    {
39
        return $this->foreignKey;
40
    }
41
42
    /**
43
     * Returns the name of the class linked to this property or null if this is not a foreign key.
44
     *
45
     * @return null|string
46
     */
47
    public function getClassName()
48
    {
49
        return TDBMDaoGenerator::getBeanNameFromTableName($this->foreignKey->getForeignTableName());
50
    }
51
52
    /**
53
     * Returns the param annotation for this property (useful for constructor).
54
     *
55
     * @return string
56
     */
57
    public function getParamAnnotation()
58
    {
59
        $str = '     * @param %s %s';
60
61
        return sprintf($str, $this->getClassName(), $this->getVariableName());
62
    }
63
64
    public function getUpperCamelCaseName()
65
    {
66
        // First, are there many column or only one?
67
        // If one column, we name the setter after it. Otherwise, we name the setter after the table name
68
        if (count($this->foreignKey->getLocalColumns()) > 1) {
69
            $name = TDBMDaoGenerator::toSingular(TDBMDaoGenerator::toCamelCase($this->foreignKey->getForeignTableName()));
70
            if ($this->alternativeName) {
71
                $camelizedColumns = array_map(['Mouf\\Database\\TDBM\\Utils\\TDBMDaoGenerator', 'toCamelCase'], $this->foreignKey->getLocalColumns());
72
73
                $name .= 'By'.implode('And', $camelizedColumns);
74
            }
75
        } else {
76
            $column = $this->foreignKey->getLocalColumns()[0];
77
            // Let's remove any _id or id_.
78
            if (strpos(strtolower($column), 'id_') === 0) {
79
                $column = substr($column, 3);
80
            }
81
            if (strrpos(strtolower($column), '_id') === strlen($column) - 3) {
82
                $column = substr($column, 0, strlen($column) - 3);
83
            }
84
            $name = TDBMDaoGenerator::toCamelCase($column);
85
            if ($this->alternativeName) {
86
                $name .= 'Object';
87
            }
88
        }
89
90
        return $name;
91
    }
92
93
    /**
94
     * Returns true if the property is compulsory (and therefore should be fetched in the constructor).
95
     *
96
     * @return bool
97
     */
98
    public function isCompulsory()
99
    {
100
        // Are all columns nullable?
101
        $localColumnNames = $this->foreignKey->getLocalColumns();
102
103
        foreach ($localColumnNames as $name) {
104
            $column = $this->table->getColumn($name);
105
            if ($column->getNotnull()) {
106
                return true;
107
            }
108
        }
109
110
        return false;
111
    }
112
113
    /**
114
     * Returns true if the property is the primary key.
115
     *
116
     * @return bool
117
     */
118
    public function isPrimaryKey()
119
    {
120
        $fkColumns = $this->foreignKey->getLocalColumns();
121
        sort($fkColumns);
122
123
        $pkColumns = $this->table->getPrimaryKeyColumns();
124
        sort($pkColumns);
125
126
        return $fkColumns == $pkColumns;
127
    }
128
129
    /**
130
     * Returns the PHP code for getters and setters.
131
     *
132
     * @return string
133
     */
134
    public function getGetterSetterCode()
135
    {
136
        $tableName = $this->table->getName();
137
        $getterName = $this->getGetterName();
138
        $setterName = $this->getSetterName();
139
140
        $referencedBeanName = TDBMDaoGenerator::getBeanNameFromTableName($this->foreignKey->getForeignTableName());
141
142
        $str = '    /**
143
     * Returns the '.$referencedBeanName.' object bound to this object via the '.implode(' and ', $this->foreignKey->getLocalColumns()).' column.
144
     *
145
     * @return '.$referencedBeanName.'
146
     */
147
    public function '.$getterName.'() {
148
        return $this->getRef('.var_export($this->foreignKey->getName(), true).', '.var_export($tableName, true).');
149
    }
150
151
    /**
152
     * The setter for the '.$referencedBeanName.' object bound to this object via the '.implode(' and ', $this->foreignKey->getLocalColumns()).' column.
153
     *
154
     * @param '.$referencedBeanName.' $object
155
     */
156
    public function '.$setterName.'('.$referencedBeanName.' $object = null) {
157
        $this->setRef('.var_export($this->foreignKey->getName(), true).', $object, '.var_export($tableName, true).');
158
    }
159
160
';
161
162
        return $str;
163
    }
164
165
    /**
166
     * Returns the part of code useful when doing json serialization.
167
     *
168
     * @return string
169
     */
170
    public function getJsonSerializeCode()
171
    {
172
        return '        if (!$stopRecursion) {
173
            $array['.var_export($this->getLowerCamelCaseName(), true).'] = $this->'.$this->getGetterName().'()->jsonSerialize(true);
174
        }
175
';
176
    }
177
}
178