UploadManyImages   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 101
Duplicated Lines 0 %

Importance

Changes 5
Bugs 0 Features 0
Metric Value
wmc 6
eloc 50
c 5
b 0
f 0
dl 0
loc 101
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A StepOneSelect() 0 10 1
A getRelationshipList() 0 12 3
A StepTwoAttach() 0 39 2
1
<?php
2
3
namespace Sunnysideup\PerfectCMSImagesUploader\Admin\UploadManyImages;
4
5
use Image;
6
use SilverStripe\Control\HTTPResponse;
7
use SilverStripe\Forms\DropdownField;
8
use SilverStripe\Forms\FieldList;
9
use SilverStripe\Forms\Form;
10
use SilverStripe\ORM\FieldType\DBHTMLText;
11
use Sunnysideup\PerfectCmsImages\Forms\PerfectCmsImagesUploadField;
0 ignored issues
show
Bug introduced by
The type Sunnysideup\PerfectCmsIm...ectCmsImagesUploadField 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...
12
13
class UploadManyImages
14
{
15
    protected $className = '';
16
17
    protected $relationshipName = '';
18
19
    private static $menu_priority = 3.3;
20
21
    private static $url_segment = 'upload-images';
22
23
    private static $allowed_actions = [
24
        'StepOneSelect',
25
        'StepTwoAttach',
26
    ];
27
28
    /**
29
     * SEE: https://github.com/sunnysideup/silverstripe-perfect-cms-images-uploader/blob/master/docs/en/INDEX.md
30
     * for an example ...
31
     *
32
     * @var array
33
     */
34
    private static $class_fields_matchers = [];
35
36
    /**
37
     * set the class and relationship name.
38
     *
39
     * @return Form
40
     */
41
    public function StepOneSelect()
42
    {
43
        return Form::create(
44
            $this,
45
            'EditForm',
46
            FieldList::create([
47
                DropdownField::create(
48
                    'ClassName',
49
                    'ObjectType',
50
                    $this->getRelationshipList()
51
                ),
52
            ])
53
        );
54
    }
55
56
    /**
57
     * @return Form
58
     */
59
    public function StepTwoAttach()
60
    {
61
        return Form::create(
62
            $this,
63
            'EditForm',
64
            FieldList::create([
65
                PerfectCmsImagesUploadField::create(
66
                    'AttachedFile',
67
                    DBHTMLText::create_field(
68
                        'HTMLFragment',
69
                        '<h2>Rapid image uploader</h2>' .
70
                        "<p>
71
                            Either select files or drag/drop files on to the box below.
72
                            The filename should start with the Code for the Items then follwed by '_' then anything else. For example;
73
                        </p>" .
74
                        '<blockquote>14184_test.jpg<br>14184_test2.jpg</blockquote>'
75
                    )
76
                )
77
                    ->setIsMultiUpload(true)
78
                    ->setAfterUpload(function (HTTPResponse $response) {
79
                        // Read data from the original response
80
                        $data = json_decode($response->getBody())[0];
81
82
                        // preg the id from the title of the file
83
                        $id = preg_split('[/._]', $data->title, PREG_SPLIT_OFFSET_CAPTURE);
84
                        $field = 'TBC';
0 ignored issues
show
Unused Code introduced by
The assignment to $field is dead and can be removed.
Loading history...
85
                        $field = 'TBC';
86
                        $object = $this->className::filter([$field => $id]);
87
                        // Find product from id
88
                        if ($object->count() === 1) {
89
                            // Attach image to found prodcut
90
                            $object->{$this->relationshipName}()->add(
91
                                Image::get_by_id($data->id)
92
                            );
93
                        }
94
                        // Return the original response (untouched)
95
                        return $response;
96
                    })
97
                    ->addExtraClass('panel--padded'),
98
            ])
99
        );
100
    }
101
102
    protected function getRelationshipList(): array
103
    {
104
        $listOfOptions = $this->Config()->get('class_fields_matchers');
0 ignored issues
show
Bug introduced by
The method Config() does not exist on Sunnysideup\PerfectCMSIm...Images\UploadManyImages. ( Ignorable by Annotation )

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

104
        $listOfOptions = $this->/** @scrutinizer ignore-call */ Config()->get('class_fields_matchers');

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...
105
        $options = [];
106
        foreach ($listOfOptions as $className => $classNameDetails) {
107
            $singleton = Injector::inst()->get($className);
0 ignored issues
show
Bug introduced by
The type Sunnysideup\PerfectCMSIm...loadManyImages\Injector 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...
108
            foreach ($classNameDetails['ImageRelationShips'] as $relationship) {
109
                $options[$className . '_' . $relationship . '_'] = $singleton->singularName() . ' - ' . $singleton->getFieldLabel($relationship);
110
            }
111
        }
112
113
        return $options;
114
    }
115
}
116