Completed
Push — master ( d755fc...4a465e )
by Nicolaas
01:34
created

ImageWithStyleDataExtension   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 10

Importance

Changes 0
Metric Value
wmc 16
lcom 1
cbo 10
dl 0
loc 100
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
C AddSelectImageList() 0 71 11
A createImageWithStyleListAndFolder() 0 20 4
A folderToListName() 0 4 1
1
<?php
2
3
4
5
class ImageWithStyleDataExtension extends DataExtension
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...
6
{
7
    public function AddSelectImageList($fields, $tabName, $methodName, $folderName = '')
8
    {
9
        if ($this->owner->exists()) {
10
            $this->owner->createImageWithStyleListAndFolder($methodName, $folderName);
11
            $obj = $this->owner->$methodName();
12
            if ($obj && $obj->exists()) {
13
                $link = $obj->CMSEditLink();
0 ignored issues
show
Unused Code introduced by
$link 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...
14
                $title = 'edit '.$obj->Title;
0 ignored issues
show
Unused Code introduced by
$title 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...
15
            } else {
16
                if (! $obj) {
17
                    $obj = Injector::inst()->get('ImagesWithStyleSelection');
18
                }
19
                $link = $obj->CMSAddLink();
0 ignored issues
show
Unused Code introduced by
$link 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...
20
                $title = 'add '.$obj->singular_name();
0 ignored issues
show
Unused Code introduced by
$title 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...
21
            }
22
            $standardListName = $this->owner->folderToListName($folderName);
23
24
            $list = ImagesWithStyleSelection::get()->map()->toArray();
25
            $myList = ImagesWithStyleSelection::get()->filter(['Title' => $standardListName]);
26
            if ($myList->count() === 1) {
27
                $myID = $myList->first()->ID;
28
                $list[$myID] = ' *** '.$list[$myID]." [RECOMMENDED] ";
29
                asort($list);
30
            }
31
            $fields->addFieldsToTab(
32
                'Root.'.$tabName,
33
                [
34
                    HasOneButtonField::create($methodName, $this->owner->$methodName(), $this->owner),
35
                    LiteralField::create($methodName.'_OR', '<h2>OR</h2>'),
36
                    $imageListField = DropdownField::create(
37
                        $methodName.'ID',
38
                        'Select Existing Images List',
39
                        [0 => '--- Select ---'] + $list
40
                    )
41
                ]
42
            );
43
44
            $fieldID = $tabName.'ImageSelectionID';
45
            if ($this->owner->$fieldID && $imageList = ImagesWithStyleSelection::get()->byID($this->owner->$fieldID)) {
46
                if ($imageList->PlaceToStoreImagesID) {
47
                    $imageListField->setRightTitle(
48
                        $this->owner->FolderDescriptionOrRightTitle($imageList->PlaceToStoreImagesID)
49
                    );
50
                }
51
            }
52
            if ($obj->exists() && $obj->StyledImages()->count()) {
53
                $config = GridFieldConfig_RecordEditor::create()->removeComponentsByType('GridFieldAddNewButton');
54
                $fields->addFieldsToTab(
55
                    'Root.'.$tabName,
56
                    [
57
                        GridField::create(
58
                            $methodName.'_Images',
59
                            'Included Are',
60
                            $obj->StyledImages(),
61
                            $config
62
                        )
63
                    ]
64
                );
65
            }
66
        } else {
67
            $fields->addFieldsToTab(
68
                'Root.'.$tabName,
69
                [
70
                    LiteralField::create(
71
                        $methodName.'_LINK',
72
                        '<h2>First save this page and then add images to it.</h2>'
73
                    )
74
                ]
75
            );
76
        }
77
    }
78
79
    public function createImageWithStyleListAndFolder($methodName, $folderName = '')
80
    {
81
        if (strtotime($this->owner->LastEdited) > (strtotime($this->owner->Created) + 1)) {
82
            if (!$folderName) {
83
                $folderName = $methodName.'-for-'.$this->owner->ClassName.'-'.$this->owner->ID;
84
            }
85
            $listName = $this->owner->folderToListName($folderName);
86
87
            $array = [
88
                'Title' => $listName
89
            ];
90
            $obj = ImagesWithStyleSelection::get()->filter($array)->first();
91
            if (! $obj) {
92
                $obj = ImagesWithStyleSelection::create($array);
93
            }
94
            $obj->write();
95
            $obj->createFolder($folderName);
96
            $fieldName = $methodName.'ID';
0 ignored issues
show
Unused Code introduced by
$fieldName 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...
97
        }
98
    }
99
100
    public function folderToListName($folderName)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
101
    {
102
        return str_replace('/', '-', $folderName);
103
    }
104
}
105