ProductDocDataExtension   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 121
Duplicated Lines 0 %

Test Coverage

Coverage 97.14%

Importance

Changes 0
Metric Value
eloc 43
dl 0
loc 121
rs 10
c 0
b 0
f 0
ccs 34
cts 35
cp 0.9714
wmc 11

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getIsProductDoc() 0 3 1
A getProductsList() 0 15 4
A getProductsCt() 0 7 2
A updateCMSFields() 0 29 3
A Link() 0 3 1
1
<?php
2
3
namespace Dynamic\ProductCatalog\ORM;
4
5
use Dynamic\ProductCatalog\Docs\ProductDoc;
6
use SilverStripe\Core\ClassInfo;
7
use SilverStripe\Forms\DropdownField;
8
use SilverStripe\Forms\FieldList;
9
use SilverStripe\Forms\GridField\GridField;
10
use SilverStripe\Forms\GridField\GridFieldAddExistingAutocompleter;
11
use SilverStripe\Forms\GridField\GridFieldAddNewButton;
12
use SilverStripe\Forms\GridField\GridFieldConfig_RelationEditor;
13
use SilverStripe\ORM\DataExtension;
14
use Symbiote\GridFieldExtensions\GridFieldAddExistingSearchButton;
15
16
class ProductDocDataExtension extends DataExtension
17
{
18
    /**
19
     * @var array
20
     */
21
    private static $belongs_many_many = array(
0 ignored issues
show
introduced by
The private property $belongs_many_many is not used, and could be removed.
Loading history...
22
        'Products' => CatalogProduct::class,
23
    );
24
25
    /**
26
     * @var array
27
     */
28
    private static $summary_fields = array(
0 ignored issues
show
introduced by
The private property $summary_fields is not used, and could be removed.
Loading history...
29
        'Name' => 'Name',
30
        'Title' => 'Title',
31
        'ProductsCt' => 'Products',
32
    );
33
34
    /**
35
     * @var array
36
     */
37
    private static $searchable_fields = array(
0 ignored issues
show
introduced by
The private property $searchable_fields is not used, and could be removed.
Loading history...
38
        'Name',
39
        'Title',
40
        'Products.ID',
41
    );
42
43
    /**
44
     * @return int
45
     */
46 1
    public function getProductsCt()
47
    {
48 1
        if ($this->owner->Products()->exists()) {
49 1
            return $this->owner->Products()->count();
50
        }
51
52
        return 0;
53
    }
54
55
    /**
56
     * @return string
57
     */
58 1
    public function getProductsList()
59
    {
60 1
        $list = '';
61
62 1
        if ($this->owner->Products()->exists()) {
63 1
            $i = 0;
64 1
            foreach ($this->owner->Products() as $product) {
65 1
                $list .= $product->Title;
66 1
                if (++$i !== $this->owner->Products()->Count()) {
67 1
                    $list .= ', ';
68
                }
69
            }
70
        }
71
72 1
        return $list;
73
    }
74
75
    /**
76
     * @param FieldList $fields
77
     */
78 5
    public function updateCMSFields(FieldList $fields)
79
    {
80 5
        $fields->removeByName(array(
81 5
            'Link',
82
            'SortOrder',
83
            'Products',
84
        ));
85
86 5
        $classes = ClassInfo::subclassesFor(ProductDoc::class);
87 5
        unset($classes['ProductDoc']);
88
89 5
        $result = array();
90 5
        foreach ($classes as $class) {
91 5
            $instance = singleton($class);
92 5
            $pageTypeName = $instance->i18n_singular_name();
93 5
            $result[$class] = $pageTypeName;
94
        }
95
96 5
        $fields->addFieldToTab(
97 5
            'Root.Main',
98 5
            DropdownField::create('SetClass', 'File type', $result, $this->owner->Classname)
99 5
                ->setEmptyString(''),
100 5
            'Content'
101
        );
102
103 5
        $fields->dataFieldByName('Image')
104 5
            ->setFolderName('Uploads/ProductDocs/Images');
105
106 5
        if ($this->owner->ID) {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
107
            // products
108
            /*
0 ignored issues
show
Unused Code Comprehensibility introduced by
59% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
109
            $config = GridFieldConfig_RelationEditor::create();
110
            $config->removeComponentsByType(GridFieldAddExistingAutocompleter::class);
111
            $config->addComponent(new GridFieldAddExistingSearchButton());
112
            $config->removeComponentsByType(GridFieldAddNewButton::class);
113
            $products = $this->owner->Products();
114
            $productsField = GridField::create('Products', 'Products', $products, $config);
115
116
            $fields->addFieldToTab('Root.Products', $productsField);
117
            */
118
        }
119
    }
120
121
    /**
122
     * Link for searchable result set.
123
     *
124
     * @return mixed
125
     */
126 1
    public function Link()
127
    {
128 1
        return $this->owner->Download()->URL;
129
    }
130
131
    /**
132
     * @return bool
133
     */
134 1
    public function getIsProductDoc()
135
    {
136 1
        return true;
137
    }
138
}
139