Completed
Push — master ( e20458...450b8f )
by Nicolaas
03:16
created

EcommerceAlsoRecommendedDOD::updateCMSFields()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 30
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 30
rs 8.8571
c 0
b 0
f 0
cc 2
eloc 20
nc 2
nop 1
1
<?php
2
3
4
5
class EcommerceAlsoRecommendedDOD extends DataExtension
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
6
{
7
    private static $many_many = array(
0 ignored issues
show
Unused Code introduced by
The property $many_many 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...
8
        'EcommerceRecommendedProducts' => 'Product'
9
    );
10
11
    private static $belongs_many_many = array(
0 ignored issues
show
Unused Code introduced by
The property $belongs_many_many 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...
12
        'RecommendedFor' => 'Product'
13
    );
14
15
    public function updateCMSFields(FieldList $fields)
16
    {
17
        if ($this->owner instanceof Product) {
18
19
            $fields->addFieldToTab(
20
                'Root.Links',
21
                GridField::create(
22
                    'EcommerceRecommendedProducts',
23
                    'Also Recommended Products',
24
                    $this->owner->EcommerceRecommendedProducts(),
25
                    $config = GridFieldBasicPageRelationConfig::create()
26
                )
27
            );
28
            $component = $config->getComponentByType('GridFieldAddExistingAutocompleter');
29
            $component->setSearchFields(array("InternalItemID", "Title"));
30
31
            $fields->addFieldToTab(
32
                'Root.Links',
33
                GridField::create(
34
                    'RecommendedFor',
35
                    'Recommended For',
36
                    $this->owner->RecommendedFor(),
37
                    $config = GridFieldBasicPageRelationConfig::create()
38
                )
39
            );
40
            $component = $config->getComponentByType('GridFieldAddExistingAutocompleter');
41
            $component->setSearchFields(array("InternalItemID", "Title"));
42
43
        }
44
    }
45
46
    /**
47
     *
48
     * small cleanup
49
     */
50
    public function onAfterWrite()
51
    {
52
        $products = $this->owner->EcommerceRecommendedProducts();
53 View Code Duplication
        if ($products->count()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
54
            foreach ($products as $product) {
55
                if (!$product instanceof Product) {
56
                    $products->remove($product);
57
                } elseif (!$product->AllowPurchase) {
58
                    $products->remove($product);
59
                }
60
            }
61
        }
62
        $products = $this->owner->RecommendedFor();
63 View Code Duplication
        if ($products->count()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
64
            foreach ($products as $product) {
65
                if (!$product instanceof Product) {
66
                    $products->remove($product);
67
                } elseif (!$product->AllowPurchase) {
68
                    $products->remove($product);
69
                }
70
            }
71
        }
72
    }
73
74
    /**
75
     * only returns the products that are for sale
76
     * if only those need to be showing.
77
     * @return DataList
78
     */
79 View Code Duplication
    public function EcommerceRecommendedProductsForSale()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
80
    {
81
        if ($this->owner->EcomConfig()->OnlyShowProductsThatCanBePurchased) {
82
            return $this->owner->EcommerceRecommendedProducts()->filter(array("AllowPurchase" => 1));
83
        } else {
84
            return $this->owner->EcommerceRecommendedProducts();
85
        }
86
    }
87
88
    /**
89
     * only returns the products that are for sale
90
     * if only those need to be showing.
91
     * @return DataList
92
     */
93 View Code Duplication
    public function RecommendedForForSale()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
94
    {
95
        if ($this->owner->EcomConfig()->OnlyShowProductsThatCanBePurchased) {
96
            return $this->owner->RecommendedFor()->filter(array("AllowPurchase" => 1));
97
        } else {
98
            return $this->owner->RecommendedFor();
99
        }
100
    }
101
}
102