Completed
Pull Request — master (#5)
by Jason
06:46
created

ProductDocCollectionDataExtension   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 53.13%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 3
dl 0
loc 51
ccs 17
cts 32
cp 0.5313
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
B updateCollectionItems() 0 19 5
A updateCollectionObject() 0 6 2
A updateCollectionForm() 0 15 1
1
<?php
2
3
class ProductDocCollectionDataExtension extends DataExtension
4
{
5
    /**
6
     * @param $object
7
     */
8 2
    public function updateCollectionObject(&$object)
9
    {
10 2
        if ($class = $this->owner->data()->ManagedClass) {
11 2
            $object = (string) $class;
12 2
        }
13 2
    }
14
15
    /**
16
     * @param $form
17
     */
18 1
    public function updateCollectionForm(&$form)
19
    {
20 1
        $fields = $form->Fields();
21 1
        $fields->insertAfter(
22 1
            SelectboxDropdownField::create('CategoryID', 'Category', CatalogCategory::get()->map())
23 1
                ->setEmptyString('All categories'),
24
            'Products__ID'
25 1
        );
26
27 1
        $fields->removeByName([
28 1
            'Name',
29 1
            'Title',
30 1
            'Products__ID',
31 1
        ]);
32 1
    }
33
34
    public function updateCollectionItems(&$collection, &$searchCriteria)
35
    {
36
        $class = $this->owner->data()->ManagedClass;
37
38
        if (isset($searchCriteria['CategoryID']) && $searchCriteria['CategoryID'] != '') {
39
            $category = CatalogCategory::get()->byID($searchCriteria['CategoryID']);
40
            $products = $category->Products();
41
            $docs = new ArrayList();
42
43
            foreach($products as $product) {
44
                $records = $class::get()->filter(['Products.ID' => $product->ID]);
45
                foreach ($records as $record) {
46
                    $docs->push($record);
47
                }
48
            }
49
50
            $collection = $docs;
51
        }
52
    }
53
}