CrudActions::save()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 5
ccs 0
cts 0
cp 0
crap 2
rs 10
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;
0 ignored issues
show
Bug introduced by
The type Illuminate\Database\Eloquent\Model 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...
7
use Illuminate\Support\Facades\Gate;
8
use Illuminate\Support\Str;
9
use Terranet\Administrator\Actions\RemoveSelected;
10
use Terranet\Administrator\Actions\SaveOrder;
11
use Terranet\Administrator\AdminRequest;
12
use Terranet\Administrator\Contracts\Services\CrudActions as CrudActionsContract;
13
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...
14
use Terranet\Administrator\Exception;
15
use Terranet\Administrator\Requests\UpdateRequest;
16
use Terranet\Administrator\Scaffolding;
17
use Terranet\Administrator\Traits\ExportsCollection;
18
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...
19
20
class CrudActions implements CrudActionsContract
21
{
22
    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...
23
24
    /** @var Scaffolding */
25
    protected $module;
26
27
    /** @var AdminRequest */
28
    protected $request;
29
30
    /**
31
     * CrudActions constructor.
32
     *
33
     * @param $module
34
     * @param  AdminRequest  $request
35
     */
36
    public function __construct($module, AdminRequest $request)
37
    {
38
        $this->module = $module;
39
        $this->request = $request;
40
    }
41
42
    /**
43
     * Default custom list of actions.
44
     *
45
     * @return array
46
     */
47
    public function actions()
48
    {
49
        return [];
50
    }
51
52
    /**
53
     * Default list of batch actions.
54
     *
55
     * @return array
56
     */
57
    public function batchActions()
58
    {
59
        $actions = [RemoveSelected::class];
60
61
        if ($this->module->model() instanceof Rankable) {
62
            array_push($actions, SaveOrder::class);
63
        }
64
65
        return $actions;
66
    }
67
68
    /**
69
     * Update item callback.
70
     *
71
     * @param               $eloquent
72
     * @param UpdateRequest $request
73
     *
74
     * @throws Exception
75
     *
76
     * @return string
77
     *
78
     * @internal param $repository
79
     */
80
    public function save($eloquent, UpdateRequest $request)
81
    {
82
        $saver = $this->module->saver($eloquent, $request);
83
84
        return $saver->sync();
85
    }
86
87
    /**
88
     * Destroy an attachment.
89
     *
90
     * @param   $item
91
     * @param   $attachment
92
     *
93
     * @return bool
94
     */
95
    public function detachFile(Model $item, $attachment)
96
    {
97
        try {
98
            $item->$attachment = Attachment::NULL_ATTACHMENT;
99
            $item->save();
100
101
            return true;
102
        } catch (\Exception $e) {
103
            return false;
104
        }
105
    }
106
107
    /**
108
     * Destroy item callback.
109
     *
110
     * @param Model $item
111
     *
112
     * @throws \Exception
113
     *
114
     * @return string
115
     */
116
    public function delete(Model $item)
117
    {
118
        if (method_exists($item, 'trashed') && $item->trashed()) {
119
            return $item->forceDelete();
120
        }
121
122
        return $item->delete();
123
    }
124
125
    /**
126
     * Determine if the user is authorized to make this request.
127
     *
128
     * @param string $method
129
     * @param        $model
130
     * @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...
131
     *
132
     * @return bool
133
     */
134
    public function authorize($method, $model = null, $module = null)
135
    {
136
        $accessGate = Gate::forUser($user = $this->request->user());
137
        $module = $module ?: $this->module;
0 ignored issues
show
introduced by
$module is of type null, thus it always evaluated to false.
Loading history...
138
        $model = $model ?: $module->model();
139
        $method = Str::camel($method);
140
141
        if (($policy = $accessGate->getPolicyFor($model)) && method_exists($policy, $method)) {
142
            return $accessGate->allows($method, $model);
143
        }
144
145
        return true;
146
    }
147
}
148