Scrutinizer GitHub App not installed

We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.

Install GitHub App

Passed
Pull Request — main (#5440)
by Cristian
29:02 queued 14:03
created

UploadersRepository::isValidUploadField()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 7
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 13
rs 10
1
<?php
2
3
namespace Backpack\CRUD\app\Library\Uploaders\Support;
4
5
use Backpack\CRUD\app\Library\Uploaders\Support\Interfaces\UploaderInterface;
6
use Illuminate\Support\Str;
7
8
final class UploadersRepository
9
{
10
    /**
11
     * The array of uploaders classes for field types.
12
     */
13
    private array $uploaderClasses;
14
15
    /**
16
     * Uploaders registered in a repeatable group.
17
     */
18
    private array $repeatableUploaders = [];
19
20
    /**
21
     * Uploaders that have already been handled (events registered) for each field/column instance.
22
     */
23
    private array $handledUploaders = [];
24
25
    public function __construct()
26
    {
27
        $this->uploaderClasses = config('backpack.crud.uploaders');
28
    }
29
30
    /**
31
     * Mark the given uploader as handled.
32
     */
33
    public function markAsHandled(string $objectName): void
34
    {
35
        if (! in_array($objectName, $this->handledUploaders)) {
36
            $this->handledUploaders[] = $objectName;
37
        }
38
    }
39
40
    /**
41
     * Check if the given uploader for field/column have been handled.
42
     */
43
    public function isUploadHandled(string $objectName): bool
44
    {
45
        return in_array($objectName, $this->handledUploaders);
46
    }
47
48
    /**
49
     * Check if there are uploads for the give object(field/column) type.
50
     */
51
    public function hasUploadFor(string $objectType, string $group): bool
52
    {
53
        return array_key_exists($objectType, $this->uploaderClasses[$group]);
54
    }
55
56
    /**
57
     * Return the uploader for the given object type.
58
     */
59
    public function getUploadFor(string $objectType, string $group): string
60
    {
61
        return $this->uploaderClasses[$group][$objectType];
62
    }
63
64
    /**
65
     * Register new uploaders or override existing ones.
66
     */
67
    public function addUploaderClasses(array $uploaders, string $group): void
68
    {
69
        $this->uploaderClasses[$group] = array_merge($this->getGroupUploadersClasses($group), $uploaders);
70
    }
71
72
    /**
73
     * Return the uploaders classes for the given group.
74
     */
75
    private function getGroupUploadersClasses(string $group): array
76
    {
77
        return $this->uploaderClasses[$group] ?? [];
78
    }
79
80
    /**
81
     * Register the specified uploader for the given upload name.
82
     */
83
    public function registerRepeatableUploader(string $uploadName, UploaderInterface $uploader): void
84
    {
85
        if (! array_key_exists($uploadName, $this->repeatableUploaders) || ! in_array($uploader, $this->repeatableUploaders[$uploadName])) {
86
            $this->repeatableUploaders[$uploadName][] = $uploader;
87
        }
88
    }
89
90
    /**
91
     * Check if there are uploaders registered for the given upload name.
92
     */
93
    public function hasRepeatableUploadersFor(string $uploadName): bool
94
    {
95
        return array_key_exists($uploadName, $this->repeatableUploaders);
96
    }
97
98
    /**
99
     * Get the repeatable uploaders for the given upload name.
100
     */
101
    public function getRepeatableUploadersFor(string $uploadName): array
102
    {
103
        return $this->repeatableUploaders[$uploadName] ?? [];
104
    }
105
106
    /**
107
     * Check if the specified upload is registered for the given repeatable uploads.
108
     */
109
    public function isUploadRegistered(string $uploadName, UploaderInterface $upload): bool
110
    {
111
        return $this->hasRepeatableUploadersFor($uploadName) && in_array($upload->getName(), $this->getRegisteredUploadNames($uploadName));
112
    }
113
114
    /**
115
     * Return the registered uploaders names for the given repeatable upload name.
116
     */
117
    public function getRegisteredUploadNames(string $uploadName): array
118
    {
119
        return array_map(function ($uploader) {
120
            return $uploader->getName();
121
        }, $this->getRepeatableUploadersFor($uploadName));
122
    }
123
124
    /**
125
     * Get the uploaders classes for the given group of uploaders.
126
     */
127
    public function getAjaxUploadTypes(string $group = 'withFiles'): array
128
    {
129
        $ajaxFieldTypes = [];
130
        foreach ($this->uploaderClasses[$group] as $fieldType => $uploader) {
131
            if (is_a($uploader, 'Backpack\Pro\Uploads\BackpackAjaxUploader', true)) {
132
                $ajaxFieldTypes[] = $fieldType;
133
            }
134
        }
135
136
        return $ajaxFieldTypes;
137
    }
138
139
    /**
140
     * Get an uploader instance for a given crud object.
141
     */
142
    public function getUploaderInstance(string $requestInputName, array $crudObject): UploaderInterface
143
    {
144
        if (! $this->isValidUploadField($requestInputName)) {
145
            abort(500, 'Invalid field for upload.');
146
        }
147
148
        if (strpos($requestInputName, '#') !== false) {
149
            $repeatableContainerName = Str::before($requestInputName, '#');
150
            $requestInputName = Str::after($requestInputName, '#');
151
            $uploaders = $this->getRepeatableUploadersFor($repeatableContainerName);
0 ignored issues
show
Unused Code introduced by
The assignment to $uploaders is dead and can be removed.
Loading history...
152
            //TODO: Implement the logic for repeatable uploaders
153
            dd('here');
154
        }
155
156
        if (! $uploadType = $this->getUploadCrudObjectMacroType($crudObject)) {
157
            abort(500, 'There is no uploader defined for the given field type.');
158
        }
159
160
        $uploaderConfiguration = $crudObject[$uploadType] ?? [];
161
        $uploaderConfiguration = ! is_array($uploaderConfiguration) ? [] : $uploaderConfiguration;
162
        $uploaderClass = $this->getUploadFor($crudObject['type'], $uploadType);
163
164
        return new $uploaderClass(['name' => $requestInputName], $uploaderConfiguration);
165
    }
166
167
    /**
168
     * Get the upload field macro type for the given object.
169
     */
170
    private function getUploadCrudObjectMacroType(array $crudObject): string
171
    {
172
        return isset($crudObject['withFiles']) ? 'withFiles' : ($crudObject['withMedia'] ? 'withMedia' : null);
0 ignored issues
show
Bug Best Practice introduced by
The expression return IssetNode ? 'with...'] ? 'withMedia' : null could return the type null which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
173
    }
174
175
    private function isValidUploadField($fieldName)
176
    {
177
        if (Str::contains($fieldName, '#')) {
178
            $container = Str::before($fieldName, '#');
179
            $fieldName = Str::after($fieldName, '#');
180
            $field = array_filter(CRUD::fields()[$container]['subfields'] ?? [], function ($item) use ($fieldName) {
0 ignored issues
show
Bug introduced by
The type Backpack\CRUD\app\Library\Uploaders\Support\CRUD 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...
181
                return $item['name'] === $fieldName && in_array($item['type'], $this->getAjaxFieldUploadTypes($fieldName));
0 ignored issues
show
Bug introduced by
The method getAjaxFieldUploadTypes() does not exist on Backpack\CRUD\app\Librar...ort\UploadersRepository. ( Ignorable by Annotation )

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

181
                return $item['name'] === $fieldName && in_array($item['type'], $this->/** @scrutinizer ignore-call */ getAjaxFieldUploadTypes($fieldName));

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...
182
            });
183
184
            return ! empty($field);
185
        }
186
187
        return isset(CRUD::fields()[$fieldName]) && in_array(CRUD::fields()[$fieldName]['type'], $this->getAjaxFieldUploadTypes($fieldName));
188
    }
189
}
190