Completed
Pull Request — master (#287)
by Nic
12:24
created

ProductHolder_Controller::product()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 16
c 0
b 0
f 0
ccs 0
cts 8
cp 0
rs 9.4285
cc 3
eloc 7
nc 3
nop 1
crap 12
1
<?php
2
3
/**
4
 * @package FoxyStripe
5
 * @method Products|SS_List $Products
6
 */
7
class ProductHolder extends Page
8
{
9
10
    /**
11
     * @var string
12
     */
13
    private static $singular_name = 'Product Group';
14
15
    /**
16
     * @var string
17
     */
18
    private static $plural_name = 'Product Groups';
19
20
    /**
21
     * @var string
22
     */
23
    private static $description = 'Display a list of related products';
24
25
    /**
26
     * @var array
27
     */
28
    private static $allowed_children = [
0 ignored issues
show
Unused Code introduced by
The property $allowed_children 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
        'ProductHolder',
30
    ];
31
32
    /**
33
     * @var array
34
     */
35
    private static $many_many = [
36
        'Products' => 'FoxyStripeProduct',
37
    ];
38
39
    /**
40
     * @var array
41
     */
42
    private static $many_many_extraFields = array(
43
        'Products' => array(
44
            'SortOrder' => 'Int'
45
        )
46
    );
47
48
    /**
49
     * @return FieldList
50
     */
51 1
    public function getCMSFields()
52
    {
53 1
        $fields = parent::getCMSFields();
54
55 1
        $config = GridFieldConfig_RelationEditor::create();
56 1
        if (class_exists('GridFieldSortableRows')) {
57
            $config->addComponent(new GridFieldSortableRows('SortOrder'));
58
        }
59 1
        $config->removeComponentsByType('GridFieldAddExistingAutocompleter');
60 1
        $config->addComponent(new GridFieldAddExistingSearchButton());
61 1
        $fields->addFieldToTab(
62 1
            'Root.Products',
63 1
            GridField::create(
64 1
                'Products',
65 1
                _t('ProductHolder.Products', 'Products'),
66 1
                $this->Products(),
67
                $config
68 1
            )
69 1
        );
70
71 1
        $this->extend('updateCMSFields', $fields);
72
73 1
        return $fields;
74
    }
75
76
    /**
77
     * loadDescendantProductGroupIDListInto function.
78
     *
79
     * @access public
80
     * @param mixed &$idList
81
     */
82 1
    public function loadDescendantProductGroupIDListInto(&$idList)
83
    {
84
        if ($children = $this->AllChildren()) {
85 1
            foreach ($children as $child) {
86
                if (in_array($child->ID, $idList)) continue;
87
88
                if ($child instanceof ProductHolder) {
89
                    $idList[] = $child->ID;
90
                    $child->loadDescendantProductGroupIDListInto($idList);
91
                }
92
            }
93
        }
94
    }
95
96
    /**
97
     * ProductGroupIDs function.
98
     *
99
     * @access public
100
     * @return array
101
     */
102
    public function ProductGroupIDs()
103
    {
104
        $holderIDs = [];
105
        $this->loadDescendantProductGroupIDListInto($holderIDs);
106
        return $holderIDs;
107
    }
108
109
    /**
110
     * Products function.
111
     *
112
     * @access public
113
     * @return PaginatedList
114
     */
115
    public function ProductList($limit = 10)
116
    {
117
        $config = SiteConfig::current_site_config();
118
119
        if ($config->ProductLimit > 0) {
120
            $limit = $config->ProductLimit;
121
        }
122
123
        if ($config->MultiGroup) {
124
            $entries = $this->Products()->sort('SortOrder');
125
        } else {
126
            $filter = '"ParentID" = ' . $this->ID;
127
128
            // Build a list of all IDs for ProductGroups that are children
129
            $holderIDs = $this->ProductGroupIDs();
130
131
            // If no ProductHolders, no ProductPages. So return false
132
            if ($holderIDs) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $holderIDs of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
133
                // Otherwise, do the actual query
134
                if ($filter) {
135
                    $filter .= ' OR ';
136
                }
137
                $filter .= '"ParentID" IN (' . implode(',', $holderIDs) . ")";
138
            }
139
140
            $order = '"SiteTree"."Title" ASC';
0 ignored issues
show
Unused Code introduced by
$order 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...
141
142
            $entries = FoxyStripeProduct::get()->where($filter);
143
        }
144
145
146
        $list = new PaginatedList($entries, Controller::curr()->request);
0 ignored issues
show
Documentation introduced by
\Controller::curr()->request is of type object<SS_HTTPRequest>, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
147
        $list->setPageLength($limit);
148
        return $list;
149
    }
150
}
151
152
class ProductHolder_Controller extends Page_Controller
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
153
{
154
    private static $allowed_actions = [
155
        'product',
156
    ];
157
158
    /**
159
     *
160
     */
161
    public function init()
162
    {
163
        parent::init();
164
165
    }
166
167
    public function product(SS_HTTPRequest $request)
168
    {
169
170
        if (!$slug = $request->latestParam('ID')) {
171
            return $this->httpError(404);
172
        }
173
174
        if (!$product = FoxyStripeProduct::get()->byUrlSegment($slug)) {
175
            return $this->httpError(404);
176
        }
177
178
        return $this->customise([
179
            'Product' => $product,
180
        ]);
181
182
    }
183
}