Completed
Push — develop ( cfb231...3b7914 )
by Abdelrahman
01:39
created

PagesController::delete()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 9
rs 9.6666
cc 1
eloc 5
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cortex\Pages\Http\Controllers\Managerarea;
6
7
use Illuminate\Http\Request;
8
use Rinvex\Pages\Contracts\PageContract;
9
use Cortex\Foundation\DataTables\LogsDataTable;
10
use Cortex\Pages\DataTables\Managerarea\PagesDataTable;
11
use Cortex\Pages\Http\Requests\Managerarea\PageFormRequest;
12
use Cortex\Foundation\Http\Controllers\AuthorizedController;
13
14
class PagesController extends AuthorizedController
15
{
16
    /**
17
     * {@inheritdoc}
18
     */
19
    protected $resource = 'pages';
20
21
    /**
22
     * Display a listing of the resource.
23
     *
24
     * @param \Cortex\Pages\DataTables\Managerarea\PagesDataTable $pagesDataTable
25
     *
26
     * @return \Illuminate\Http\JsonResponse|\Illuminate\View\View
27
     */
28
    public function index(PagesDataTable $pagesDataTable)
29
    {
30
        return $pagesDataTable->with([
31
            'id' => 'cortex-pages',
32
            'phrase' => trans('cortex/pages::common.pages'),
33
        ])->render('cortex/tenants::managerarea.pages.datatable');
34
    }
35
36
    /**
37
     * Display a listing of the resource logs.
38
     *
39
     * @param \Rinvex\Pages\Contracts\PageContract        $page
40
     * @param \Cortex\Foundation\DataTables\LogsDataTable $logsDataTable
41
     *
42
     * @return \Illuminate\Http\JsonResponse|\Illuminate\View\View
43
     */
44
    public function logs(PageContract $page, LogsDataTable $logsDataTable)
45
    {
46
        return $logsDataTable->with([
47
            'tab' => 'logs',
48
            'type' => 'pages',
49
            'resource' => $page,
50
            'id' => 'cortex-pages-logs',
51
            'phrase' => trans('cortex/pages::common.pages'),
52
        ])->render('cortex/tenants::managerarea.pages.datatable-tab');
53
    }
54
55
    /**
56
     * Store a newly created resource in storage.
57
     *
58
     * @param \Cortex\Pages\Http\Requests\Managerarea\PageFormRequest $request
59
     *
60
     * @return \Illuminate\Http\Response
0 ignored issues
show
Documentation introduced by
Should the return type not be \Illuminate\Http\Redirec...inate\Http\JsonResponse?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
61
     */
62
    public function store(PageFormRequest $request)
63
    {
64
        return $this->process($request, app('rinvex.pages.page'));
65
    }
66
67
    /**
68
     * Update the given resource in storage.
69
     *
70
     * @param \Cortex\Pages\Http\Requests\Managerarea\PageFormRequest $request
71
     * @param \Rinvex\Pages\Contracts\PageContract                   $page
72
     *
73
     * @return \Illuminate\Http\Response
0 ignored issues
show
Documentation introduced by
Should the return type not be \Illuminate\Http\Redirec...inate\Http\JsonResponse?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
74
     */
75
    public function update(PageFormRequest $request, PageContract $page)
76
    {
77
        return $this->process($request, $page);
78
    }
79
80
    /**
81
     * Delete the given resource from storage.
82
     *
83
     * @param \Rinvex\Pages\Contracts\PageContract $page
84
     *
85
     * @return \Illuminate\Http\Response
0 ignored issues
show
Documentation introduced by
Should the return type not be \Illuminate\Http\Redirec...inate\Http\JsonResponse?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
86
     */
87
    public function delete(PageContract $page)
88
    {
89
        $page->delete();
90
91
        return intend([
92
            'url' => route('managerarea.pages.index'),
93
            'with' => ['warning' => trans('cortex/pages::messages.page.deleted', ['slug' => $page->slug])],
0 ignored issues
show
Bug introduced by
Accessing slug on the interface Rinvex\Pages\Contracts\PageContract suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
94
        ]);
95
    }
96
97
    /**
98
     * Show the form for create/update of the given resource.
99
     *
100
     * @param \Rinvex\Pages\Contracts\PageContract $page
101
     *
102
     * @return \Illuminate\Http\Response
0 ignored issues
show
Documentation introduced by
Should the return type not be \Illuminate\View\View|\I...\Contracts\View\Factory?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
103
     */
104
    public function form(PageContract $page)
105
    {
106
        return view('cortex/pages::managerarea.forms.page', compact('page'));
107
    }
108
109
    /**
110
     * Process the form for store/update of the given resource.
111
     *
112
     * @param \Illuminate\Http\Request             $request
113
     * @param \Rinvex\Pages\Contracts\PageContract $page
114
     *
115
     * @return \Illuminate\Http\Response
0 ignored issues
show
Documentation introduced by
Should the return type not be \Illuminate\Http\Redirec...inate\Http\JsonResponse?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
116
     */
117
    protected function process(Request $request, PageContract $page)
118
    {
119
        // Prepare required input fields
120
        $data = $request->all();
121
122
        // Verify existing view
123
        if (! view()->exists($data['view'])) {
0 ignored issues
show
Bug introduced by
The method exists does only exist in Illuminate\Contracts\View\Factory, but not in Illuminate\View\View.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
124
            return intend([
125
                'back' => true,
126
                'withInput' => $request->all(),
127
                'withErrors' => ['view' => trans('cortex/pages::messages.page.invalid_view')],
128
            ]);
129
        }
130
131
        // Save page
132
        $page->fill($data)->save();
133
134
        return intend([
135
            'url' => route('managerarea.pages.index'),
136
            'with' => ['success' => trans('cortex/pages::messages.page.saved', ['slug' => $page->slug])],
0 ignored issues
show
Bug introduced by
Accessing slug on the interface Rinvex\Pages\Contracts\PageContract suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
137
        ]);
138
    }
139
}
140