Completed
Pull Request — master (#34)
by Jason
05:39
created

FormBlock   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Test Coverage

Coverage 6.67%

Importance

Changes 0
Metric Value
wmc 10
lcom 0
cbo 4
dl 0
loc 88
ccs 2
cts 30
cp 0.0667
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A BlockForm() 0 12 4
A canCreate() 0 7 2
A canView() 0 7 2
A getCMSFields() 0 18 2
1
<?php
2
3
class FormBlock extends Block
4
{
5
    /**
6
     * @var string
7
     */
8
    private static $singular_name = 'Form Block';
9
10
    /**
11
     * @var string
12
     */
13
    private static $plural_name = 'Form Blocks';
14
15
    /**
16
     * @var array
17
     */
18
    private static $db = array(
19
        'Content' => 'HTMLText',
20
    );
21
22
    /**
23
     * @var array
24
     */
25
    private static $has_one = array(
26
        'Form' => 'UserDefinedForm',
27
    );
28
29
    /**
30
     * @return FieldList
31
     */
32 1
    public function getCMSFields()
33
    {
34 1
        $fields = singleton('Block')->getCMSFields();
35
36
        if (class_exists('UserDefinedForm')) {
37
            $fields->addFieldToTab('Root.Main', DropdownField::create(
38
                'FormID',
39
                'Form',
40
                UserDefinedForm::get()->map()
41
                )->setEmptyString('')
42
                ->setDescription('select an existing User Defined Form to display')
43
            );
44
        }
45
46
        $fields->addFieldToTab('Root.Main', HtmlEditorField::create('Content'));
47
48
        return $fields;
49
    }
50
51
    /**
52
     * @return Forms|HTMLText
53
     */
54
    public function BlockForm()
55
    {
56
        if ($this->Form()->exists()) {
0 ignored issues
show
Documentation Bug introduced by
The method Form does not exist on object<FormBlock>? 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...
57
            $controller = new UserDefinedForm_Controller($this->Form());
0 ignored issues
show
Documentation Bug introduced by
The method Form does not exist on object<FormBlock>? 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...
58
            $current = Controller::curr();
59
            if ($current && $current->getAction() == 'finished') {
60
                return $controller->renderWith('ReceivedFormSubmission');
61
            }
62
            $form = $controller->Form();
63
            return $form;
64
        }
65
    }
66
67
    /**
68
     * @param null $member
69
     * @return bool
70
     */
71
    public function canCreate($member = null)
72
    {
73
        if (!class_exists('UserDefinedForm')) {
74
            return false;
75
        }
76
        return parent::canCreate();
77
    }
78
79
    /**
80
     * @param null $member
81
     * @return bool
82
     */
83
    public function canView($member = null)
84
    {
85
        if (!class_exists('UserDefinedForm')) {
86
            return false;
87
        }
88
        return parent::canView();
89
    }
90
}