Completed
Pull Request — master (#62)
by Jason
15:39
created

FormBlock::BlockForm()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 0
cts 6
cp 0
rs 9.2
c 0
b 0
f 0
cc 4
eloc 8
nc 3
nop 0
crap 20
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 11 and the first side effect is on line 8.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
3 1
namespace Dynamic\DynamicBlocks\Block;
4 1
5
use SheaDawson\Blocks\Model\Block;
6
7
if (!class_exists(UserDefinedForm::class)) {
8
    return;
9
}
10
11
class FormBlock extends Block
12
{
13
    /**
14
     * @var string
15
     */
16
    private static $singular_name = 'Form Block';
17
18
    /**
19
     * @var string
20
     */
21
    private static $plural_name = 'Form Blocks';
22
23
    /**
24
     * @var array
25
     */
26
    private static $db = array(
27
        'Content' => 'HTMLText',
28
    );
29
30
    /**
31
     * @var array
32
     */
33
    private static $has_one = array(
34
        'Form' => 'UserDefinedForm',
35
    );
36 1
37
    /**
38 1
     * @return FieldList
39
     */
40 1 View Code Duplication
    public function getCMSFields()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
41
    {
42
        $fields = singleton('Block')->getCMSFields();
43
44
        if (class_exists('UserDefinedForm')) {
45
            $fields->addFieldToTab('Root.Main', DropdownField::create(
46
                'FormID',
47
                'Form',
48
                UserDefinedForm::get()->map()
49
                )->setEmptyString('')
50 1
                ->setDescription('select an existing User Defined Form to display')
51
            );
52 1
        }
53
54
        $fields->addFieldToTab('Root.Main', HtmlEditorField::create('Content'));
55
56
        return $fields;
57
    }
58
59
    /**
60
     * @return Forms|HTMLText
61
     */
62
    public function BlockForm()
63
    {
64
        if ($this->Form()->exists()) {
0 ignored issues
show
Documentation Bug introduced by
The method Form does not exist on object<Dynamic\DynamicBlocks\Block\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...
65
            $controller = new UserDefinedForm_Controller($this->Form());
0 ignored issues
show
Documentation Bug introduced by
The method Form does not exist on object<Dynamic\DynamicBlocks\Block\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...
66
            $current = Controller::curr();
67
            if ($current && $current->getAction() == 'finished') {
68
                return $controller->renderWith('ReceivedFormSubmission');
69
            }
70
            $form = $controller->Form();
71
            return $form;
72
        }
73
    }
74
75 1
    /**
76
     * @param null $member
77 1
     * @param array $context
78 1
     * @return bool
79
     */
80
    public function canCreate($member = NULL, $context = [])
81
    {
82
        if (!class_exists('UserDefinedForm')) {
83
            return false;
84
        }
85
        return parent::canCreate();
86
    }
87 1
88
    /**
89 1
     * @param null $member
90 1
     * @param array $context
91
     * @return bool
92
     */
93
    public function canView($member = NULL, $context = [])
0 ignored issues
show
Unused Code introduced by
The parameter $context is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
94
    {
95
        if (!class_exists('UserDefinedForm')) {
96
            return false;
97
        }
98
        return parent::canView();
99
    }
100
}