1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This software package is licensed under AGPL or Commercial license. |
5
|
|
|
* |
6
|
|
|
* @package maslosoft/mangan |
7
|
|
|
* @licence AGPL or Commercial |
8
|
|
|
* @copyright Copyright (c) Piotr Masełkowski <[email protected]> |
9
|
|
|
* @copyright Copyright (c) Maslosoft |
10
|
|
|
* @copyright Copyright (c) Others as mentioned in code |
11
|
|
|
* @link http://maslosoft.com/mangan/ |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Maslosoft\Mangan\Traits\Model; |
15
|
|
|
|
16
|
|
|
use Exception; |
17
|
|
|
use Maslosoft\Mangan\EntityManager; |
18
|
|
|
use Maslosoft\Mangan\Events\Event; |
19
|
|
|
use Maslosoft\Mangan\Events\ModelEvent; |
20
|
|
|
use Maslosoft\Mangan\Events\RestoreEvent; |
21
|
|
|
use Maslosoft\Mangan\Events\TrashEvent; |
22
|
|
|
use Maslosoft\Mangan\Finder; |
23
|
|
|
use Maslosoft\Mangan\Helpers\PkManager; |
24
|
|
|
use Maslosoft\Mangan\Interfaces\TrashInterface; |
25
|
|
|
use Maslosoft\Mangan\Meta\ManganMeta; |
26
|
|
|
use Maslosoft\Mangan\Model\Trash; |
27
|
|
|
use Maslosoft\Mangan\ScenarioManager; |
28
|
|
|
use Maslosoft\Mangan\Validator; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Uswe this trait to make model trashable |
32
|
|
|
* |
33
|
|
|
* @author Piotr |
34
|
|
|
*/ |
35
|
|
|
trait TrashableTrait |
36
|
|
|
{ |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Move to trash. Validation will be performed |
40
|
|
|
* before trashing with `trash` (TrashInterface::ScenarioTrash) scenario. |
41
|
|
|
* |
42
|
|
|
* |
43
|
|
|
* @see TrashInterface::ScenarioTrash |
44
|
|
|
* @param boolean $runValidation whether to perform validation before saving the record. |
45
|
|
|
* If the validation fails, the record will not be saved to database. |
46
|
|
|
* |
47
|
|
|
* @return boolean |
48
|
|
|
* @Ignored |
49
|
|
|
*/ |
50
|
4 |
|
public function trash($runValidation = true) |
51
|
|
|
{ |
52
|
4 |
|
ScenarioManager::setScenario($this, TrashInterface::ScenarioTrash); |
53
|
4 |
|
$validator = new Validator($this); |
54
|
4 |
|
if (!$runValidation || $validator->validate()) |
55
|
|
|
{ |
56
|
4 |
|
$eventBefore = new TrashEvent($this); |
57
|
4 |
|
if (Event::hasHandler($this, TrashInterface::EventBeforeTrash)) |
58
|
|
|
{ |
59
|
|
|
if (!Event::valid($this, TrashInterface::EventBeforeTrash, $eventBefore)) |
60
|
|
|
{ |
61
|
|
|
return false; |
62
|
|
|
} |
63
|
|
|
} |
64
|
4 |
|
$meta = ManganMeta::create($this); |
65
|
|
|
|
66
|
4 |
|
$trash = $eventBefore->getTrash(); |
67
|
4 |
|
if (empty($trash)) |
68
|
|
|
{ |
69
|
4 |
|
$trash = new Trash(); |
70
|
|
|
} |
71
|
4 |
|
$trash->name = (string) $this; |
|
|
|
|
72
|
4 |
|
$trash->data = $this; |
|
|
|
|
73
|
4 |
|
$trash->type = isset($meta->type()->label) ? $meta->type()->label : get_class($this); |
|
|
|
|
74
|
4 |
|
if (!$trash->save()) |
75
|
|
|
{ |
76
|
|
|
return false; |
77
|
|
|
} |
78
|
|
|
|
79
|
4 |
|
$eventAfter = new TrashEvent($this); |
80
|
4 |
|
$eventAfter->setTrash($trash); |
81
|
|
|
|
82
|
4 |
|
if (!Event::valid($this, TrashInterface::EventAfterTrash, $eventAfter)) |
83
|
|
|
{ |
84
|
|
|
return false; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
// Use deleteOne, to avoid beforeDelete event, |
88
|
|
|
// which should be raised only when really removing document: |
89
|
|
|
// when emtying trash |
90
|
|
|
|
91
|
4 |
|
$em = new EntityManager($this); |
92
|
4 |
|
return $em->deleteOne(PkManager::prepareFromModel($this)); |
93
|
|
|
} |
94
|
|
|
return false; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Restore trashed item |
99
|
|
|
* @return boolean |
100
|
|
|
* @throws Exception |
101
|
|
|
* @Ignored |
102
|
|
|
*/ |
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); |
|
|
|
|
111
|
|
|
|
112
|
|
|
// Set scenario to `restore` for model, which is just about to be restored |
113
|
3 |
|
ScenarioManager::setScenario($this->data, TrashInterface::ScenarioRestore); |
|
|
|
|
114
|
|
|
|
115
|
3 |
|
if (!Event::valid($this->data, TrashInterface::EventBeforeRestore)) |
|
|
|
|
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); |
|
|
|
|
126
|
2 |
|
$model = $finder->find(PkManager::prepareFromModel($this->data)); |
|
|
|
|
127
|
2 |
|
if (!$model) |
128
|
|
|
{ |
129
|
|
|
return false; |
130
|
|
|
} |
131
|
2 |
|
$eventAfter = new RestoreEvent(); |
132
|
2 |
|
$eventAfter->setTrashed($this->data); |
|
|
|
|
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; |
|
|
|
|
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
|
|
|
|
149
|
|
|
} |
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: