Completed
Push — master ( 5cca73...7a2a08 )
by John
10s
created

ElementVirtualLinked_Controller::__call()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 7
nc 2
nop 2
1
<?php
2
3
/**
4
 * Virtual Linked Element.
5
 *
6
 * As elemental is based on widgets which have a natural has_one relation to an
7
 * object, this is a workaround for allowing the same element to be linked to
8
 * multiple pages.
9
 *
10
 * {@see ElementalGridFieldAddExistingAutocompleter}
11
 *
12
 * @package elemental
13
 */
14
class ElementVirtualLinked extends BaseElement {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
15
    /**
16
     * @var string
17
     */
18
    private static $title = 'Virtual linked Block';
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $title is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
19
20
    /**
21
     * @var string
22
     */
23
    private static $singular_name = 'Virtual linked Block';
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $singular_name is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
24
25
    private static $has_one = array(
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $has_one is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
26
        'LinkedElement' => 'BaseElement'
27
    );
28
29
    public function getTitle()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
30
    {
31
        return $this->LinkedElement()->getTitle();
0 ignored issues
show
Bug introduced by
The method LinkedElement() does not exist on ElementVirtualLinked. Did you maybe mean getVirtualLinkedElements()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
32
    }
33
34
    public function i18n_singular_name()
35
    {
36
        return _t(__CLASS__, $this->LinkedElement()->config()->title);
0 ignored issues
show
Bug introduced by
The method LinkedElement() does not exist on ElementVirtualLinked. Did you maybe mean getVirtualLinkedElements()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
37
    }
38
39
    public function __construct($record = null, $isSingleton = false, $model = null) {
40
        parent::__construct($record, $isSingleton, $model);
41
        $this->LinkedElement()->setVirtualOwner($this);
0 ignored issues
show
Bug introduced by
The method LinkedElement() does not exist on ElementVirtualLinked. Did you maybe mean getVirtualLinkedElements()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
42
    }
43
44
    public function getCMSFields() {
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
45
        $message = sprintf('<p>%s</p><p><a href="%2$s">%2$s</a></p>',
46
             _t('ElementVirtualLinked.DESCRIBE', 'This is a virtual copy of a block. To edit, visit'),
47
             $this->LinkedElement()->getEditLink()
0 ignored issues
show
Bug introduced by
The method LinkedElement() does not exist on ElementVirtualLinked. Did you maybe mean getVirtualLinkedElements()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
48
        );
49
50
        $fields = new FieldList(
51
            new TabSet('Root', $main = new Tab('Main'))
52
        );
53
54
        if ($this->isInvalidPublishState()) {
0 ignored issues
show
Documentation Bug introduced by
The method isInvalidPublishState does not exist on object<ElementVirtualLinked>? 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...
55
            $warning = 'Error: The original block is not published. This block will not work on the live site until you click the link below and publish it.';
56
            $main->push(new LiteralField('WarningHeader', '<p class="message error">' .$warning. '</p>'));
57
        }
58
        $main->push(new LiteralField('Existing', $message));
59
60
        $this->extend('updateCMSFields', $fields);
61
62
        return $fields;
63
    }
64
65
}
66
67
class ElementVirtualLinked_Controller extends BaseElement_Controller {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
68
69
    protected $controllerClass;
70
71
    public function __construct($widget) {
72
        parent::__construct($widget);
73
74
        $controllerClass = get_class($this->LinkedElement()) . '_Controller';
0 ignored issues
show
Documentation Bug introduced by
The method LinkedElement does not exist on object<ElementVirtualLinked_Controller>? 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...
75
        if(class_exists($controllerClass)) {
76
            $this->controllerClass = $controllerClass;
77
        } else {
78
            $this->controllerClass = 'BaseElement_Controller';
79
        }
80
    }
81
82
    /**
83
     * Returns the current widget in scope rendered into its' holder
84
     *
85
     * @return HTML
0 ignored issues
show
Documentation introduced by
Should the return type not be HTMLText?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
86
     */
87
    public function WidgetHolder()
88
    {
89
        return $this->renderWith("ElementHolder_VirtualLinked");
90
    }
91
92
    public function __call($method, $arguments) {
93
        try {
94
           $retVal = parent::__call($method, $arguments);
95
96
        } catch (Exception $e) {
97
            $controller = new $this->controllerClass($this->LinkedElement());
0 ignored issues
show
Documentation Bug introduced by
The method LinkedElement does not exist on object<ElementVirtualLinked_Controller>? 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...
98
            $retVal = call_user_func_array(array($controller, $method), $arguments);
99
        }
100
        return $retVal;
101
    }
102
103 View Code Duplication
    public function hasMethod($action) {
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
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...
104
        if(parent::hasMethod($action)) {
105
            return true;
106
        }
107
108
        $controller = new $this->controllerClass($this->LinkedElement());
0 ignored issues
show
Documentation Bug introduced by
The method LinkedElement does not exist on object<ElementVirtualLinked_Controller>? 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...
109
        return $controller->hasMethod($action);
110
    }
111
112 View Code Duplication
    public function hasAction($action) {
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
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...
113
        if(parent::hasAction($action)) {
114
            return true;
115
        }
116
117
        $controller = new $this->controllerClass($this->LinkedElement());
0 ignored issues
show
Documentation Bug introduced by
The method LinkedElement does not exist on object<ElementVirtualLinked_Controller>? 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...
118
        return $controller->hasAction($action);
119
    }
120
121 View Code Duplication
    public function checkAccessAction($action) {
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
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...
122
        if(parent::checkAccessAction($action)) {
123
            return true;
124
        }
125
126
        $controller = new $this->controllerClass($this->LinkedElement());
0 ignored issues
show
Documentation Bug introduced by
The method LinkedElement does not exist on object<ElementVirtualLinked_Controller>? 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...
127
        return $controller->checkAccessAction($action);
128
    }
129
130
    /**
131
     * Detect when a user has published a ElementVirtualLinked block
132
     * but has not published the LinkedElement block. 
133
     */
134
    public function isInvalidPublishState() {
135
        $block = $this->LinkedElement();
0 ignored issues
show
Documentation Bug introduced by
The method LinkedElement does not exist on object<ElementVirtualLinked_Controller>? 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...
136
        return (!$block->isPublished() && $this->isPublished());
0 ignored issues
show
Documentation Bug introduced by
The method isPublished does not exist on object<ElementVirtualLinked_Controller>? 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...
137
    }
138
139
    public function getCMSPublishedState() {
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
140
        if ($this->isInvalidPublishState()) {
141
            $colour = '#C00';
142
            $text = 'Error';
143
            $html = new HTMLText('PublishedState');
144
            $html->setValue(sprintf(
145
                '<span style="color: %s;">%s</span>',
146
                $colour,
147
                htmlentities($text)
148
            ));
149
            return $html;
150
        }
151
152
        $publishedState = null;
153
        foreach($this->extension_instances as $instance) {
154
            if (method_exists($instance, 'getCMSPublishedState')) {
155
                $instance->setOwner($this);
156
                $publishedState = $instance->getCMSPublishedState();
157
                $instance->clearOwner();
158
                break;
159
            }
160
        }
161
        return $publishedState;
162
    }
163
}
164