Conditions | 6 |
Paths | 6 |
Total Lines | 45 |
Code Lines | 22 |
Lines | 0 |
Ratio | 0 % |
Tests | 18 |
CRAP Score | 6.2163 |
Changes | 4 | ||
Bugs | 0 | Features | 0 |
1 | <?php |
||
103 | 3 | public function restore() |
|
104 | { |
||
105 | 3 | if (!$this instanceof TrashInterface) |
|
106 | { |
||
107 | // When trying to restore normal document instead of trash item |
||
108 | throw new Exception(sprintf('Restore can be performed only on `%s` instance', TrashInterface::class)); |
||
109 | } |
||
110 | 3 | $em = new EntityManager($this->data); |
|
1 ignored issue
–
show
|
|||
111 | |||
112 | // Set scenario to `restore` for model, which is just about to be restored |
||
113 | 3 | ScenarioManager::setScenario($this->data, TrashInterface::ScenarioRestore); |
|
1 ignored issue
–
show
|
|||
114 | |||
115 | 3 | if (!Event::valid($this->data, TrashInterface::EventBeforeRestore)) |
|
1 ignored issue
–
show
|
|||
116 | { |
||
117 | 1 | return false; |
|
118 | } |
||
119 | |||
120 | 2 | $saved = $em->save(); |
|
121 | 2 | if (!$saved) |
|
122 | { |
||
123 | return false; |
||
124 | } |
||
125 | 2 | $finder = new Finder($this->data); |
|
1 ignored issue
–
show
|
|||
126 | 2 | $model = $finder->find(PkManager::prepareFromModel($this->data)); |
|
1 ignored issue
–
show
|
|||
127 | 2 | if (!$model) |
|
128 | { |
||
129 | return false; |
||
130 | } |
||
131 | 2 | $eventAfter = new RestoreEvent(); |
|
132 | 2 | $eventAfter->setTrashed($this->data); |
|
1 ignored issue
–
show
|
|||
133 | 2 | $eventAfter->setTrash($this); |
|
134 | 2 | if (!Event::valid($model, TrashInterface::EventAfterRestore, $eventAfter)) |
|
135 | { |
||
136 | return false; |
||
137 | } |
||
138 | |||
139 | 2 | $trashEm = new EntityManager($this); |
|
140 | |||
141 | 2 | $this->data = null; |
|
1 ignored issue
–
show
|
|||
142 | |||
143 | // Use deleteOne, to avoid beforeDelete event, |
||
144 | // which should be raised only when really removing document: |
||
145 | // when emtying trash |
||
146 | 2 | return $trashEm->deleteOne(PkManager::prepareFromModel($this)); |
|
147 | } |
||
148 | |||
150 |
If you access a property on an interface, you most likely code against a concrete implementation of the interface.
Available Fixes
Adding an additional type check:
Changing the type hint: