Conditions | 16 |
Paths | 16 |
Total Lines | 112 |
Code Lines | 77 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
67 | public function handle() |
||
68 | { |
||
69 | $timer = new \DateTime(); |
||
70 | $this->setFormproperties(); |
||
71 | $this->setLanguage(); |
||
72 | $this->userId = $this->import->user_id; |
||
73 | $vocabId = $this->isElementSet() ? $this->import->schema_id : $this->import->vocabulary_id; |
||
74 | $changeset = $this->import->instructions; |
||
75 | $total_processed = 0; |
||
76 | $added = 0; |
||
77 | $updated = 0; |
||
78 | $deleted = 0; |
||
79 | //each item in the main array is a row. Each item in the statements array is a statement |
||
80 | foreach ($changeset['update'] as $reg_id => $row) { |
||
81 | $this->updatedStatements = []; |
||
82 | //start a transaction |
||
83 | DB::transaction(function () use ($reg_id, $row, &$updated) { |
||
84 | $statements = $this->getStatements($reg_id); |
||
85 | $dirty = false; |
||
86 | foreach ($row as $statement) { |
||
87 | $old = $statements->find($statement['statement_id']); |
||
88 | if ($old) { |
||
89 | if ($statement['new value']) { |
||
90 | $old->update([ |
||
91 | 'object' => $statement['new value'], |
||
92 | 'last_import_id' => $this->import->id, |
||
93 | 'is_generated' => false, |
||
94 | ]); |
||
95 | $this->addUpdateStatement($statement); |
||
96 | $dirty = true; |
||
97 | } else { |
||
98 | $old->delete(); |
||
99 | $this->addUpdateStatement($statement); |
||
100 | $dirty = true; |
||
101 | } |
||
102 | } else { |
||
103 | //make a new one |
||
104 | $existingStatement = $statements->filter(function ($item) use ($statement) { |
||
105 | /* @var Model $item */ |
||
106 | return $item->profile_property_id == $statement['property_id'] && |
||
107 | $item->object == $statement['new value'] && |
||
108 | $item->getOriginal('language') === $statement['language']; |
||
109 | }); |
||
110 | if ($existingStatement->count() === 0) { |
||
111 | $newStatement = $this->makeStatement($statement); |
||
112 | $this->resource->statements()->save($newStatement); |
||
113 | $this->addUpdateStatement($statement); |
||
114 | $dirty = true; |
||
115 | } |
||
116 | } |
||
117 | } |
||
118 | if (\count($this->updatedStatements)) { |
||
119 | try { |
||
120 | $this->resource->updated_user_id = $this->userId; |
||
121 | $this->resource->updateFromStatements($this->updatedStatements); |
||
122 | } catch (Exception $e) { |
||
123 | //log the error |
||
124 | $this->makeErrorLogEntry($row['*uri']['new value'], $e->getMessage()); |
||
125 | //cancel the transaction |
||
126 | return false; |
||
127 | } |
||
128 | } |
||
129 | if ($dirty) { |
||
130 | $updated++; |
||
131 | } |
||
132 | }); |
||
133 | $total_processed++; |
||
134 | } |
||
135 | foreach ($changeset['add'] as $row) { |
||
136 | DB::transaction(function () use ($row, $vocabId, &$added) { |
||
137 | $allStatements = []; |
||
138 | foreach ($row as $statement) { |
||
139 | $this->addUpdateStatement($statement); |
||
140 | $allStatements[] = $this->makeStatement($statement); |
||
141 | } |
||
142 | try { |
||
143 | //this will make a resource with insufficient values... |
||
144 | $resource = $this->makeResource($vocabId); |
||
145 | //this will update the resource from the statements and save it... |
||
146 | $resource->updateFromStatements($this->updatedStatements); |
||
147 | //this will add the statements... |
||
148 | $resource->statements()->saveMany($allStatements); |
||
149 | $this->UpdatePrefLabelId($resource); |
||
150 | } catch (Exception $e) { |
||
151 | //log the error |
||
152 | $this->makeErrorLogEntry($row['*uri']['new value'], $e->getMessage()); |
||
153 | //cancel the transaction |
||
154 | return false; |
||
155 | } |
||
156 | |||
157 | $added++; |
||
158 | }); |
||
159 | $total_processed++; |
||
160 | } |
||
161 | foreach ($changeset['delete'] as $row) { |
||
162 | //TODO implement this... |
||
163 | //delete the resource |
||
164 | //cascade delete the statements, which should cascade delete the reciprocals |
||
165 | $total_processed++; |
||
166 | $deleted++; |
||
167 | } |
||
168 | $this->setResults('timer', $timer->diff(new \DateTime())->format('%h hours; %i minutes; %s seconds')); |
||
169 | $this->import->results = $this->results; |
||
170 | //$this->import->batch->increment('handled_count'); |
||
171 | $this->import->total_processed_count = $total_processed; |
||
172 | $this->import->added_count = $added; |
||
173 | $this->import->updated_count = $updated; |
||
174 | $this->import->deleted_count = $deleted; |
||
175 | $this->import->imported_at = new \DateTime(); |
||
176 | $this->import->save(); |
||
177 | |||
178 | event(new ImportFinished($this->import)); |
||
179 | } |
||
297 |