Completed
Push — develop ( 0e2361...50a387 )
by Abdelrahman
01:29
created

PagesController::process()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 23
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 23
rs 9.0856
c 0
b 0
f 0
cc 2
eloc 12
nc 2
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cortex\Pages\Http\Controllers\Tenantarea;
6
7
use Illuminate\Http\Request;
8
use Rinvex\Pages\Contracts\PageContract;
9
use Cortex\Foundation\DataTables\LogsDataTable;
10
use Cortex\Pages\DataTables\Tenantarea\PagesDataTable;
11
use Cortex\Pages\Http\Requests\Tenantarea\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
     * @return \Illuminate\Http\Response
25
     */
26
    public function index()
27
    {
28
        return app(PagesDataTable::class)->with([
29
            'id' => 'cortex-pages',
30
            'phrase' => trans('cortex/pages::common.pages'),
31
        ])->render('cortex/foundation::tenantarea.pages.datatable');
32
    }
33
34
    /**
35
     * Display a listing of the resource logs.
36
     *
37
     * @return \Illuminate\Http\Response
38
     */
39
    public function logs(PageContract $page)
40
    {
41
        return app(LogsDataTable::class)->with([
42
            'type' => 'pages',
43
            'resource' => $page,
44
            'id' => 'cortex-pages-logs',
45
            'phrase' => trans('cortex/pages::common.pages'),
46
        ])->render('cortex/foundation::tenantarea.pages.datatable-logs');
47
    }
48
49
    /**
50
     * Store a newly created resource in storage.
51
     *
52
     * @param \Cortex\Pages\Http\Requests\Tenantarea\PageFormRequest $request
53
     *
54
     * @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...
55
     */
56
    public function store(PageFormRequest $request)
57
    {
58
        return $this->process($request, app('rinvex.pages.page'));
59
    }
60
61
    /**
62
     * Update the given resource in storage.
63
     *
64
     * @param \Cortex\Pages\Http\Requests\Tenantarea\PageFormRequest $request
65
     * @param \Rinvex\Pages\Contracts\PageContract                  $page
66
     *
67
     * @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...
68
     */
69
    public function update(PageFormRequest $request, PageContract $page)
70
    {
71
        return $this->process($request, $page);
72
    }
73
74
    /**
75
     * Delete the given resource from storage.
76
     *
77
     * @param \Rinvex\Pages\Contracts\PageContract $page
78
     *
79
     * @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...
80
     */
81
    public function delete(PageContract $page)
82
    {
83
        $page->delete();
84
85
        return intend([
86
            'url' => route('tenantarea.pages.index'),
87
            '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...
88
        ]);
89
    }
90
91
    /**
92
     * Show the form for create/update of the given resource.
93
     *
94
     * @param \Rinvex\Pages\Contracts\PageContract $page
95
     *
96
     * @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...
97
     */
98
    public function form(PageContract $page)
99
    {
100
        return view('cortex/pages::tenantarea.forms.page', compact('page'));
101
    }
102
103
    /**
104
     * Process the form for store/update of the given resource.
105
     *
106
     * @param \Illuminate\Http\Request             $request
107
     * @param \Rinvex\Pages\Contracts\PageContract $page
108
     *
109
     * @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...
110
     */
111
    protected function process(Request $request, PageContract $page)
112
    {
113
        // Prepare required input fields
114
        $data = $request->all();
115
116
        // Verify existing view
117
        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...
118
            return intend([
119
                'back' => true,
120
                'withInput' => $request->all(),
121
                'withErrors' => ['view' => trans('cortex/pages::messages.page.invalid_view')],
122
            ]);
123
        }
124
125
        // Save page
126
        $page->fill($data)->save();
127
        $page->attachTenants(config('rinvex.tenants.tenant.active'));
0 ignored issues
show
Bug introduced by
The method attachTenants() does not seem to exist on object<Rinvex\Pages\Contracts\PageContract>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
128
129
        return intend([
130
            'url' => route('tenantarea.pages.index'),
131
            '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...
132
        ]);
133
    }
134
}
135