Completed
Push — master ( 961436...7b1539 )
by Will
11s
created

ElementVirtualLinked   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 109
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 6

Importance

Changes 0
Metric Value
wmc 14
lcom 2
cbo 6
dl 0
loc 109
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getTitle() 0 4 1
A i18n_singular_name() 0 4 1
A __construct() 0 5 1
A getCMSFields() 0 22 2
A isInvalidPublishState() 0 5 2
B getCMSPublishedState() 0 25 4
A getAnchor() 0 10 3
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
    /**
17
     * @var string
18
     */
19
    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...
20
21
    /**
22
     * @var string
23
     */
24
    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...
25
26
    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...
27
        'LinkedElement' => 'BaseElement'
28
    );
29
30
    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...
31
    {
32
        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...
33
    }
34
35
    public function i18n_singular_name()
36
    {
37
        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...
38
    }
39
40
    public function __construct($record = null, $isSingleton = false, $model = null)
41
    {
42
        parent::__construct($record, $isSingleton, $model);
43
        $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...
44
    }
45
46
    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...
47
    {
48
        $message = sprintf(
49
            '<p>%s</p><p><a href="%2$s">%2$s</a></p>',
50
            _t('ElementVirtualLinked.DESCRIBE', 'This is a virtual copy of a block. To edit, visit'),
51
            $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...
52
        );
53
54
        $fields = new FieldList(
55
            new TabSet('Root', $main = new Tab('Main'))
56
        );
57
58
        if ($this->isInvalidPublishState()) {
59
            $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.';
60
            $main->push(new LiteralField('WarningHeader', '<p class="message error">' .$warning. '</p>'));
61
        }
62
        $main->push(new LiteralField('Existing', $message));
63
64
        $this->extend('updateCMSFields', $fields);
65
66
        return $fields;
67
    }
68
69
70
    /**
71
     * Detect when a user has published a ElementVirtualLinked block
72
     * but has not published the LinkedElement block.
73
     */
74
    public function isInvalidPublishState()
75
    {
76
        $block = $this->LinkedElement();
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...
77
        return (!$block->isPublished() && $this->isPublished());
0 ignored issues
show
Documentation Bug introduced by
The method isPublished 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...
78
    }
79
80
    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...
81
    {
82
        if ($this->isInvalidPublishState()) {
83
            $colour = '#C00';
84
            $text = 'Error';
85
            $html = new HTMLText('PublishedState');
86
            $html->setValue(sprintf(
87
                '<span style="color: %s;">%s</span>',
88
                $colour,
89
                htmlentities($text)
90
            ));
91
            return $html;
92
        }
93
94
        $publishedState = null;
95
        foreach ($this->extension_instances as $instance) {
96
            if (method_exists($instance, 'getCMSPublishedState')) {
97
                $instance->setOwner($this);
98
                $publishedState = $instance->getCMSPublishedState();
99
                $instance->clearOwner();
100
                break;
101
            }
102
        }
103
        return $publishedState;
104
    }
105
106
107
    /**
108
     * Get a unique anchor name
109
     *
110
     * @return string
111
     */
112
    public function getAnchor() {
113
        $linkedElement = $this->LinkedElement();
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...
114
115
        if ($linkedElement && $linkedElement->exists()) {
116
            return $linkedElement->getAnchor();
117
        }
118
119
        // generic fallback
120
        return 'e'.$this->ID;
121
    }
122
}
123
124
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...
125
{
126
127
    protected $controllerClass;
128
129
    public function __construct($widget)
130
    {
131
        parent::__construct($widget);
132
133
        $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...
134
        if (class_exists($controllerClass)) {
135
            $this->controllerClass = $controllerClass;
136
        } else {
137
            $this->controllerClass = 'BaseElement_Controller';
138
        }
139
    }
140
141
    /**
142
     * Returns the current widget in scope rendered into its' holder
143
     *
144
     * @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...
145
     */
146
    public function WidgetHolder()
147
    {
148
        return $this->renderWith("ElementHolder_VirtualLinked");
149
    }
150
151
    public function __call($method, $arguments)
152
    {
153
        try {
154
            $retVal = parent::__call($method, $arguments);
155
        } catch (Exception $e) {
156
            $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...
157
            $retVal = call_user_func_array(array($controller, $method), $arguments);
158
        }
159
        return $retVal;
160
    }
161
162 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...
163
    {
164
        if (parent::hasMethod($action)) {
165
            return true;
166
        }
167
168
        $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...
169
        return $controller->hasMethod($action);
170
    }
171
172 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...
173
    {
174
        if (parent::hasAction($action)) {
175
            return true;
176
        }
177
178
        $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...
179
        return $controller->hasAction($action);
180
    }
181
182 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...
183
    {
184
        if (parent::checkAccessAction($action)) {
185
            return true;
186
        }
187
188
        $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...
189
        return $controller->checkAccessAction($action);
190
    }
191
}
192