Completed
Push — master ( 67ead9...61a703 )
by Daniel
12s
created

EditableFileField   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 150
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 11

Test Coverage

Coverage 59.31%

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 11
dl 0
loc 150
ccs 35
cts 59
cp 0.5931
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getValueFromData() 0 4 1
A getSubmittedFormField() 0 4 1
A get_php_max_file_size() 0 6 1
A getPHPMaxFileSizeMB() 0 4 1
B getCMSFields() 0 27 1
A validate() 0 11 2
B getFormField() 0 34 4
A migrateSettings() 0 10 2
1
<?php
2
3
/**
4
 * Allows a user to add a field that can be used to upload a file.
5
 *
6
 * @package userforms
7
 */
8
class EditableFileField extends EditableFormField
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
9
{
10
11
    private static $singular_name = 'File Upload Field';
0 ignored issues
show
Unused Code introduced by
The property $singular_name is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
12
13
    private static $plural_names = 'File Fields';
0 ignored issues
show
Unused Code introduced by
The property $plural_names is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
14
15
    private static $db = array(
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $db is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
16
        'MaxFileSizeMB' => 'Float',
17
    );
18
19
    private static $has_one = array(
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $has_one is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
20
        'Folder' => 'Folder' // From CustomFields
21
    );
22
23
    /**
24
     * Further limit uploadable file extensions in addition to the restrictions
25
     * imposed by the File.allowed_extensions global configuration.
26
     * @config
27
     */
28
    private static $allowed_extensions_blacklist = array(
0 ignored issues
show
Unused Code introduced by
The property $allowed_extensions_blacklist is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
29
        'htm', 'html', 'xhtml', 'swf', 'xml'
30
    );
31
32
    /**
33
     * @return FieldList
34
     */
35
    public function getCMSFields()
36
    {
37
        $fields = parent::getCMSFields();
38
39
        $fields->addFieldToTab(
40
            'Root.Main',
41
            TreeDropdownField::create(
42
                'FolderID',
43
                _t('EditableUploadField.SELECTUPLOADFOLDER', 'Select upload folder'),
44
                'Folder'
45
            )
46
        );
47
48
        $fields->addFieldToTab("Root.Main", new LiteralField("FileUploadWarning",
49
            "<p class=\"message notice\">" . _t("UserDefinedForm.FileUploadWarning",
50
                "Files uploaded through this field could be publicly accessible if the exact URL is known")
51
            . "</p>"), "Type");
52
53
        $fields->addFieldToTab(
54
            'Root.Main',
55
            NumericField::create('MaxFileSizeMB')
56
                ->setTitle('Max File Size MB')
57
                ->setDescription("Note: Maximum php allowed size is {$this->getPHPMaxFileSizeMB()} MB")
58
        );
59
60
        return $fields;
61
    }
62
63
    /**
64
     * @return ValidationResult
65
     */
66 4
    public function validate()
67
    {
68 4
        $result = parent::validate();
69
70 4
        $max = static::get_php_max_file_size();
71 4
        if ($this->MaxFileSizeMB * 1024 > $max) {
0 ignored issues
show
Documentation introduced by
The property MaxFileSizeMB does not exist on object<EditableFileField>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
72 1
            $result->error("Your max file size limit can't be larger than the server's limit of {$this->getPHPMaxFileSizeMB()}.");
73
        }
74
75 4
        return $result;
76
    }
77
78 4
    public function getFormField()
79
    {
80 4
        $field = FileField::create($this->Name, $this->EscapedTitle)
0 ignored issues
show
Documentation introduced by
The property EscapedTitle does not exist on object<EditableFileField>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
81 4
            ->setFieldHolderTemplate('UserFormsField_holder')
82 4
            ->setTemplate('UserFormsFileField');
83
84 4
        $field->setFieldHolderTemplate('UserFormsField_holder')
85 4
            ->setTemplate('UserFormsFileField');
86
87 4
        $field->getValidator()->setAllowedExtensions(
88 4
            array_diff(
89
            // filter out '' since this would be a regex problem on JS end
90 4
                array_filter(Config::inst()->get('File', 'allowed_extensions')),
91 4
                $this->config()->allowed_extensions_blacklist
92
            )
93
        );
94
95 4
        if ($this->MaxFileSizeMB > 0) {
0 ignored issues
show
Documentation introduced by
The property MaxFileSizeMB does not exist on object<EditableFileField>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
96 1
            $field->getValidator()->setAllowedMaxFileSize($this->MaxFileSizeMB * 1024 * 1024);
0 ignored issues
show
Documentation introduced by
The property MaxFileSizeMB does not exist on object<EditableFileField>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
97
        } else {
98 3
            $field->getValidator()->setAllowedMaxFileSize(static::get_php_max_file_size());
99
        }
100
101 4
        $folder = $this->Folder();
0 ignored issues
show
Documentation Bug introduced by
The method Folder does not exist on object<EditableFileField>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
102 4
        if ($folder && $folder->exists()) {
103
            $field->setFolderName(
104
                preg_replace("/^assets\//", "", $folder->Filename)
105
            );
106
        }
107
108 4
        $this->doUpdateFormField($field);
109
110 4
        return $field;
111
    }
112
113
114
    /**
115
     * Return the value for the database, link to the file is stored as a
116
     * relation so value for the field can be null.
117
     *
118
     * @return string
119
     */
120
    public function getValueFromData()
121
    {
122
        return null;
123
    }
124
125
    public function getSubmittedFormField()
126
    {
127
        return new SubmittedFileField();
128
    }
129
130
131 2
    public function migrateSettings($data)
132
    {
133
        // Migrate 'Folder' setting to 'FolderID'
134 2
        if (isset($data['Folder'])) {
135 2
            $this->FolderID = $data['Folder'];
0 ignored issues
show
Documentation introduced by
The property FolderID does not exist on object<EditableFileField>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
136 2
            unset($data['Folder']);
137
        }
138
139 2
        parent::migrateSettings($data);
140 2
    }
141
142
    /**
143
     * @return float
144
     */
145 7
    public static function get_php_max_file_size()
146
    {
147 7
        $maxUpload = File::ini2bytes(ini_get('upload_max_filesize'));
148 7
        $maxPost = File::ini2bytes(ini_get('post_max_size'));
149 7
        return min($maxUpload, $maxPost);
150
    }
151
152 1
    public function getPHPMaxFileSizeMB()
153
    {
154 1
        return round(static::get_php_max_file_size() / 1024.0, 1);
155
    }
156
157
}
158