ProductDocCollectionDataExtension   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Test Coverage

Coverage 57.69%

Importance

Changes 0
Metric Value
eloc 25
dl 0
loc 58
rs 10
c 0
b 0
f 0
ccs 15
cts 26
cp 0.5769
wmc 8

3 Methods

Rating   Name   Duplication   Size   Complexity  
A updateCollectionForm() 0 19 1
A updateCollectionItems() 0 17 5
A updateCollectionObject() 0 4 2
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 2
    public function updateCollectionObject(&$object)
16
    {
17 2
        if ($class = $this->owner->data()->ManagedClass) {
18 2
            $object = singleton((string) $class);
19
        }
20
    }
21
22
    /**
23
     * @param $form
24
     */
25 1
    public function updateCollectionForm(&$form)
26
    {
27 1
        $fields = $form->Fields();
28
29 1
        $fields->insertAfter(
30 1
            DropdownField::create('CategoryID', 'Category', CatalogCategory::get()->map())
31 1
                ->setEmptyString('All categories'),
32 1
            'Title'
33
        );
34
35 1
        $fields->insertAfter(
36 1
            DropdownField::create('Products__ID', 'Product', CatalogProduct::get()->map())
37 1
                ->setEmptyString('All products'),
38 1
            'CategoryID'
39
        );
40
41 1
        $fields->removeByName([
42 1
            '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