Passed
Push — master ( a1de92...1a465b )
by Robbie
03:52
created

handleElement()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 31
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 17
nc 5
nop 0
dl 0
loc 31
rs 9.3888
c 0
b 0
f 0
1
<?php
2
3
namespace DNADesign\Elemental\Extensions;
4
5
use DNADesign\Elemental\Models\ElementalArea;
6
use DNADesign\Elemental\Extensions\ElementalAreasExtension;
7
use SilverStripe\Core\Extension;
8
9
class ElementalContentControllerExtension extends Extension
10
{
11
    /**
12
     * @var array
13
     */
14
    private static $allowed_actions = array(
0 ignored issues
show
introduced by
The private property $allowed_actions is not used, and could be removed.
Loading history...
15
        'handleElement'
16
    );
17
18
    public function handleElement()
19
    {
20
        $id = $this->owner->getRequest()->param('ID');
21
22
        if (!$id) {
23
            user_error('No element ID supplied', E_USER_ERROR);
24
            return false;
25
        }
26
27
        /** @var SiteTree $elementOwner */
28
        $elementOwner = $this->owner->data();
29
30
        $elementalAreaRelations = $this->owner->getElementalRelations();
31
32
        if (!$elementalAreaRelations) {
33
            user_error(get_class($this->owner) . ' has no ElementalArea relationships', E_USER_ERROR);
34
            return false;
35
        }
36
37
        foreach ($elementalAreaRelations as $elementalAreaRelation) {
38
            $element = $elementOwner->$elementalAreaRelation()->Elements()
39
                ->filter('ID', $id)
40
                ->First();
41
42
            if ($element) {
43
                return $element->getController();
44
            }
45
        }
46
47
        user_error('Element $id not found for this page', E_USER_ERROR);
48
        return false;
49
    }
50
}
51