Passed
Push — master ( b298f7...a19e37 )
by Antony
02:10
created

ProductExtension::getModelDescription()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 3
nc 2
nop 0
1
<?php
2
3
namespace AntonyThorpe\SilverShopProductModel;
4
5
use SilverStripe\Forms\FieldList;
6
use SilverStripe\ORM\ArrayLib;
7
use SilverStripe\Forms\DropdownField;
8
use SilverStripe\ORM\DataExtension;
9
10
/**
11
 * Add model dropdown to Product where available
12
 * Extends SilverShop\Page\Product
13
 */
14
class ProductExtension extends DataExtension
15
{
16
    public function updateCMSFields(FieldList $fields)
17
    {
18
        // if there are Models set in the Product Category then use a dropdown to select
19
        if ($this->owner->Parent && $this->owner->Parent->ProductModels()->count()) {
20
            $fields->replaceField(
21
                'Model',
22
                DropdownField::create(
23
                    'Model',
24
                    _t(self::class . 'ModelRequired', 'Model (required)'),
25
                    ArrayLib::valuekey($this->owner->Parent->ProductModels()->column('Title'))
26
                )
27
                    ->setEmptyString(_t(self::class . 'ModelSelect', 'Select...'))
28
                    ->setAttribute('Required', true)
29
            );
30
        } else {
31
            // Update Model for extended length
32
            // see config.yml for updated db settings
33
            $model = $fields->dataFieldByName('Model');
34
            $model->setMaxLength(100);
35
        }
36
    }
37
38
    /**
39
     * For the template within the GroupedList, provide the model's Description recorded within the ProductModel Class
40
     * @return string description of the model
41
     */
42
    public function getModelDescription()
43
    {
44
        $productmodels = $this->owner->Parent->ProductModels();
0 ignored issues
show
Bug introduced by
The method ProductModels() 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

44
        /** @scrutinizer ignore-call */ 
45
        $productmodels = $this->owner->Parent->ProductModels();

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...
45
        if ($productmodels->count() && $model = $this->owner->Model) {
46
            return $productmodels->find('Title', $model)->Description;
47
        }
48
    }
49
}
50