Completed
Pull Request — master (#30)
by Jason
17:47 queued 02:47
created

updateCollectionObject()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 2
nc 2
nop 1
dl 0
loc 4
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace Dynamic\ProductCatalog\ORM;
4
5
use Dynamic\ProductCatalog\Page\CatalogCategory;
6
use SilverStripe\Forms\DropdownField;
7
use SilverStripe\ORM\ArrayList;
8
use SilverStripe\ORM\DataExtension;
9
10
class ProductDocCollectionDataExtension extends DataExtension
11
{
12
    /**
13
     * @param $object
14
     */
15
    public function updateCollectionObject(&$object)
16
    {
17
        if ($class = $this->owner->data()->ManagedClass) {
18
            $object = (string) $class;
19
        }
20
    }
21
22
    /**
23
     * @param $form
24
     */
25
    public function updateCollectionForm(&$form)
26
    {
27
        $fields = $form->Fields();
28
29
        $fields->insertAfter(
30
            DropdownField::create('CategoryID', 'Category', CatalogCategory::get()->map())
0 ignored issues
show
Bug introduced by
'CategoryID' of type string is incompatible with the type array expected by parameter $args of SilverStripe\View\ViewableData::create(). ( Ignorable by Annotation )

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

30
            DropdownField::create(/** @scrutinizer ignore-type */ 'CategoryID', 'Category', CatalogCategory::get()->map())
Loading history...
31
                ->setEmptyString('All categories'),
32
            'Title'
33
        );
34
35
        $fields->insertAfter(
36
            DropdownField::create('Products__ID', 'Product', CatalogProduct::get()->map())
0 ignored issues
show
Bug introduced by
Dynamic\ProductCatalog\O...ogProduct::get()->map() of type SilverStripe\ORM\Map is incompatible with the type array expected by parameter $args of SilverStripe\View\ViewableData::create(). ( Ignorable by Annotation )

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

36
            DropdownField::create('Products__ID', 'Product', /** @scrutinizer ignore-type */ CatalogProduct::get()->map())
Loading history...
37
                ->setEmptyString('All products'),
38
            'CategoryID'
39
        );
40
41
        $fields->removeByName([
42
            'Name',
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 = CatalogCategory::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