Product   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 175
Duplicated Lines 0 %

Importance

Changes 4
Bugs 1 Features 0
Metric Value
wmc 12
eloc 66
c 4
b 1
f 0
dl 0
loc 175
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A setHasImages() 0 4 1
A setImage() 0 6 2
A getHasImages() 0 6 2
A getThumbnail() 0 9 3
A getCMSFields() 0 43 2
A getImage() 0 6 2
1
<?php
2
3
namespace Dynamic\Products\Page;
4
5
use Bummzack\SortableFile\Forms\SortableUploadField;
6
use Dynamic\Products\Model\Brochure;
7
use SilverStripe\Assets\File;
8
use SilverStripe\Assets\Image;
9
use SilverStripe\Forms\FieldList;
10
use SilverStripe\Forms\GridField\GridField;
11
use SilverStripe\Forms\GridField\GridFieldAddExistingAutocompleter;
12
use SilverStripe\Forms\GridField\GridFieldConfig_RecordEditor;
13
use SilverStripe\Forms\TextField;
14
use Symbiote\GridFieldExtensions\GridFieldAddExistingSearchButton;
15
use Symbiote\GridFieldExtensions\GridFieldOrderableRows;
16
17
class Product extends \Page
18
{
19
    /**
20
     * @var
21
     */
22
    private $image;
23
24
    /**
25
     * @var
26
     */
27
    private $has_images;
28
29
    /**
30
     * @var string
31
     */
32
    private static $table_name = 'Product';
0 ignored issues
show
introduced by
The private property $table_name is not used, and could be removed.
Loading history...
33
34
    /**
35
     * @var array
36
     */
37
    private static $db = [
0 ignored issues
show
introduced by
The private property $db is not used, and could be removed.
Loading history...
38
        'SKU' => 'Varchar(100)',
39
    ];
40
41
    /**
42
     * @var array
43
     */
44
    private static $many_many = [
0 ignored issues
show
introduced by
The private property $many_many is not used, and could be removed.
Loading history...
45
        'Images' => File::class,
46
        'Brochures' => Brochure::class,
47
    ];
48
49
    /**
50
     * @var array
51
     */
52
    private static $many_many_extraFields = [
0 ignored issues
show
introduced by
The private property $many_many_extraFields is not used, and could be removed.
Loading history...
53
        'Images' => [
54
            'SortOrder' => 'Int',
55
        ],
56
        'Brochures' => [
57
            'SortOrder' => 'Int',
58
        ],
59
    ];
60
61
    /**
62
     *
63
     */
64
    private static $owns = [
0 ignored issues
show
introduced by
The private property $owns is not used, and could be removed.
Loading history...
65
        'Images',
66
    ];
67
68
    /**
69
     * The relation name was established before requests for videos.
70
     * The relation has subsequently been updated from Image::class to File::class
71
     * to allow for additional file types such as mp4
72
     *
73
     * @var array
74
     */
75
    private static $allowed_images_extensions = [
0 ignored issues
show
introduced by
The private property $allowed_images_extensions is not used, and could be removed.
Loading history...
76
        'gif',
77
        'jpeg',
78
        'jpg',
79
        'png',
80
        'bmp',
81
        'ico',
82
        'mp4',
83
    ];
84
85
    /**
86
     * @var array
87
     */
88
    private static $allowed_children = [];
0 ignored issues
show
introduced by
The private property $allowed_children is not used, and could be removed.
Loading history...
89
90
    /**
91
     * @return \SilverStripe\Forms\FieldList
92
     */
93
    public function getCMSFields()
94
    {
95
        $this->beforeUpdateCMSFields(function (FieldList $fields) {
96
            $fields->insertBefore(
97
                'Content',
98
                TextField::create('SKU', 'Product SKU')
99
            );
100
101
            // Images tab
102
            $images = SortableUploadField::create('Images')
103
                ->setSortColumn('SortOrder')
104
                ->setIsMultiUpload(true)
105
                ->setAllowedExtensions($this->config()->get('allowed_images_extensions'))
106
                ->setFolderName('Uploads/Products/Images');
107
108
            $fields->addFieldsToTab('Root.Images', [
109
                $images,
110
            ]);
111
112
            if ($this->ID) {
113
                // Brochures
114
                $config = GridFieldConfig_RecordEditor::create();
115
                $config->addComponents([
116
                    new GridFieldOrderableRows('SortOrder'),
117
                    new GridFieldAddExistingSearchButton(),
118
                ])
119
                    ->removeComponentsByType([
120
                        GridFieldAddExistingAutocompleter::class,
121
                    ]);
122
123
                $brochures = GridField::create(
124
                    'Brochures',
125
                    'Brochures',
126
                    $this->Brochures()->sort('SortOrder'),
0 ignored issues
show
Bug introduced by
The method Brochures() does not exist on Dynamic\Products\Page\Product. 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

126
                    $this->/** @scrutinizer ignore-call */ 
127
                           Brochures()->sort('SortOrder'),
Loading history...
127
                    $config
128
                );
129
                $fields->addFieldsToTab('Root.Files.Brochures', [
130
                    $brochures,
131
                ]);
132
            }
133
        });
134
135
        return parent::getCMSFields();
136
    }
137
138
    /**
139
     * @return bool
140
     */
141
    public function setImage()
142
    {
143
        if ($this->getHasImages()) {
144
            $this->image = $this->Images()->sort('SortOrder')->first();
0 ignored issues
show
Bug introduced by
The method Images() does not exist on Dynamic\Products\Page\Product. 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

144
            $this->image = $this->/** @scrutinizer ignore-call */ Images()->sort('SortOrder')->first();
Loading history...
145
        }
146
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type Dynamic\Products\Page\Product which is incompatible with the documented return type boolean.
Loading history...
147
    }
148
149
    /**
150
     * @return mixed
151
     */
152
    public function getImage()
153
    {
154
        if (!$this->image) {
155
            $this->setImage();
156
        }
157
        return $this->image;
158
    }
159
160
    /**
161
     * @return mixed
162
     */
163
    public function getThumbnail()
164
    {
165
        if ($image = $this->getImage()) {
166
            if ($thumb = Image::get()->byID($image->ID)) {
167
                return $thumb->CMSThumbnail();
168
            }
169
        }
170
171
        return false;
172
    }
173
174
    /**
175
     * @return $this
176
     */
177
    public function setHasImages()
178
    {
179
        $this->has_images = $this->Images()->exists();
180
        return $this;
181
    }
182
183
    /**
184
     * @return mixed
185
     */
186
    public function getHasImages()
187
    {
188
        if (!$this->has_images) {
189
            $this->setHasImages();
190
        }
191
        return $this->has_images;
192
    }
193
}
194