VersionedFolderExtension::onBeforeDelete()   B
last analyzed

Complexity

Conditions 7
Paths 11

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 23
rs 8.6186
c 0
b 0
f 0
cc 7
nc 11
nop 0
1
<?php
2
/**
3
 * @package silverstripe-versionedfiles
4
 */
5
class VersionedFolderExtension extends DataExtension
6
{
7
    public function onBeforeDelete()
8
    {
9
        // A workaround for that fact that Folder::onBeforeDelete() only removes
10
        // the folder if only a _resampled dir exists. If the folder is empty
11
        // and only has _resampled and _versions directories then delete it.
12
        if ($this->owner->Filename && is_dir($this->owner->getFullPath())) {
0 ignored issues
show
Bug introduced by
The property Filename does not seem to exist in SS_Object.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
13
            $items       = scandir($this->owner->getFullPath());
14
            $versionsDir = FileVersion::VERSION_FOLDER;
15
16
            // Remove . and .. items.
17
            array_shift($items);
18
            array_shift($items);
19
20
            $delete = (
21
                count($items) == 1 && $items[0] == $versionsDir
22
                || count($items) == 2 && $items == array('_resampled', $versionsDir)
23
            );
24
25
            if ($delete) {
26
                Filesystem::removeFolder($this->owner->getFullPath());
27
            }
28
        }
29
    }
30
}
31