Completed
Push — master ( f85faa...6214aa )
by Jason
10s
created

PromoBlock::canCreate()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 15.5468

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 2
cts 8
cp 0.25
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 7
nc 4
nop 1
crap 15.5468
1
<?php
2
3
class PromoBlock extends Block
4
{
5
    /**
6
     * @return string
7
     */
8 6
    public function singular_name()
9
    {
10 6
        return _t('PromoBlock.SINGULARNAME', 'Promo Block');
11
    }
12
13
    /**
14
     * @return string
15
     */
16
    public function plural_name()
17
    {
18
        return _t('PromoBlock.PLURALNAME', 'Promo Blocks');
19
    }
20
21
    /**
22
     * @var array
23
     */
24
    private static $many_many = array(
25
        'Promos' => 'PromoObject',
26
    );
27
28
    /**
29
     * @var array
30
     */
31
    private static $many_many_extraFields = array(
32
        'Promos' => array(
33
            'SortOrder' => 'Int',
34
        ),
35
    );
36
37
    /**
38
     * @return FieldList
39
     */
40 1
    public function getCMSFields()
41
    {
42 1
        $fields = parent::getCMSFields();
43
44 1
        if ($this->ID) {
45 1
            $config = GridFieldConfig_RelationEditor::create();
46 1
            if (class_exists('GridFieldSortableRows')) {
47
                $config->addComponent(new GridFieldSortableRows('SortOrder'));
48
            }
49 1
            if (class_exists('GridFieldAddExistingSearchButton')) {
50 1
                $config->removeComponentsByType('GridFieldAddExistingAutocompleter');
51 1
                $config->addComponent(new GridFieldAddExistingSearchButton());
52 1
            }
53 1
            $promos = $this->Promos()->sort('SortOrder');
0 ignored issues
show
Documentation Bug introduced by
The method Promos does not exist on object<PromoBlock>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
54 1
            $promoField = GridField::create('Promos', 'Promos', $promos, $config);
55
56 1
            $fields->addFieldsToTab('Root.Main', array(
57 1
                $promoField,
58 1
            ));
59 1
        }
60
61 1
        return $fields;
62
    }
63
64
    /**
65
     * @return mixed
66
     */
67 1
    public function getPromoList()
68
    {
69 1
        return $this->Promos()->sort('SortOrder');
0 ignored issues
show
Documentation Bug introduced by
The method Promos does not exist on object<PromoBlock>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
70
    }
71
72
    /**
73
     * @param null $member
74
     * @return bool|null
75
     */
76 1
    public function canCreate($member = null)
77
    {
78
        if (!$member || !(is_a($member, 'Member')) || is_numeric($member)) {
79
            $member = Member::currentUserID();
80
        }
81
82
        // Standard mechanism for accepting permission changes from extensions
83
        $extended = $this->extendedCan('canCreate', $member);
84
        if ($extended !== null) {
85 1
            return $extended;
86
        }
87
88
        return parent::canCreate($member);
89
    }
90
91
    /**
92
     * @param null $member
93
     * @return bool|null
94
     */
95
    public function canView($member = null)
96
    {
97
        if (!$member || !(is_a($member, 'Member')) || is_numeric($member)) {
98
            $member = Member::currentUserID();
99
        }
100
101
        // Standard mechanism for accepting permission changes from extensions
102
        $extended = $this->extendedCan('canView', $member);
103
        if ($extended !== null) {
104
            return $extended;
105
        }
106
107
        return parent::canView($member);
108
    }
109
}