Completed
Pull Request — master (#5)
by Jason
06:46
created

ProductDocDataExtension   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 118
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 94.23%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 7
dl 0
loc 118
ccs 49
cts 52
cp 0.9423
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getProductsCt() 0 7 2
A getProductsList() 0 15 4
B updateCMSFields() 0 40 3
A Link() 0 4 1
A getIsProductDoc() 0 3 1
1
<?php
2
3
class ProductDocDataExtension extends DataExtension
4
{
5
    /**
6
     * @var array
7
     */
8
    private static $belongs_many_many = array(
9
        'Products' => 'CatalogProduct',
10
    );
11
12
    /**
13
     * @var array
14
     */
15
    private static $summary_fields = array(
16
        'Name' => 'Name',
17
        'Title' => 'Title',
18
        'ProductsCt' => 'Products',
19
    );
20
21
    /**
22
     * @var array
23
     */
24
    private static $searchable_fields = array(
25
        'Name',
26
        'Title',
27
        'Products.ID',
28
    );
29
30
    /**
31
     * @return int
32
     */
33 5
    public function getProductsCt()
34
    {
35 5
        if ($this->owner->Products()->exists()) {
36 5
            return $this->owner->Products()->count();
37
        }
38
        return 0;
39
    }
40
41
    /**
42
     * @return string
43
     */
44 1
    public function getProductsList()
45
    {
46 1
        $list = '';
47
48 1
        if ($this->owner->Products()->exists()) {
49 1
            $i = 0;
50 1
            foreach($this->owner->Products() as $product) {
51 1
                $list .= $product->Title;
52 1
                if(++$i !== $this->owner->Products()->Count()) {
53
                    $list .= ", ";
54
                }
55 1
            }
56 1
        }
57 1
        return $list;
58
    }
59
60
    /**
61
     * @param FieldList $fields
62
     */
63 5
    public function updateCMSFields(FieldList $fields)
64
    {
65 5
        $fields->removeByName(array(
66 5
            'Link',
67 5
            'SortOrder',
68 5
            'Products',
69 5
        ));
70
71 5
        $classes = ClassInfo::subclassesFor('ProductDoc');
72 5
        unset($classes['ProductDoc']);
73
74 5
        $result = array();
75 5
        foreach ($classes as $class) {
0 ignored issues
show
Bug introduced by
The expression $classes of type null|array is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
76 5
            $instance = singleton($class);
77 5
            $pageTypeName = $instance->i18n_singular_name();
78 5
            $result[$class] = $pageTypeName;
79 5
        }
80
81 5
        $fields->addFieldToTab(
82 5
            'Root.Main',
83 5
            DropdownField::create('SetClass', 'File type', $result, $this->owner->Classname)
84 5
                ->setEmptyString(''),
85 5
            'Content'
86 5
        );
87
88 5
        $fields->dataFieldByName('Image')
89 5
            ->setFolderName('Uploads/ProductDocs/Images');
90
91 5
        if ($this->owner->ID) {
92
            // products
93 5
            $config = GridFieldConfig_RelationEditor::create();
94 5
            $config->removeComponentsByType('GridFieldAddExistingAutocompleter');
95 5
            $config->addComponent(new GridFieldAddExistingSearchButton());
96 5
            $config->removeComponentsByType('GridFieldAddNewButton');
97 5
            $products = $this->owner->Products();
98 5
            $productsField = GridField::create('Products', 'Products', $products, $config);
99
100 5
            $fields->addFieldToTab('Root.Products', $productsField);
101 5
        }
102 5
    }
103
104
    /**
105
     * Link for searchable result set
106
     *
107
     * @return mixed
108
     */
109 1
    public function Link()
110
    {
111 1
        return $this->owner->Download()->URL;
112
    }
113
114
    /**
115
     * @return bool
116
     */
117 1
    public function getIsProductDoc() {
118 1
        return true;
119
    }
120
}