ProductFileCollectionDataExtension   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
eloc 24
c 1
b 0
f 0
dl 0
loc 57
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A updateCollectionForm() 0 18 1
A updateCollectionItems() 0 17 5
A updateCollectionObject() 0 4 2
1
<?php
2
3
namespace Dynamic\Products\Extension;
4
5
use Dynamic\Products\Page\Product;
6
use Dynamic\Products\Page\ProductCategory;
7
use SilverStripe\Forms\DropdownField;
8
use SilverStripe\ORM\ArrayList;
9
use SilverStripe\ORM\DataExtension;
10
11
class ProductFileCollectionDataExtension extends DataExtension
12
{
13
    /**
14
     * @param $object
15
     */
16
    public function updateCollectionObject(&$object)
17
    {
18
        if ($class = $this->owner->data()->ManagedClass) {
19
            $object = singleton((string) $class);
20
        }
21
    }
22
23
    /**
24
     * @param $form
25
     */
26
    public function updateCollectionForm(&$form)
27
    {
28
        $fields = $form->Fields();
29
30
        $fields->insertAfter(
31
            DropdownField::create('CategoryID', 'Category', ProductCategory::get()->map())
32
                ->setEmptyString('All categories'),
33
            'Title'
34
        );
35
36
        $fields->insertAfter(
37
            DropdownField::create('Products__ID', 'Product', Product::get()->map())
38
                ->setEmptyString('All products'),
39
            'CategoryID'
40
        );
41
42
        $fields->removeByName([
43
            'Title',
44
        ]);
45
    }
46
47
    /**
48
     * @param $collection
49
     * @param $searchCriteria
50
     */
51
    public function updateCollectionItems(&$collection, &$searchCriteria)
52
    {
53
        $class = $this->owner->data()->ManagedClass;
54
55
        if (isset($searchCriteria['CategoryID']) && $searchCriteria['CategoryID'] != '') {
56
            $category = ProductCategory::get()->byID($searchCriteria['CategoryID']);
57
            $products = $category->Products();
58
            $docs = new ArrayList();
59
60
            foreach ($products as $product) {
61
                $records = $class::get()->filter(['Products.ID' => $product->ID]);
0 ignored issues
show
Bug introduced by
The method get() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

61
                $records = $class::/** @scrutinizer ignore-call */ get()->filter(['Products.ID' => $product->ID]);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
62
                foreach ($records as $record) {
63
                    $docs->push($record);
64
                }
65
            }
66
67
            $collection = $docs;
68
        }
69
    }
70
}
71