Completed
Push — master ( e1b964...b298f7 )
by Antony
07:32
created

ProductModel::validate()   A

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
namespace AntonyThorpe\SilverShopProductModel;
4
5
use SilverShop\Page\ProductCategory;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, AntonyThorpe\SilverShopP...ctModel\ProductCategory. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
6
use SilverStripe\Forms\TextField;
7
use SilverStripe\Forms\FieldList;
8
use SilverStripe\ORM\DataObject;
9
10
/**
11
 * Create Product Models in the Product Category and use these for the dropdown when editing a product
12
 * (instead of a textfield).
13
 * In addition, loop over the models on the Product Category Page
14
 */
15
class ProductModel extends DataObject
16
{
17
    private static $db = array(
0 ignored issues
show
introduced by
The private property $db is not used, and could be removed.
Loading history...
18
        'Title' => 'Varchar(100)',
19
        'Description' => 'Varchar(255)',
20
        'Sort' => 'Int'
21
    );
22
23
    private static $has_one = array(
0 ignored issues
show
introduced by
The private property $has_one is not used, and could be removed.
Loading history...
24
        'ProductCategory' => ProductCategory::class
25
    );
26
27
    private static $required_fields = array(
0 ignored issues
show
introduced by
The private property $required_fields is not used, and could be removed.
Loading history...
28
        'Title'
29
    );
30
31
    private static $table_name = 'AntonyThorpe_SilverShopProductModel_ProductModel';
0 ignored issues
show
introduced by
The private property $table_name is not used, and could be removed.
Loading history...
32
33
    public function getCMSFields()
34
    {
35
        return FieldList::create(
36
            TextField::create('Title', _t(self::class . 'Title', 'Model Title'))
37
                ->setMaxLength(100),
38
            TextField::create('Description', _t(self::class . 'Description', 'Model Description'))
39
                ->setRightTitle(_t(self::class . 'DescriptionRightTitle', 'An additional description if needed by the website'))
40
                ->setMaxLength(255)
41
        );
42
    }
43
44
    public function validate()
45
    {
46
        $result = parent::validate();
47
48
        if (empty($this->Title)) {
49
            $result->addError(
50
                _t(
51
                    self::class . 'ValidationMessageTitle',
52
                    'ProductModel Class validation - missing the Model Title field'
53
                )
54
            );
55
        }
56
57
        if (empty($this->ProductCategoryID)) {
0 ignored issues
show
Bug Best Practice introduced by
The property ProductCategoryID does not exist on AntonyThorpe\SilverShopProductModel\ProductModel. Since you implemented __get, consider adding a @property annotation.
Loading history...
58
            $result->addError(
59
                _t(
60
                    self::class . 'ValidationMessageProductCategoryID',
61
                    'ProductModel Class validation - missing ProductCategoryID field'
62
                )
63
            );
64
        }
65
66
        return $result;
67
    }
68
}
69