Completed
Push — master ( 2116b8...f7b255 )
by Nicolaas
01:34
created

DiscountCouponSiteTreeDOD   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 4
dl 0
loc 76
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A updateCMSFields() 0 17 1
D canBeDiscounted() 0 37 9
1
<?php
2
3
/**
4
 *
5
 */
6
7
class DiscountCouponSiteTreeDOD 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...
8
{
9
    private static $db = array(
0 ignored issues
show
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...
10
        'PageIDs' => 'Text(700)'
11
    );
12
13
    /**
14
     * update the CMS Fields
15
     *
16
     * @param FieldList $fields
17
     *
18
     */
19
    public function updateCMSFields(FieldList $fields)
20
    {
21
        $label = _t(
22
            "DiscountCouponSiteTreeDOD.SELECT_PRODUCTS_AND_SERVICES",
23
            'Select Product Categories and/or Products (if nothing is selected, the discount coupon will apply to all buyables).'
24
        );
25
        $field = new DiscountCouponSiteTreeDOD_Field(
26
            $name = "PageIDs",
27
            $title = $label,
28
            $sourceObject = "SiteTree",
29
            $keyField = "ID",
30
            $labelField = "MenuTitle"
31
        );
32
        $filter = create_function('$obj', 'return ( ( $obj InstanceOf ProductGroup || $obj InstanceOf Product) && ($obj->ParentID != '.$this->owner->ID.'));');
0 ignored issues
show
Security Best Practice introduced by
The use of create_function is highly discouraged, better use a closure.

create_function can pose a great security vulnerability as it is similar to eval, and could be used for arbitrary code execution. We highly recommend to use a closure instead.

// Instead of
$function = create_function('$a, $b', 'return $a + $b');

// Better use
$function = function($a, $b) { return $a + $b; }
Loading history...
33
        $field->setFilterFunction($filter);
34
        $fields->addFieldToTab('Root.AppliesTo', $field);
35
    }
36
37
    /**
38
     * normally returns TRUE, but returns FALSE when it, or its parent is in the list.
39
     * todo: add products in other product categories
40
     *
41
     * @param SiteTree $page
42
     *
43
     * @return boolean
44
     */
45
    public function canBeDiscounted(SiteTree $page)
46
    {
47
        if ($this->owner->PageIDs) {
48
            $allowedPageIDs = explode(',', $this->owner->PageIDs);
49
            $checkPages = ArrayList::create([$page]);
50
            $alreadyCheckedPageIDs = array();
51
            while ($checkPages->Count()) {
52
                $page = $checkPages->First();
53
                if (array_search($page->ID, $allowedPageIDs) !== false) {
54
                    return true;
55
                }
56
                $alreadyCheckedPageIDs[] = $page->ID;
57
                $checkPages->remove($page);
58
59
                // Parents list update
60
                if ($page->hasMethod('AllParentGroups')) {
61
                    $parents = ArrayList::create($page->AllParentGroups()->toArray());
62
                } else {
63
                    $parents = ArrayList::create();
64
                }
65
66
                $parent = $page->Parent();
67
                if ($parent && $parent->exists()) {
68
                    $parents->unshift($parent);
69
                }
70
71
                foreach ($parents as $parent) {
72
                    if (array_search($parent->ID, $alreadyCheckedPageIDs) === false) {
73
                        $checkPages->push($parent);
74
                    }
75
                }
76
                $checkPages->removeDuplicates();
77
            }
78
            return false;
79
        }
80
        return true;
81
    }
82
}
83