ImageUploadField::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 22
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 11
c 2
b 0
f 0
dl 0
loc 22
rs 9.9
cc 2
nc 2
nop 3
1
<?php
2
3
namespace Dynamic\ImageUpload;
4
5
use SilverStripe\AssetAdmin\Forms\UploadField;
0 ignored issues
show
Bug introduced by
The type SilverStripe\AssetAdmin\Forms\UploadField 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...
6
use SilverStripe\Core\Convert;
7
use SilverStripe\Core\Injector\Injector;
8
use SilverStripe\ORM\SS_List;
9
10
/**
11
 * Class ImageUploadField
12
 * @package Dynamic\SilverStripe\ImageUpload
13
 */
14
class ImageUploadField extends UploadField
15
{
16
    /**
17
     * @var int
18
     */
19
    private static $max_upload = 1024000;
0 ignored issues
show
introduced by
The private property $max_upload is not used, and could be removed.
Loading history...
20
21
    /**
22
     * @var string
23
     */
24
    private static $category = 'image/supported';
0 ignored issues
show
introduced by
The private property $category is not used, and could be removed.
Loading history...
25
26
    /**
27
     * @var array
28
     */
29
    private static $allowed_categories = [
0 ignored issues
show
introduced by
The private property $allowed_categories is not used, and could be removed.
Loading history...
30
        'image/supported',
31
        'image',
32
    ];
33
34
    /**
35
     * ImageUploadField constructor.
36
     * @param string $name
37
     * @param null $title
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $title is correct as it would always require null to be passed?
Loading history...
38
     * @param SS_List|null $items
39
     */
40
    public function __construct($name, $title = null, SS_List $items = null)
41
    {
42
        parent::__construct($name, $title);
43
44
        // set the max upload size
45
        $maxUpload = $this->config()->get('max_upload');
46
        $maxPost = Convert::memstring2bytes(ini_get('post_max_size'));
47
        $this->getValidator()->setAllowedMaxFileSize(min($maxUpload, $maxPost));
48
49
        // other image specific options
50
        $this->setAllowedMaxFileNumber(1);
51
52
        $category = static::config()->get('category');
53
54
        if (!in_array($category, $this->config()->get('allowed_categories'))) {
55
            $msg = "{$category} not listed in ImageUploadField::allowed_categories. Defaulting to \"image/supported\"";
56
            user_error($msg);
57
58
            $category = 'image/supported';
59
        }
60
61
        $this->setAllowedFileCategories($category);
62
    }
63
}
64