Completed
Push — master ( 76b9d6...f2a606 )
by Terzi
04:33
created

CrudActions::getCachedResponse()   B

Complexity

Conditions 8
Paths 10

Size

Total Lines 32
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 72

Importance

Changes 0
Metric Value
cc 8
eloc 15
nc 10
nop 3
dl 0
loc 32
ccs 0
cts 16
cp 0
crap 72
rs 8.4444
c 0
b 0
f 0
1
<?php
2
3
namespace Terranet\Administrator\Services;
4
5
use Czim\Paperclip\Attachment\Attachment;
0 ignored issues
show
Bug introduced by
The type Czim\Paperclip\Attachment\Attachment 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 Illuminate\Database\Eloquent\Model;
7
use Illuminate\Support\Facades\Gate;
8
use Terranet\Administrator\Actions\RemoveSelected;
9
use Terranet\Administrator\Actions\SaveOrder;
10
use Terranet\Administrator\Contracts\Services\CrudActions as CrudActionsContract;
11
use Terranet\Administrator\Contracts\Services\Saver;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Terranet\Administrator\Services\Saver. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
12
use Terranet\Administrator\Exception;
13
use Terranet\Administrator\Requests\UpdateRequest;
14
use Terranet\Administrator\Scaffolding;
15
use Terranet\Administrator\Traits\ExportsCollection;
16
use Terranet\Rankable\Rankable;
0 ignored issues
show
Bug introduced by
The type Terranet\Rankable\Rankable 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...
17
18
class CrudActions implements CrudActionsContract
19
{
20
    use ExportsCollection;
0 ignored issues
show
introduced by
The trait Terranet\Administrator\Traits\ExportsCollection requires some properties which are not provided by Terranet\Administrator\Services\CrudActions: $orders, $unionOrders
Loading history...
21
22
    /** @var Scaffolding */
23
    protected $module;
24
25
    public function __construct($module)
26
    {
27
        $this->module = $module;
28
    }
29
30
    /**
31
     * Default custom list of actions.
32
     *
33
     * @return array
34
     */
35
    public function actions()
36
    {
37
        return [];
38
    }
39
40
    /**
41
     * Default list of batch actions.
42
     *
43
     * @return array
44
     */
45
    public function batchActions()
46
    {
47
        $actions = [RemoveSelected::class];
48
49
        if ($this->module->model() instanceof Rankable) {
50
            array_push($actions, SaveOrder::class);
51
        }
52
53
        return $actions;
54
    }
55
56
    /**
57
     * Update item callback.
58
     *
59
     * @param               $eloquent
60
     * @param UpdateRequest $request
61
     *
62
     * @throws Exception
63
     *
64
     * @return string
65
     *
66
     * @internal param $repository
67
     */
68
    public function save($eloquent, UpdateRequest $request)
69
    {
70
        $saver = app('scaffold.module')->saver();
0 ignored issues
show
Bug introduced by
The method saver() does not exist on Illuminate\Foundation\Application. ( Ignorable by Annotation )

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

70
        $saver = app('scaffold.module')->/** @scrutinizer ignore-call */ saver();

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...
71
        $saver = new $saver($eloquent, $request);
72
73
        if (!$saver instanceof Saver) {
74
            throw new Exception('Saver must implement '.Saver::class.' contract');
75
        }
76
77
        return $saver->sync();
78
    }
79
80
    /**
81
     * Destroy an attachment.
82
     *
83
     * @param   $item
84
     * @param   $attachment
85
     *
86
     * @return bool
87
     */
88
    public function detachFile(Model $item, $attachment)
89
    {
90
        try {
91
            $item->$attachment = Attachment::NULL_ATTACHMENT;
92
            $item->save();
93
94
            return true;
95
        } catch (\Exception $e) {
96
            return false;
97
        }
98
    }
99
100
    /**
101
     * Destroy item callback.
102
     *
103
     * @param Model $item
104
     *
105
     * @throws \Exception
106
     *
107
     * @return string
108
     */
109
    public function delete(Model $item)
110
    {
111
        if (method_exists($item, 'trashed') && $item->trashed()) {
112
            return $item->forceDelete();
113
        }
114
115
        return $item->delete();
116
    }
117
118
    /**
119
     * Determine if the user is authorized to make this request.
120
     *
121
     * @param string $method
122
     * @param        $model
123
     * @param null $module
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $module is correct as it would always require null to be passed?
Loading history...
124
     *
125
     * @return bool
126
     */
127
    public function authorize($method, $model = null, $module = null)
128
    {
129
        $accessGate = Gate::forUser(auth('admin')->user());
130
        $module = $module ?: app('scaffold.module');
0 ignored issues
show
introduced by
$module is of type null, thus it always evaluated to false.
Loading history...
131
        $model = $model ?: $module->model();
0 ignored issues
show
Bug introduced by
The method model() does not exist on Illuminate\Foundation\Application. ( Ignorable by Annotation )

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

131
        $model = $model ?: $module->/** @scrutinizer ignore-call */ model();

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...
132
        $method = camel_case($method);
133
134
        if (($policy = $accessGate->getPolicyFor($model)) && method_exists($policy, $method)) {
135
            return $accessGate->allows($method, $model);
136
        }
137
138
        return true;
139
    }
140
}
141