PerfectCmsImagesUploadField   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 121
Duplicated Lines 0 %

Importance

Changes 11
Bugs 0 Features 0
Metric Value
eloc 42
c 11
b 0
f 0
dl 0
loc 121
rs 10
wmc 8

6 Methods

Rating   Name   Duplication   Size   Complexity  
A setDescription() 0 7 1
A __construct() 0 18 2
A setAfterUpload() 0 5 1
A upload() 0 10 2
A selectFormattingStandard() 0 20 1
A setPerfectFolderName() 0 14 1
1
<?php
2
3
namespace Sunnysideup\PerfectCmsImages\Forms;
4
5
use SilverStripe\AssetAdmin\Forms\UploadField;
6
use SilverStripe\Assets\Folder;
7
use SilverStripe\Control\HTTPRequest;
8
use SilverStripe\Control\HTTPResponse;
9
use SilverStripe\ORM\FieldType\DBField;
10
use SilverStripe\ORM\SS_List;
11
use Sunnysideup\PerfectCmsImages\Api\PerfectCMSImages;
12
use Sunnysideup\PerfectCmsImages\Filesystem\PerfectCmsImageValidator;
13
14
/**
15
 * image-friendly upload field.
16
 *
17
 * Usage:
18
 *     $field = PerfectCmsImagesUploadFielde::create(
19
 *         "ImageField",
20
 *         "Add Image",
21
 *        );
22
 */
23
class PerfectCmsImagesUploadField extends UploadField
24
{
25
    private static $max_size_in_kilobytes = 2048;
26
27
    private static $folder_prefix = '';
28
29
    /**
30
     * @config
31
     *
32
     * @var array
33
     */
34
    private static $allowed_actions = [
35
        'upload',
36
    ];
37
38
    private $afterUpload;
39
40
    /**
41
     * @param string       $name            the internal field name, passed to forms
42
     * @param null|string  $title           the field label
43
     * @param null|SS_List $items           If no items are defined, the field will try to auto-detect an existing relation
44
     * @param null|string  $alternativeName - name used for formatting
45
     */
46
    public function __construct(
47
        string $name,
48
        ?string $title = null,
49
        ?SS_List $items = null,
50
        ?string $alternativeName = null
51
    ) {
52
        parent::__construct(
53
            $name,
54
            $title,
55
            $items
56
        );
57
        $perfectCMSImageValidator = new PerfectCMSImageValidator();
58
        $this->setValidator($perfectCMSImageValidator);
59
        $finalName = $name;
60
        if (null !== $alternativeName) {
61
            $finalName = $alternativeName;
62
        }
63
        $this->selectFormattingStandard($finalName);
64
    }
65
66
    public function setDescription($string): self
67
    {
68
        parent::setDescription(
69
            DBField::create_field('HTMLText', $string . '<br />' . $this->RightTitle())
70
        );
71
        //important!
72
        return $this;
73
    }
74
75
    /**
76
     * @param string $name Formatting Standard
77
     */
78
    public function selectFormattingStandard(string $name): self
79
    {
80
        //folder
81
        $this->setPerfectFolderName($name);
82
83
        // description
84
        $this->setDescription(PerfectCMSImages::get_description_for_cms($name));
85
86
        // standard stuff
87
        $this->setAllowedFileCategories('image');
88
        $alreadyAllowed = $this->getAllowedExtensions();
89
        $this->setAllowedExtensions($alreadyAllowed + ['svg']);
90
        //keep the size reasonable
91
        $maxSizeInKilobytes = PerfectCMSImages::max_size_in_kilobytes($name);
92
        $this->getValidator()->setAllowedMaxFileSize(1 * 1024 * $maxSizeInKilobytes);
93
94
        //make sure the validator knows about the name.
95
        $this->getValidator()->setFieldName($name);
0 ignored issues
show
Bug introduced by
The method setFieldName() does not exist on SilverStripe\Assets\Upload_Validator. It seems like you code against a sub-type of SilverStripe\Assets\Upload_Validator such as Sunnysideup\PerfectCmsIm...erfectCmsImageValidator. ( Ignorable by Annotation )

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

95
        $this->getValidator()->/** @scrutinizer ignore-call */ setFieldName($name);
Loading history...
96
97
        return $this;
98
    }
99
100
    /**
101
     * Creates a single file based on a form-urlencoded upload.
102
     * Allows for hooking AfterUpload.
103
     *
104
     * @return HTTPResponse
105
     */
106
    public function upload(HTTPRequest $request)
107
    {
108
        $response = parent::upload($request);
109
110
        // If afterUpload is a function ..
111
        return is_callable($this->afterUpload) ?
112
            //  .. then return the results from that ..
113
            ($this->afterUpload)($response) :
114
            //  .. else return the original $response
115
            $response;
116
    }
117
118
    /**
119
     * Add an anonymous functions to run after upload completes.
120
     *
121
     * @param callable $func
122
     */
123
    public function setAfterUpload($func): self
124
    {
125
        $this->afterUpload = $func;
126
127
        return $this;
128
    }
129
130
    protected function setPerfectFolderName(string $name)
131
    {
132
        $folderPrefix = $this->Config()->get('folder_prefix');
133
134
        $folderName = (string) trim($this->folderName);
0 ignored issues
show
Unused Code introduced by
The assignment to $folderName is dead and can be removed.
Loading history...
135
        //folder related stuff ...
136
        $folderName = (string) PerfectCMSImages::get_folder($name);
137
        $folderName = implode(
138
            '/',
139
            array_filter([$folderPrefix, $folderName])
140
        );
141
        Folder::find_or_make($folderName);
142
        //set folder
143
        $this->setFolderName($folderName);
144
    }
145
}
146