Total Complexity | 46 |
Total Lines | 213 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 1 |
Complex classes like ALdap often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ALdap, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
19 | abstract class ALdap extends AMapper |
||
20 | { |
||
21 | use TFill; |
||
22 | use TTable; |
||
23 | |||
24 | /** @var Storage\Database\Raw\Ldap */ |
||
25 | protected $database = null; |
||
26 | /** @var Storage\Shared\QueryBuilder */ |
||
27 | protected $queryBuilder = null; |
||
28 | /** @var Storage\Database\Dialects\LdapQueries */ |
||
29 | protected $dialect = null; |
||
30 | |||
31 | /** |
||
32 | * @throws MapperException |
||
33 | */ |
||
34 | public function __construct() |
||
35 | { |
||
36 | parent::__construct(); |
||
37 | $config = Storage\Database\ConfigStorage::getInstance()->getConfig($this->getSource()); |
||
38 | $this->database = Storage\Database\DatabaseSingleton::getInstance()->getDatabase($config); |
||
39 | $this->dialect = new Storage\Database\Dialects\LdapQueries(); |
||
40 | $this->queryBuilder = new Storage\Shared\QueryBuilder(); |
||
41 | } |
||
42 | |||
43 | public function getAlias(): string |
||
44 | { |
||
45 | return $this->getTable(); |
||
46 | } |
||
47 | |||
48 | protected function insertRecord(ARecord $record): bool |
||
49 | { |
||
50 | $this->queryBuilder->clear(); |
||
51 | $this->queryBuilder->setBaseTable($this->getTable()); |
||
52 | foreach ($record as $key => $item) { |
||
53 | $this->queryBuilder->addProperty($this->getTable(), $this->relations[$key], $item); |
||
54 | } |
||
55 | $this->database->connect(); |
||
56 | $connect = $this->database->getConnection(); |
||
57 | if (!(is_resource($connect) || is_object($connect))) { |
||
58 | return false; |
||
59 | } |
||
60 | return ldap_add( |
||
61 | $connect, |
||
62 | $this->dialect->domainDn($this->database->getDomain()), |
||
63 | $this->dialect->changed($this->queryBuilder) |
||
64 | ); |
||
65 | } |
||
66 | |||
67 | protected function updateRecord(ARecord $record): bool |
||
68 | { |
||
69 | $this->queryBuilder->clear(); |
||
70 | $this->queryBuilder->setBaseTable($this->getTable()); |
||
71 | foreach ($record as $key => $item) { |
||
72 | if (!$record->getEntry($key)->isFromStorage()) { |
||
73 | $this->queryBuilder->addProperty($this->getTable(), $this->relations[$key], $item); |
||
74 | } |
||
75 | } |
||
76 | $this->database->connect(); |
||
77 | $connect = $this->database->getConnection(); |
||
78 | if (!(is_resource($connect) || is_object($connect))) { |
||
79 | return false; |
||
80 | } |
||
81 | return ldap_mod_replace( |
||
82 | $connect, |
||
83 | $this->dialect->userDn($this->database->getDomain(), $this->getPk($record)), |
||
|
|||
84 | $this->dialect->changed($this->queryBuilder) |
||
85 | ); |
||
86 | } |
||
87 | |||
88 | protected function deleteRecord(ARecord $record): bool |
||
89 | { |
||
90 | $this->database->connect(); |
||
91 | $connect = $this->database->getConnection(); |
||
92 | if (!(is_resource($connect) || is_object($connect))) { |
||
93 | return false; |
||
94 | } |
||
95 | return ldap_delete( |
||
96 | $connect, |
||
97 | $this->dialect->userDn($this->database->getDomain(), $this->getPk($record)) |
||
98 | ); |
||
99 | } |
||
100 | |||
101 | /** |
||
102 | * @param ARecord $record |
||
103 | * @throws MapperException |
||
104 | * @return mixed |
||
105 | */ |
||
106 | protected function getPk(ARecord $record) |
||
107 | { |
||
108 | $pks = $this->getPrimaryKeys(); |
||
109 | $pk = reset($pks); |
||
110 | return $record->offsetGet($pk); |
||
111 | } |
||
112 | |||
113 | protected function loadRecord(ARecord $record): bool |
||
128 | } |
||
129 | |||
130 | public function countRecord(ARecord $record): int |
||
131 | { |
||
132 | $this->fillConditions($record); |
||
133 | $entries = $this->multiple(); |
||
134 | if (empty($entries) || empty($entries['count'])) { |
||
135 | return 0; |
||
136 | } |
||
137 | return intval($entries['count']); |
||
138 | } |
||
139 | |||
140 | public function loadMultiple(ARecord $record): array |
||
141 | { |
||
142 | $this->fillConditions($record); |
||
143 | $lines = $this->multiple(); |
||
144 | if (empty($lines)) { |
||
145 | return []; |
||
146 | } |
||
147 | |||
148 | $result = []; |
||
149 | $relationMap = array_flip($this->relations); |
||
150 | foreach ($lines as $key => $line) { |
||
151 | if (is_numeric($key) && is_iterable($line)) { |
||
152 | $rec = clone $record; |
||
153 | foreach ($line as $index => $item) { |
||
154 | $entry = $rec->getEntry($relationMap[$index]); |
||
155 | $entry->setData($this->typedFillSelection($entry, $this->readItem($item)), true); |
||
156 | } |
||
157 | $result[] = $rec; |
||
158 | } |
||
159 | } |
||
160 | return $result; |
||
161 | } |
||
162 | |||
163 | /** |
||
164 | * @param mixed $item |
||
165 | * @return string |
||
166 | */ |
||
167 | protected function readItem($item) |
||
170 | } |
||
171 | |||
172 | /** |
||
173 | * @param ARecord $record |
||
174 | * @throws MapperException |
||
175 | */ |
||
176 | protected function fillConditions(ARecord $record): void |
||
183 | } |
||
184 | } |
||
185 | } |
||
186 | |||
187 | /** |
||
188 | * @throws MapperException |
||
189 | * @return array<string|int, string|int|float|array<string|int|float>> |
||
190 | */ |
||
191 | protected function multiple(): array |
||
208 | } |
||
209 | |||
210 | /** |
||
211 | * @param string[] $params |
||
212 | * @throws MapperException |
||
213 | * @return bool |
||
214 | */ |
||
215 | public function authorize(array $params): bool |
||
232 | } |
||
233 | } |
||
234 |