SnapshotsController::removeAllAction()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 9.6666
c 0
b 0
f 0
cc 2
nc 2
nop 1
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\Timestamps;
12
use Spiral\Vault\Vault;
13
use Spiral\Views\ViewManager;
14
15
/**
16
 * @property InputManager    $input
17
 * @property ViewManager     $views
18
 * @property Vault           $vault
19
 * @property ResponseWrapper $response
20
 */
21
class SnapshotsController extends AbstractController
22
{
23
    /**
24
     * List of snapshots.
25
     *
26
     * @param SnapshotService $service
27
     * @param Timestamps      $timestamps
28
     * @return string
29
     */
30
    public function indexAction(SnapshotService $service, Timestamps $timestamps)
31
    {
32
        $snapshots = $service->getSnapshots();
33
34
        return $this->views->render('snapshotter:file/list', [
35
            'selector'   => $snapshots->paginate(20),
36
            'timestamps' => $timestamps
37
        ]);
38
    }
39
40
    /**
41
     * View snapshot.
42
     *
43
     * @param SnapshotService $service
44
     * @param Timestamps      $timestamps
45
     * @return string
46
     */
47 View Code Duplication
    public function viewAction(SnapshotService $service, Timestamps $timestamps)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
48
    {
49
        $filename = $this->input->input('filename');
50
        $snapshot = $service->getSnapshot($filename);
51
52
        if (empty($snapshot)) {
53
            throw new NotFoundException;
54
        }
55
56
        $this->authorize('view', compact('snapshot'));
57
58
        return $this->views->render('snapshotter:file/snapshot', [
59
            'snapshot'   => $snapshot,
60
            'timestamps' => $timestamps,
61
        ]);
62
    }
63
64
    /**
65
     * View last snapshot incident source.
66
     *
67
     * @param SnapshotService $service
68
     * @return string
69
     */
70
    public function iframeAction(SnapshotService $service)
71
    {
72
        $filename = $this->input->input('filename');
73
        $snapshot = $service->getSnapshot($filename);
74
75
        if (empty($snapshot)) {
76
            throw new NotFoundException;
77
        }
78
79
        $this->authorize('view', compact('snapshot'));
80
81
        return $service->read($snapshot);
82
    }
83
84
    /**
85
     * Remove all snapshots with all incident records.
86
     *
87
     * @param SnapshotService $service
88
     * @return array|\Psr\Http\Message\ResponseInterface
89
     */
90
    public function removeAllAction(SnapshotService $service)
91
    {
92
        $this->authorize('removeAll');
93
94
        $service->deleteSnapshots();
95
96
        $uri = $this->vault->uri('snapshots');
97
98
        if ($this->input->isAjax()) {
99
            return [
100
                'status'  => 200,
101
                'message' => $this->say('Snapshots deleted.'),
102
                'action'  => ['redirect' => $uri]
103
            ];
104
        } else {
105
            return $this->response->redirect($uri);
106
        }
107
    }
108
109
    /**
110
     * Remove single snapshot with all incident records.
111
     *
112
     * @param SnapshotService        $service
113
     * @param ServerRequestInterface $request
114
     * @return array|\Psr\Http\Message\ResponseInterface
115
     */
116 View Code Duplication
    public function removeAction(SnapshotService $service, ServerRequestInterface $request)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
117
    {
118
        $filename = $this->input->input('filename');
119
        $snapshot = $service->getSnapshot($filename);
120
121
        if (empty($snapshot)) {
122
            throw new NotFoundException;
123
        }
124
125
        $this->authorize('remove', compact('snapshot'));
126
127
        $service->deleteSnapshot($snapshot);
128
129
        $uri = $this->removeBackURI($request);
130
131
        if ($this->input->isAjax()) {
132
            return [
133
                'status'  => 200,
134
                'message' => $this->say('Snapshot deleted.'),
135
                'action'  => ['redirect' => $uri]
136
            ];
137
        } else {
138
            return $this->response->redirect($uri);
139
        }
140
    }
141
142
    /**
143
     * Build redirect URI for removal operation.
144
     *
145
     * @param ServerRequestInterface $request
146
     * @return \Psr\Http\Message\UriInterface
147
     */
148 View Code Duplication
    protected function removeBackURI(ServerRequestInterface $request)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
149
    {
150
        $query = $request->getQueryParams();
151
        if (array_key_exists('backToList', $query)) {
152
            $uri = $this->vault->uri('snapshots');
153
        } else {
154
            $uri = $request->getServerParams()['HTTP_REFERER'];
155
        }
156
157
        return $uri;
158
    }
159
}