ProductModel::validate()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 23
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 12
dl 0
loc 23
rs 9.8666
c 0
b 0
f 0
cc 3
nc 4
nop 0
1
<?php
2
/**
3
 * Create Product Models in the Product Category and use these for the dropdown when editing a product
4
 * (instead of a textfield).
5
 * In addition, loop over the models on the Product Category Page
6
 */
7
namespace AntonyThorpe\SilverShopProductModel;
8
9
use SilverShop\Page\ProductCategory;
0 ignored issues
show
Bug introduced by
The type SilverShop\Page\ProductCategory 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...
10
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...
11
use SilverStripe\Forms\TextField;
0 ignored issues
show
Bug introduced by
The type SilverStripe\Forms\TextField 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...
12
use SilverStripe\ORM\DataObject;
0 ignored issues
show
Bug introduced by
The type SilverStripe\ORM\DataObject 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...
13
14
class ProductModel extends DataObject
15
{
16
    /**
17
     * @config
18
     */
19
    private static array $db = [
0 ignored issues
show
introduced by
The private property $db is not used, and could be removed.
Loading history...
20
        'Title' => 'Varchar(100)',
21
        'Description' => 'Varchar(255)',
22
        'Sort' => 'Int'
23
    ];
24
25
    /**
26
     * @config
27
     */
28
    private static array $has_one = [
0 ignored issues
show
introduced by
The private property $has_one is not used, and could be removed.
Loading history...
29
        'ProductCategory' => ProductCategory::class
30
    ];
31
32
    /**
33
     * @config
34
     */
35
    private static array $required_fields = ['Title'];
0 ignored issues
show
introduced by
The private property $required_fields is not used, and could be removed.
Loading history...
36
37
    /**
38
     * @config
39
     */
40
    private static string $table_name = 'SilverShop_ProductModel';
0 ignored issues
show
introduced by
The private property $table_name is not used, and could be removed.
Loading history...
41
42
    public function getCMSFields()
43
    {
44
        return FieldList::create(
45
            TextField::create('Title', _t(self::class . 'Title', 'Model Title'))
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

45
            TextField::create('Title', /** @scrutinizer ignore-call */ _t(self::class . 'Title', 'Model Title'))
Loading history...
46
                ->setMaxLength(100),
47
            TextField::create('Description', _t(self::class . 'Description', 'Model Description'))
48
                ->setRightTitle(_t(self::class . 'DescriptionRightTitle', 'An additional description if needed by the website'))
49
                ->setMaxLength(255)
50
        );
51
    }
52
53
    public function validate()
54
    {
55
        $result = parent::validate();
56
57
        if (empty($this->Title)) {
58
            $result->addError(
59
                _t(
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

59
                /** @scrutinizer ignore-call */ 
60
                _t(
Loading history...
60
                    self::class . 'ValidationMessageTitle',
61
                    'ProductModel Class validation - missing the Model Title field'
62
                )
63
            );
64
        }
65
66
        if (empty($this->ProductCategoryID)) {
67
            $result->addError(
68
                _t(
69
                    self::class . 'ValidationMessageProductCategoryID',
70
                    'ProductModel Class validation - missing ProductCategoryID field'
71
                )
72
            );
73
        }
74
75
        return $result;
76
    }
77
}
78