Total Complexity | 43 |
Total Lines | 239 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like NOSQLService 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 NOSQLService, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
26 | class NOSQLService extends Service { |
||
27 | |||
28 | /** |
||
29 | * @Injectable |
||
30 | * @var \PSFS\base\Cache |
||
31 | */ |
||
32 | protected $cache; |
||
33 | |||
34 | /** |
||
35 | * @var array |
||
36 | */ |
||
37 | private $types = []; |
||
38 | |||
39 | /** |
||
40 | * @return array |
||
41 | */ |
||
42 | public function getTypes() |
||
43 | { |
||
44 | return $this->types; |
||
45 | } |
||
46 | |||
47 | /** |
||
48 | * @param array $types |
||
49 | */ |
||
50 | public function setTypes($types) |
||
51 | { |
||
52 | $this->types = $types; |
||
53 | } |
||
54 | |||
55 | /** |
||
56 | * @throws \ReflectionException |
||
57 | */ |
||
58 | private function extractTypes() { |
||
59 | $baseClass = new \ReflectionClass(NOSQLBase::class); |
||
60 | if(null !== $baseClass) { |
||
61 | $types = []; |
||
62 | foreach($baseClass->getConstants() as $constant) { |
||
63 | $types[] = $constant; |
||
64 | } |
||
65 | $this->setTypes($types); |
||
66 | } |
||
67 | } |
||
68 | |||
69 | /** |
||
70 | * @throws \ReflectionException |
||
71 | */ |
||
72 | public function init() |
||
73 | { |
||
74 | parent::init(); |
||
75 | $this->extractTypes(); |
||
76 | } |
||
77 | |||
78 | /** |
||
79 | * @return array |
||
80 | */ |
||
81 | public function getDomains() { |
||
82 | $domains = []; |
||
83 | $storedDomains = $this->cache->getDataFromFile(CONFIG_DIR . DIRECTORY_SEPARATOR . 'domains.json', Cache::JSON, TRUE); |
||
84 | if(!empty($storedDomains)) { |
||
85 | foreach($storedDomains as $domain => $data) { |
||
86 | $domainLabel = str_replace(['@', '/'], '', $domain); |
||
87 | if('ROOT' !== $domainLabel) { |
||
88 | $domains[] = $domainLabel; |
||
89 | } |
||
90 | } |
||
91 | } |
||
92 | return $domains; |
||
93 | } |
||
94 | |||
95 | /** |
||
96 | * @param string $module |
||
97 | * @return array |
||
98 | */ |
||
99 | public function getCollections($module) { |
||
106 | } |
||
107 | |||
108 | /** |
||
109 | * @param string $module |
||
110 | * @param array $collections |
||
111 | * @throws \PSFS\base\exception\GeneratorException |
||
112 | */ |
||
113 | public function setCollections($module, $collections) { |
||
140 | } |
||
141 | } |
||
142 | } |
||
143 | |||
144 | /** |
||
145 | * @param string $fileContent |
||
146 | * @param string $filename |
||
147 | * @param bool $force |
||
148 | * @return bool |
||
149 | */ |
||
150 | private function writeTemplateToFile($fileContent, $filename, $force = false) |
||
164 | } |
||
165 | |||
166 | /** |
||
167 | * @param Database $db |
||
168 | * @param $collection |
||
169 | */ |
||
170 | private function createIndexes(Database $db, $collectionDto) { |
||
171 | try { |
||
172 | $collection = $db->selectCollection($collectionDto['name']); |
||
173 | $textIndexes = []; |
||
174 | foreach($collectionDto['properties'] as $property) { |
||
175 | if(in_array($property['type'], [NOSQLBase::NOSQL_TYPE_STRING, NOSQLBase::NOSQL_TYPE_OBJECT])) { |
||
176 | $textIndexes[$property['name']] = 'text'; |
||
177 | } |
||
178 | } |
||
179 | if(count($textIndexes)) { |
||
180 | $collection->createIndex($textIndexes, ['name' => 'idx_text_' . $collectionDto['name']]); |
||
181 | } |
||
182 | } catch (\Exception $exception) { |
||
183 | Logger::log($exception->getMessage(), LOG_DEBUG); |
||
184 | } |
||
185 | } |
||
186 | |||
187 | /** |
||
188 | * @param $module |
||
189 | * @return bool |
||
190 | * @throws \PSFS\base\exception\GeneratorException |
||
191 | */ |
||
192 | public function syncCollections($module) { |
||
193 | $db = ParserService::getInstance()->createConnection($module); |
||
194 | $collections = $this->getCollections($module); |
||
195 | $success = true; |
||
196 | foreach($collections as $raw) { |
||
197 | $jsonSchema = $this->parseCollection($raw); |
||
198 | try { |
||
199 | /** @var BSONDocument $result */ |
||
200 | $result = $db->createCollection($raw['name'], [ |
||
201 | 'validation' => [ |
||
202 | '$jsonSchema' => $jsonSchema->toArray(), |
||
203 | ] |
||
204 | ]); |
||
205 | $response = $result->getArrayCopy(); |
||
206 | $success = array_key_exists('ok', $response) && $response['ok'] > 0; |
||
207 | } catch(\Exception $exception) { |
||
208 | if($exception->getCode() !== 48) { |
||
209 | $success = false; |
||
210 | } |
||
211 | } |
||
212 | $this->createIndexes($db, $raw); |
||
213 | } |
||
214 | return $success; |
||
215 | } |
||
216 | |||
217 | /** |
||
218 | * @param array $raw |
||
219 | * @return JsonSchemaDto |
||
220 | * @throws \PSFS\base\exception\GeneratorException |
||
221 | */ |
||
222 | private function parseCollection($raw) |
||
252 | } |
||
253 | |||
254 | /** |
||
255 | * @return array |
||
256 | * @throws \ReflectionException |
||
257 | */ |
||
258 | public function getValidations() { |
||
265 | } |
||
266 | } |
||
267 |