ProductExtension::updateCMSFields()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 12
dl 0
loc 19
rs 9.8666
c 0
b 0
f 0
cc 3
nc 2
nop 1
1
<?php
2
3
namespace AntonyThorpe\SilverShopProductModel;
4
5
use SilverStripe\Forms\FieldList;
0 ignored issues
show
Bug introduced by
The type SilverStripe\Forms\FieldList 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...
6
use SilverStripe\ORM\ArrayLib;
0 ignored issues
show
Bug introduced by
The type SilverStripe\ORM\ArrayLib 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...
7
use SilverStripe\Forms\DropdownField;
0 ignored issues
show
Bug introduced by
The type SilverStripe\Forms\DropdownField 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...
8
use SilverStripe\ORM\DataExtension;
0 ignored issues
show
Bug introduced by
The type SilverStripe\ORM\DataExtension 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...
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): void
17
    {
18
        // if there are Models set in the Product Category then use a dropdown to select
19
        if ($this->getOwner()->Parent && $this->getOwner()->Parent->ProductModels()->count()) {
20
            $fields->replaceField(
21
                'Model',
22
                DropdownField::create(
23
                    'Model',
24
                    _t(self::class . 'ModelRequired', 'Model (required)'),
0 ignored issues
show
Bug introduced by
The function _t was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

24
                    /** @scrutinizer ignore-call */ 
25
                    _t(self::class . 'ModelRequired', 'Model (required)'),
Loading history...
25
                    ArrayLib::valuekey($this->getOwner()->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(): string|null
43
    {
44
        if ($this->getOwner()->Parent) {
45
            $productmodels = $this->getOwner()->Parent->ProductModels();
46
            if ($productmodels->count() && $this->getOwner()->Model) {
47
                $model = $this->getOwner()->Model;
48
                return $productmodels->find('Title', $model)->Description;
49
            }
50
        }
51
        return null;
52
    }
53
}
54