Completed
Push — master ( 3b23c0...1ceb95 )
by Damian
9s
created

AssetAdminFile::humanizedChanges()   C

Complexity

Conditions 10
Paths 27

Size

Total Lines 47
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 47
rs 5.1578
cc 10
eloc 29
nc 27
nop 2

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace SilverStripe\AssetAdmin\Controller;
4
5
use SilverStripe\Assets\File;
6
use SilverStripe\Control\Director;
7
use SilverStripe\ORM\DataExtension;
8
use SilverStripe\ORM\Versioning\Versioned;
9
use SilverStripe\ORM\Versioning\DataDifferencer;
10
11
/**
12
 * Update File dataobjects to be editable in this asset admin
13
 *
14
 * @property File $owner
15
 */
16
class AssetAdminFile extends DataExtension
17
{
18
    /**
19
     * Max width for inserted images
20
     *
21
     * @config
22
     * @var int
23
     */
24
    private static $insert_width = 600;
0 ignored issues
show
Unused Code introduced by
The property $insert_width 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...
25
26
    /**
27
     * Max height for inserted images
28
     *
29
     * @config
30
     * @var int
31
     */
32
    private static $insert_height = 400;
0 ignored issues
show
Unused Code introduced by
The property $insert_height 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...
33
34
    public function updateCMSEditLink(&$link)
35
    {
36
        // Update edit link for this file to point to the new asset admin
37
        $controller = AssetAdmin::singleton();
38
        $link = Director::absoluteURL($controller->getFileEditLink($this->owner));
39
    }
40
41
    /**
42
     * Calculate width to insert into html area
43
     *
44
     * @return int|null
45
     */
46
    public function getInsertWidth() {
47
        $size = $this->getInsertDimensions();
48
        return $size ? $size['width'] : null;
49
    }
50
51
    /**
52
     * Calculate width to insert into html area
53
     *
54
     * @return int
55
     */
56
    public function getInsertHeight() {
57
        $size = $this->getInsertDimensions();
58
        return $size ? $size['height'] : null;
59
    }
60
61
    /**
62
     * Get dimensions of this image sized within insert_width x insert_height
63
     *
64
     * @return array|null
65
     */
66
    protected function getInsertDimensions() {
67
        $dimensions = $this->owner->getDimensions('array');
68
        if (!$dimensions) {
69
            return null;
70
        }
71
72
        list ($width, $height) = $dimensions;
73
        $maxWidth = $this->owner->config()->get('insert_width');
74
        $maxHeight = $this->owner->config()->get('insert_height');
75
76
        // Within bounds
77
        if ($width < $maxWidth && $height < $maxHeight) {
78
            return [
79
                'width' => $width,
80
                'height' => $height,
81
            ];
82
        }
83
84
        // Check if sizing by height or width
85
		if( ($width * $maxHeight) < ($height * $maxWidth) ) {
86
			// Size by height
87
            return [
88
                'width' => intval(($width * $maxHeight) / $height + 0.5),
89
                'height' => $maxHeight,
90
            ];
91
92
		} else {
93
			// Size by width
94
            return [
95
                'width' => $maxWidth,
96
                'height' => intval(($height * $maxWidth) / $width + 0.5),
97
            ];
98
		}
99
    }
100
101
    /**
102
     * @param int $from
103
     * @param int $to
104
     *
105
     * @return string
106
     */
107
    public function humanizedChanges($from, $to) {
108
        if(!$from) {
109
            return _t('SilverStripe\\AssetAdmin\\Controller\\AssetAdmin.UPLOADEDFILE', "Uploaded file");
110
        }
111
112
        $fromRecord = Versioned::get_version($this->owner->class, $this->owner->ID, $from);
113
        $toRecord = Versioned::get_version($this->owner->class, $this->owner->ID, $to);
114
115
        $diff = new DataDifferencer($fromRecord, $toRecord);
116
        $changes = $diff->changedFieldNames();
117
118
        $k = array_search('LastEdited', $changes);
119
120
        if($k !== false) {
121
            unset($changes[$k]);
122
        }
123
124
        $output = array();
125
126
        foreach($changes as $change) {
127
            $human = $change;
0 ignored issues
show
Unused Code introduced by
$human is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
128
129
            if($change == "ParentID") {
130
                // updated folder ID
131
                $human = _t('SilverStripe\\AssetAdmin\\Controller\\AssetAdminFile.MOVEDFOLDER', "Moved file");
132
            } else if($change == 'Title') {
133
                $human = _t('SilverStripe\\AssetAdmin\\Controller\\AssetAdminFile.RENAMEDTITLE', "Updated title to ") . $fromRecord->Title;
134
            } else if($change == 'Name') {
135
                $human = _t('SilverStripe\\AssetAdmin\\Controller\\AssetAdminFile.RENAMEDFILE', "Renamed file to ") . $fromRecord->Filename;
136
            } else if($change == 'File') {
137
                // check to make sure the files are actually different
138
                if($fromRecord->getHash() != $toRecord->getHash()) {
139
                    $human = _t('SilverStripe\\AssetAdmin\\Controller\\AssetAdminFile.RENAMEDFILE', "Replaced file");
140
                } else {
141
                    $human = false;
142
                }
143
            } else {
144
                $human = false;
145
            }
146
147
            if($human) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $human of type string|false is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
148
                $output[] = $human;
149
            }
150
        }
151
152
        return implode(", ", $output);
153
    }
154
}
155