Completed
Pull Request — master (#49)
by Nic
09:03
created

TestDiscountExtension::updateCMSFields()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 32
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 25
dl 0
loc 32
rs 9.52
c 1
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
namespace Dynamic\Foxy\Discounts\Tests\TestOnly\Extension;
4
5
use Dynamic\Foxy\Discounts\Tests\TestOnly\Page\ProductPage;
6
use SilverStripe\Forms\FieldList;
7
use SilverStripe\Forms\GridField\GridFieldAddExistingAutocompleter;
8
use SilverStripe\Forms\GridField\GridFieldAddNewButton;
9
use SilverStripe\ORM\DataExtension;
10
use Dynamic\Products\Page\Product;
0 ignored issues
show
Bug introduced by
The type Dynamic\Products\Page\Product was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
use SilverStripe\Versioned\GridFieldArchiveAction;
12
use Symbiote\GridFieldExtensions\GridFieldAddExistingSearchButton;
13
14
/**
15
 * Class TestDiscountExtension
16
 * @package Dynamic\Foxy\Discounts\Tests\TestOnly\Extension
17
 */
18
class TestDiscountExtension extends DataExtension
19
{
20
    /**
21
     * @var array
22
     */
23
    private static $many_many = [
0 ignored issues
show
introduced by
The private property $many_many is not used, and could be removed.
Loading history...
24
        'Products' => ProductPage::class,
25
        'ExcludeProducts' => ProductPage::class,
26
    ];
27
28
    /**
29
     * @param FieldList $fields
30
     */
31
    public function updateCMSFields(FieldList $fields)
32
    {
33
        if ($this->owner->ID) {
34
            // Products
35
            $field = $fields->dataFieldByName('Products');
36
            $fields->removeByName('Products');
37
            $fields->addFieldToTab('Root.Included', $field);
38
            $field->setDescription('Limit the discount to these products. If no products specified, all products will receive the discount');
39
            $config = $field->getConfig();
40
            $config
41
                ->removeComponentsByType([
42
                    GridFieldAddExistingAutocompleter::class,
43
                    GridFieldAddNewButton::class,
44
                    GridFieldArchiveAction::class,
45
                ])
46
                ->addComponents([
47
                    new GridFieldAddExistingSearchButton(),
48
                ]);
49
50
            $exclusions = $fields->dataFieldByName('ExcludeProducts');
51
            $fields->removeByName('ExcludeProducts');
52
            $fields->addFieldToTab('Root.Excluded', $exclusions);
53
            $exclusions->setDescription('Products in this list will ALWAYS be excluded from the discount, even if added to the "Included" tab.');
54
            $excludeConfig = $exclusions->getConfig();
55
            $excludeConfig
56
                ->removeComponentsByType([
57
                    GridFieldAddExistingAutocompleter::class,
58
                    GridFieldAddNewButton::class,
59
                    GridFieldArchiveAction::class,
60
                ])
61
                ->addComponents([
62
                    new GridFieldAddExistingSearchButton(),
63
                ]);
64
        }
65
    }
66
67
    /**
68
     * @return array
69
     */
70
    public function getRestrictions()
71
    {
72
        if ($this->owner->Products()->count() == 0) {
73
            $products = Product::get()->column();
74
        } else {
75
            $products = $this->owner->Products()->column();
76
        }
77
78
        foreach ($this->owner->ExcludeProducts()->column() as $id) {
79
            if (in_array($id, $products)) {
80
                $key = array_search($id, $products);
81
                unset($products[$key]);
82
            }
83
        }
84
85
        return $products;
86
    }
87
}
88