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
Push — add-method-to-get-ajax-uploade... ( 29c841...61176a )
by Pedro
14:14
created

UploadersRepository::hasRepeatableUploadersFor()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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

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