Passed
Push — master ( c98abf...91bafd )
by Jonathan
19:18
created

DeleteController::deleteRelation()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 10
c 0
b 0
f 0
nc 3
nop 1
dl 0
loc 18
rs 9.9332
1
<?php
2
3
namespace Uccello\Core\Http\Controllers\Core;
4
5
use Illuminate\Http\Request;
0 ignored issues
show
Bug introduced by
The type Illuminate\Http\Request was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use Uccello\Core\Models\Domain;
7
use Uccello\Core\Models\Module;
8
use Uccello\Core\Events\BeforeDeleteEvent;
9
use Uccello\Core\Events\AfterDeleteEvent;
10
11
class DeleteController extends Controller
12
{
13
    protected $viewName = null;
14
15
    /**
16
     * Check user permissions
17
     */
18
    protected function checkPermissions()
19
    {
20
        $this->middleware('uccello.permissions:delete');
21
    }
22
23
    /**
24
     * @inheritDoc
25
     */
26
    public function process(?Domain $domain, Module $module, Request $request)
27
    {
28
        // Pre-process
29
        $this->preProcess($domain, $module, $request);
30
        
31
        $this->deleteRecord($module, $request);
32
        $message = 'notification.record.deleted';
33
34
        // Notification
35
        if (!empty($message)) {
0 ignored issues
show
introduced by
The condition empty($message) is always false.
Loading history...
36
            ucnotify(uctrans($message, $module), 'success');
0 ignored issues
show
Bug introduced by
It seems like uctrans($message, $module) can also be of type array; however, parameter $message of ucnotify() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

36
            ucnotify(/** @scrutinizer ignore-type */ uctrans($message, $module), 'success');
Loading history...
37
        }
38
39
        // Redirect to the previous page
40
        $route = $this->getRedirectionRoute();
41
42
        return redirect($route);
0 ignored issues
show
Bug introduced by
The function redirect was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

42
        return /** @scrutinizer ignore-call */ redirect($route);
Loading history...
43
    }
44
45
    /**
46
     * Retrieve the redirection route from request parameters.
47
     *
48
     * @return string
49
     */
50
    protected function getRedirectionRoute() : string
51
    {
52
        $request = $this->request;
0 ignored issues
show
Unused Code introduced by
The assignment to $request is dead and can be removed.
Loading history...
53
        $domain = $this->domain;
54
        $module = $this->module;
55
56
        return ucroute('uccello.list', $domain, $module);
57
    }
58
59
    /**
60
     * Delete record after retrieving from the request
61
     *
62
     * @param Module $module
63
     * @param Request $request
64
     * @return void
65
     */
66
    protected function deleteRecord(Module $module, Request $request)
67
    {
68
        $record = $this->getRecordFromRequest();
69
70
        // Delete record if exists
71
        if ($record) {
72
            event(new BeforeDeleteEvent($this->domain, $module, $request, $record, 'delete'));
0 ignored issues
show
Bug introduced by
The function event was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

72
            /** @scrutinizer ignore-call */ 
73
            event(new BeforeDeleteEvent($this->domain, $module, $request, $record, 'delete'));
Loading history...
73
74
            $record->delete();
75
76
            event(new AfterDeleteEvent($this->domain, $module, $request, $record, 'delete'));
77
        }
78
    }
79
}
80