Completed
Pull Request — 3.4 (#46)
by David
06:17
created

ObjectBeanPropertyDescriptor   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 140
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 15
c 2
b 0
f 0
lcom 1
cbo 5
dl 0
loc 140
rs 10

8 Methods

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