1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spiral\Snapshotter\FileHandler\Controllers; |
4
|
|
|
|
5
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
6
|
|
|
use Spiral\Http\Exceptions\ClientExceptions\NotFoundException; |
7
|
|
|
use Spiral\Http\Request\InputManager; |
8
|
|
|
use Spiral\Http\Response\ResponseWrapper; |
9
|
|
|
use Spiral\Snapshotter\AbstractController; |
10
|
|
|
use Spiral\Snapshotter\FileHandler\Services\SnapshotService; |
11
|
|
|
use Spiral\Snapshotter\Helpers\Names; |
12
|
|
|
use Spiral\Snapshotter\Helpers\Timestamps; |
13
|
|
|
|
14
|
|
|
use Spiral\Vault\Vault; |
15
|
|
|
use Spiral\Views\ViewManager; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @property InputManager $input |
19
|
|
|
* @property ViewManager $views |
20
|
|
|
* @property Vault $vault |
21
|
|
|
* @property ResponseWrapper $response |
22
|
|
|
*/ |
23
|
|
|
class SnapshotsController extends AbstractController |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* List of snapshots. |
27
|
|
|
* |
28
|
|
|
* @param SnapshotService $service |
29
|
|
|
* @param Timestamps $timestamps |
30
|
|
|
* @param Names $names |
31
|
|
|
* @return string |
32
|
|
|
*/ |
33
|
|
|
public function indexAction(SnapshotService $service, Timestamps $timestamps, Names $names) |
34
|
|
|
{ |
35
|
|
|
$snapshots = $service->getSnapshots(); |
36
|
|
|
|
37
|
|
|
return $this->views->render('snapshotter:file/list', [ |
38
|
|
|
'selector' => $snapshots, |
39
|
|
|
'timestamps' => $timestamps, |
40
|
|
|
'names' => $names |
41
|
|
|
]); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* View snapshot. |
46
|
|
|
* |
47
|
|
|
* @param SnapshotService $service |
48
|
|
|
* @param Timestamps $timestamps |
49
|
|
|
* @return string |
50
|
|
|
*/ |
51
|
|
View Code Duplication |
public function viewAction(SnapshotService $service, Timestamps $timestamps) |
|
|
|
|
52
|
|
|
{ |
53
|
|
|
$filename = $this->input->input('filename'); |
54
|
|
|
$snapshot = $service->getSnapshot($filename); |
55
|
|
|
|
56
|
|
|
if (empty($snapshot)) { |
57
|
|
|
throw new NotFoundException; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
$this->authorize('view', compact('snapshot')); |
61
|
|
|
|
62
|
|
|
return $this->views->render('snapshotter:file/snapshot', [ |
63
|
|
|
'snapshot' => $snapshot, |
64
|
|
|
'timestamps' => $timestamps, |
65
|
|
|
]); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* View last snapshot incident source. |
70
|
|
|
* |
71
|
|
|
* @param SnapshotService $service |
72
|
|
|
* @return string |
73
|
|
|
*/ |
74
|
|
|
public function iframeAction(SnapshotService $service) |
75
|
|
|
{ |
76
|
|
|
$filename = $this->input->input('filename'); |
77
|
|
|
$snapshot = $service->getSnapshot($filename); |
78
|
|
|
|
79
|
|
|
if (empty($snapshot)) { |
80
|
|
|
throw new NotFoundException; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
$this->authorize('view', compact('snapshot')); |
84
|
|
|
|
85
|
|
|
return $service->read($snapshot); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Remove all snapshots with all incident records. |
90
|
|
|
* |
91
|
|
|
* @param SnapshotService $service |
92
|
|
|
* @return array|\Psr\Http\Message\ResponseInterface |
93
|
|
|
*/ |
94
|
|
|
public function removeAllAction(SnapshotService $service) |
95
|
|
|
{ |
96
|
|
|
$this->authorize('removeAll'); |
97
|
|
|
|
98
|
|
|
$service->deleteSnapshots(); |
99
|
|
|
|
100
|
|
|
$uri = $this->vault->uri('snapshots'); |
101
|
|
|
|
102
|
|
|
if ($this->input->isAjax()) { |
103
|
|
|
return [ |
104
|
|
|
'status' => 200, |
105
|
|
|
'message' => $this->say('Snapshots deleted.'), |
106
|
|
|
'action' => ['redirect' => $uri] |
107
|
|
|
]; |
108
|
|
|
} else { |
109
|
|
|
return $this->response->redirect($uri); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Remove single snapshot with all incident records. |
115
|
|
|
* |
116
|
|
|
* @param SnapshotService $service |
117
|
|
|
* @param ServerRequestInterface $request |
118
|
|
|
* @return array|\Psr\Http\Message\ResponseInterface |
119
|
|
|
*/ |
120
|
|
View Code Duplication |
public function removeAction(SnapshotService $service, ServerRequestInterface $request) |
|
|
|
|
121
|
|
|
{ |
122
|
|
|
$filename = $this->input->input('filename'); |
123
|
|
|
$snapshot = $service->getSnapshot($filename); |
124
|
|
|
|
125
|
|
|
if (empty($snapshot)) { |
126
|
|
|
throw new NotFoundException; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
$this->authorize('remove', compact('snapshot')); |
130
|
|
|
|
131
|
|
|
$service->deleteSnapshot($snapshot); |
132
|
|
|
|
133
|
|
|
$uri = $this->removeBackURI($request); |
134
|
|
|
|
135
|
|
|
if ($this->input->isAjax()) { |
136
|
|
|
return [ |
137
|
|
|
'status' => 200, |
138
|
|
|
'message' => $this->say('Snapshot deleted.'), |
139
|
|
|
'action' => ['redirect' => $uri] |
140
|
|
|
]; |
141
|
|
|
} else { |
142
|
|
|
return $this->response->redirect($uri); |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Build redirect URI for removal operation. |
148
|
|
|
* |
149
|
|
|
* @param ServerRequestInterface $request |
150
|
|
|
* @return \Psr\Http\Message\UriInterface |
151
|
|
|
*/ |
152
|
|
View Code Duplication |
protected function removeBackURI(ServerRequestInterface $request) |
|
|
|
|
153
|
|
|
{ |
154
|
|
|
$query = $request->getQueryParams(); |
155
|
|
|
if (array_key_exists('backToList', $query)) { |
156
|
|
|
$uri = $this->vault->uri('snapshots'); |
157
|
|
|
} else { |
158
|
|
|
$uri = $request->getServerParams()['HTTP_REFERER']; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
return $uri; |
162
|
|
|
} |
163
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.