Passed
Pull Request — 2.x (#899)
by Antonio Carlos
07:23
created

FileLibraryController::getRequestFilters()   A

Complexity

Conditions 5
Paths 8

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 5.0488

Importance

Changes 0
Metric Value
cc 5
eloc 7
nc 8
nop 0
dl 0
loc 15
ccs 7
cts 8
cp 0.875
crap 5.0488
rs 9.6111
c 0
b 0
f 0
1
<?php
2
3
namespace A17\Twill\Http\Controllers\Admin;
4
5
use Illuminate\Http\Request;
6
use Illuminate\Http\JsonResponse;
7
use Illuminate\Routing\UrlGenerator;
8
use Illuminate\Routing\ResponseFactory;
9
use Illuminate\Config\Repository as Config;
10
use A17\Twill\Http\Requests\Admin\FileRequest;
11
use Illuminate\Contracts\Foundation\Application;
12
use Illuminate\Contracts\Container\BindingResolutionException;
13
14
class FileLibraryController extends LibraryController
15
{
16
    /**
17
     * @var string
18
     */
19
    protected $moduleName = 'files';
20
21
    /**
22
     * @var Illuminate\Routing\UrlGenerator
0 ignored issues
show
Bug introduced by
The type A17\Twill\Http\Controlle...te\Routing\UrlGenerator was not found. Did you mean Illuminate\Routing\UrlGenerator? If so, make sure to prefix the type with \.
Loading history...
23
     */
24
    protected $urlGenerator;
25
26
    public function __construct(
27
        Application $app,
28
        Request $request,
29
        UrlGenerator $urlGenerator,
30
        ResponseFactory $responseFactory,
31
        Config $config
32
    ) {
33
        parent::__construct($app, $request);
34
        $this->urlGenerator = $urlGenerator;
0 ignored issues
show
Documentation Bug introduced by
It seems like $urlGenerator of type Illuminate\Routing\UrlGenerator is incompatible with the declared type A17\Twill\Http\Controlle...te\Routing\UrlGenerator of property $urlGenerator.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
35
        $this->responseFactory = $responseFactory;
0 ignored issues
show
Documentation Bug introduced by
It seems like $responseFactory of type Illuminate\Routing\ResponseFactory is incompatible with the declared type A17\Twill\Http\Controlle...Routing\ResponseFactory of property $responseFactory.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
36
        $this->config = $config;
0 ignored issues
show
Documentation Bug introduced by
It seems like $config of type Illuminate\Config\Repository is incompatible with the declared type A17\Twill\Http\Controlle...inate\Config\Repository of property $config.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
37
38
        $this->removeMiddleware('can:edit');
39
        $this->middleware('can:edit', ['only' => ['signS3Upload', 'signAzureUpload', 'tags', 'store', 'singleUpdate', 'bulkUpdate']]);
40
        $this->endpointType = $this->config->get('twill.file_library.endpoint_type');
41
    }
42
43
    /**
44
     * @param array $prependScope
45
     * @return array
46
     */
47
    public function getIndexData($prependScope = [])
48
    {
49
        $scopes = $this->filterScope($prependScope);
50
        $items = $this->getIndexItems($scopes);
51
52
        return [
53
            'items' => $items->map(function ($item) {
54
                return $this->buildFile($item);
55
            })->toArray(),
56
            'maxPage' => $items->lastPage(),
57
            'total' => $items->total(),
58
            'tags' => $this->repository->getTagsList(),
0 ignored issues
show
Bug introduced by
The method getTagsList() does not exist on A17\Twill\Repositories\ModuleRepository. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

58
            'tags' => $this->repository->/** @scrutinizer ignore-call */ getTagsList(),
Loading history...
59
        ];
60
    }
61
62
    /**
63
     * @param \A17\Twill\Models\File $item
64
     * @return array
65
     */
66
    private function buildFile($item)
67
    {
68
        return $item->toCmsArray() + [
69
            'tags' => $item->tags->map(function ($tag) {
0 ignored issues
show
Bug Best Practice introduced by
The property tags does not exist on A17\Twill\Models\File. Since you implemented __get, consider adding a @property annotation.
Loading history...
Bug introduced by
The method map() does not exist on null. ( Ignorable by Annotation )

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

69
            'tags' => $item->tags->/** @scrutinizer ignore-call */ map(function ($tag) {

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...
70 4
                return $tag->name;
71
            }),
72
            'deleteUrl' => $item->canDeleteSafely() ? moduleRoute($this->moduleName, $this->routePrefix, 'destroy', $item->id) : null,
0 ignored issues
show
Bug introduced by
$item->id of type integer is incompatible with the type array expected by parameter $parameters of moduleRoute(). ( Ignorable by Annotation )

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

72
            'deleteUrl' => $item->canDeleteSafely() ? moduleRoute($this->moduleName, $this->routePrefix, 'destroy', /** @scrutinizer ignore-type */ $item->id) : null,
Loading history...
73
            'updateUrl' => $this->urlGenerator->route('admin.file-library.files.single-update'),
74
            'updateBulkUrl' => $this->urlGenerator->route('admin.file-library.files.bulk-update'),
75
            'deleteBulkUrl' => $this->urlGenerator->route('admin.file-library.files.bulk-delete'),
76
        ];
77 4
    }
78 4
79 4
    /**
80 4
     * @param int|null $parentModuleId
81
     * @return JsonResponse
82 4
     * @throws BindingResolutionException
83 4
     */
84 4
    public function store($parentModuleId = null)
85 4
    {
86
        $request = $this->app->make(FileRequest::class);
87
88
        if ($this->endpointType === 'local') {
89
            $file = $this->storeFile($request);
90
        } else {
91 1
            $file = $this->storeReference($request);
92
        }
93 1
94 1
        return $this->responseFactory->json(['media' => $this->buildFile($file), 'success' => true], 200);
95
    }
96
97 1
    /**
98
     * @param Request $request
99
     * @return \A17\Twill\Models\File
100
     */
101
    public function storeReference($request)
102
    {
103
        $fields = [
104 1
            'uuid' => $request->input('key') ?? $request->input('blob'),
105
            'filename' => $request->input('name'),
106 1
        ];
107 1
108
        if ($this->shouldReplaceFile($id = $request->input('media_to_replace_id'))) {
0 ignored issues
show
Bug introduced by
The method shouldReplaceFile() does not exist on A17\Twill\Http\Controlle...n\FileLibraryController. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

108
        if ($this->/** @scrutinizer ignore-call */ shouldReplaceFile($id = $request->input('media_to_replace_id'))) {
Loading history...
109
            $file = $this->repository->whereId($id)->first();
110 1
            $this->repository->afterDelete($file);
111
            $file->update($fields);
112 1
            return $file->fresh();
113 1
        } else {
114 1
            return $this->repository->create($fields);
115 1
        }
116
    }
117
118
    /**
119
     * @return JsonResponse
120
     */
121
    public function singleUpdate()
122
    {
123 3
        $this->repository->update(
124
            $this->request->input('id'),
125 3
            $this->request->only('tags')
126 3
        );
127 1
128 3
        return $this->responseFactory->json([], 200);
129 3
    }
130 3
131 3
    /**
132 3
     * @return JsonResponse
133
     */
134
    public function bulkUpdate()
135
    {
136
        $ids = explode(',', $this->request->input('ids'));
137
138
        $previousCommonTags = $this->repository->getTags(null, $ids);
0 ignored issues
show
Bug introduced by
The method getTags() does not exist on A17\Twill\Repositories\ModuleRepository. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

138
        /** @scrutinizer ignore-call */ 
139
        $previousCommonTags = $this->repository->getTags(null, $ids);
Loading history...
139 2
        $newTags = array_filter(explode(',', $this->request->input('tags')));
140
141 2
        foreach ($ids as $id) {
142 1
            $this->repository->update($id, ['bulk_tags' => $newTags, 'previous_common_tags' => $previousCommonTags]);
143
        }
144
145 2
        $scopes = $this->filterScope(['id' => $ids]);
146 1
        $items = $this->getIndexItems($scopes);
147
148
        return $this->responseFactory->json([
149 2
            'items' => $items->map(function ($item) {
150
                return $this->buildFile($item);
151
            })->toArray(),
152
            'tags' => $this->repository->getTagsList(),
153 2
        ], 200);
154
    }
155
}
156