| Total Complexity | 43 |
| Total Lines | 277 |
| Duplicated Lines | 0 % |
| Changes | 4 | ||
| Bugs | 0 | Features | 0 |
Complex classes like NOSQLActiveRecord 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 NOSQLActiveRecord, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 19 | abstract class NOSQLActiveRecord { |
||
| 20 | use NOSQLModelTrait; |
||
| 21 | use NOSQLParserTrait; |
||
| 22 | use SingletonTrait; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * NOSQLActiveRecord constructor. |
||
| 26 | * @throws \NOSQL\Exceptions\NOSQLParserException |
||
| 27 | * @throws \PSFS\base\exception\GeneratorException |
||
| 28 | */ |
||
| 29 | public function __construct() |
||
| 30 | { |
||
| 31 | $this->hydrate(); |
||
| 32 | } |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @return array |
||
| 36 | */ |
||
| 37 | public function toArray() { |
||
| 38 | return $this->dto->toArray(); |
||
| 39 | } |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @param bool $cleanPk |
||
| 43 | * @return \NOSQL\Dto\Model\NOSQLModelDto |
||
| 44 | */ |
||
| 45 | public function getDtoCopy($cleanPk = false) { |
||
| 46 | $copy = clone $this->dto; |
||
| 47 | if($cleanPk) { |
||
| 48 | $this->dto->resetPk(); |
||
| 49 | } |
||
| 50 | return $copy; |
||
| 51 | } |
||
| 52 | |||
| 53 | private function prepareData() { |
||
| 54 | $this->dto->validate(true); |
||
| 55 | } |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @param Database|null $con |
||
| 59 | * @return bool |
||
| 60 | */ |
||
| 61 | public function save(Database $con = null) { |
||
| 62 | $saved = false; |
||
| 63 | if(null === $con) { |
||
| 64 | $con = ParserService::getInstance()->createConnection($this->getDomain()); |
||
| 65 | } |
||
| 66 | $collection = $con->selectCollection($this->getSchema()->name); |
||
| 67 | try { |
||
| 68 | $isInsert = $isUpdate = false; |
||
| 69 | $this->prepareData(); |
||
| 70 | $this->dto->setLastUpdate(); |
||
| 71 | if($this->isNew()) { |
||
| 72 | $this->preInsert($con); |
||
| 73 | $isInsert = true; |
||
| 74 | } elseif ($this->isModified()) { |
||
| 75 | $this->preUpdate($con); |
||
| 76 | $isUpdate = true; |
||
| 77 | } |
||
| 78 | $result = $collection->insertOne($this->toArray()); |
||
| 79 | if($result->getInsertedCount() > 0) { |
||
| 80 | $id = $result->getInsertedId(); |
||
| 81 | $this->dto->setPk($id->jsonSerialize()['$oid']); |
||
| 82 | if($isInsert) { |
||
| 83 | $this->postInsert($con); |
||
| 84 | } elseif($isUpdate) { |
||
| 85 | $this->postUpdate($con); |
||
| 86 | } |
||
| 87 | $saved = true; |
||
| 88 | $this->countAction(); |
||
| 89 | } |
||
| 90 | } catch(\Exception $exception) { |
||
| 91 | if($exception instanceof NOSQLValidationException) { |
||
| 92 | throw $exception; |
||
| 93 | } else { |
||
| 94 | Logger::log($exception->getMessage(), LOG_CRIT, $this->toArray()); |
||
| 95 | } |
||
| 96 | } |
||
| 97 | return $saved; |
||
| 98 | } |
||
| 99 | |||
| 100 | /** |
||
| 101 | * @param Database|null $con |
||
| 102 | * @return bool |
||
| 103 | */ |
||
| 104 | public function update(Database $con = null) { |
||
| 128 | } |
||
| 129 | |||
| 130 | /** |
||
| 131 | * @param array $data |
||
| 132 | * @param Database|null $con |
||
| 133 | * @return int |
||
| 134 | */ |
||
| 135 | public function bulkInsert(array $data, Database $con = null) { |
||
| 151 | } |
||
| 152 | |||
| 153 | /** |
||
| 154 | * Function to make a bulk upsert of documents |
||
| 155 | * @param array $data |
||
| 156 | * @param string $id |
||
| 157 | * @param Database|null $con |
||
| 158 | * @return int |
||
| 159 | */ |
||
| 160 | public function bulkUpsert(array $data, $id, Database $con = null) { |
||
| 161 | if(null === $con) { |
||
| 162 | $con = ParserService::getInstance()->createConnection($this->getDomain()); |
||
| 163 | } |
||
| 164 | $collection = $con->selectCollection($this->getSchema()->name); |
||
| 165 | |||
| 166 | $upserts = 0; |
||
| 167 | $filter = $options = $operations = []; |
||
| 168 | try { |
||
| 169 | // Check index collation |
||
| 170 | $indexes = $collection->listIndexes(); |
||
| 171 | foreach($indexes as $index) { |
||
| 172 | $indexInfo = $index->__debugInfo(); |
||
| 173 | $keys = array_keys($index["key"]); |
||
| 174 | if ((count($keys) === 1) && ($keys[0] === $id) && (array_key_exists("collation", $indexInfo))) { |
||
| 175 | $collation = $indexInfo["collation"]; |
||
| 176 | $options["collation"] = ["locale" => $collation["locale"], "strength" => $collation["strength"]]; |
||
| 177 | break; |
||
| 178 | } |
||
| 179 | } |
||
| 180 | |||
| 181 | foreach($data as $item) { |
||
| 182 | $filter[$id] = ['$eq' => $item[$id]]; |
||
| 183 | $update = []; |
||
| 184 | $update['$set'] = $item; |
||
| 185 | $options['upsert'] = true; |
||
| 186 | $operation = [ |
||
| 187 | "updateOne" => [$filter, $update, $options] |
||
| 188 | ]; |
||
| 189 | $operations[] = $operation; |
||
| 190 | } |
||
| 191 | /** @var BulkWriteResult $result */ |
||
| 192 | $result = $collection->bulkWrite($operations); |
||
| 193 | $upserts = $result->getModifiedCount(); |
||
| 194 | } catch (\Exception $exception) { |
||
| 195 | Logger::log($exception->getMessage(), LOG_CRIT, $this->toArray()); |
||
| 196 | } |
||
| 197 | |||
| 198 | return $upserts; |
||
| 199 | } |
||
| 200 | |||
| 201 | /** |
||
| 202 | * @param Database|null $con |
||
| 203 | * @return bool |
||
| 204 | */ |
||
| 205 | public function delete(Database $con = null) { |
||
| 222 | } |
||
| 223 | |||
| 224 | /** |
||
| 225 | * Function to make a bulk delete of documents |
||
| 226 | * @param array $filters |
||
| 227 | * @param Database|null $con |
||
| 228 | * @return int |
||
| 229 | */ |
||
| 230 | public function bulkDelete(array $filters, Database $con = null) { |
||
| 243 | } |
||
| 244 | |||
| 245 | /** |
||
| 246 | * @param array $data |
||
| 247 | * @param Database $con |
||
| 248 | * @return array |
||
| 249 | * @throws \NOSQL\Exceptions\NOSQLValidationException |
||
| 250 | */ |
||
| 251 | private function prepareInsertDtos(array $data, Database $con) |
||
| 271 | } |
||
| 272 | |||
| 273 | /** |
||
| 274 | * @param Database $con |
||
| 275 | * @param ObjectId[] $ids |
||
| 276 | * @param NOSQLModelDto[] $dtos |
||
| 277 | * @return int |
||
| 278 | * @throws \NOSQL\Exceptions\NOSQLValidationException |
||
| 279 | */ |
||
| 280 | private function parseInsertedDtos(Database $con, $ids, $dtos) |
||
| 298 |