Passed
Pull Request — master (#221)
by Ingo
03:53
created

ElementalArea::getOwnerPage()   C

Complexity

Conditions 13
Paths 14

Size

Total Lines 46
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 13
eloc 26
nc 14
nop 0
dl 0
loc 46
rs 5.1118
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace DNADesign\Elemental\Models;
4
5
use DNADesign\Elemental\Extensions\ElementalAreasExtension;
6
use SilverStripe\CMS\Model\SiteTree;
7
use SilverStripe\Core\ClassInfo;
8
use SilverStripe\Core\Extensible;
9
use SilverStripe\Core\Injector\Injector;
10
use SilverStripe\ORM\ArrayList;
11
use SilverStripe\ORM\DataObject;
12
use SilverStripe\ORM\FieldType\DBField;
13
use SilverStripe\ORM\FieldType\DBHTMLText;
14
use SilverStripe\ORM\HasManyList;
15
use SilverStripe\Versioned\Versioned;
16
17
/**
18
 * Class ElementalArea
19
 * @package DNADesign\Elemental\Models
20
 *
21
 * @property string $OwnerClassName
22
 *
23
 * @method HasManyList|BaseElement[] Elements()
24
 */
25
class ElementalArea extends DataObject
26
{
27
    private static $db = [
0 ignored issues
show
introduced by
The private property $db is not used, and could be removed.
Loading history...
28
        'OwnerClassName' => 'Varchar(255)',
29
    ];
30
31
    private static $has_many = [
0 ignored issues
show
introduced by
The private property $has_many is not used, and could be removed.
Loading history...
32
        'Elements' => BaseElement::class,
33
    ];
34
35
    private static $extensions = [
0 ignored issues
show
introduced by
The private property $extensions is not used, and could be removed.
Loading history...
36
        Versioned::class,
37
    ];
38
39
    private static $owns = [
0 ignored issues
show
introduced by
The private property $owns is not used, and could be removed.
Loading history...
40
        'Elements',
41
    ];
42
43
    private static $cascade_deletes = [
0 ignored issues
show
introduced by
The private property $cascade_deletes is not used, and could be removed.
Loading history...
44
        'Elements',
45
    ];
46
47
    private static $cascade_duplicates = [
0 ignored issues
show
introduced by
The private property $cascade_duplicates is not used, and could be removed.
Loading history...
48
        'Elements',
49
    ];
50
51
    private static $summary_fields = [
0 ignored issues
show
introduced by
The private property $summary_fields is not used, and could be removed.
Loading history...
52
        'Title' => 'Title',
53
    ];
54
55
    private static $table_name = 'ElementalArea';
0 ignored issues
show
introduced by
The private property $table_name is not used, and could be removed.
Loading history...
56
57
    /**
58
     * @return array
59
     */
60
    public function supportedPageTypes()
61
    {
62
        $elementalClasses = [];
63
64
        foreach (ClassInfo::getValidSubClasses(SiteTree::class) as $class) {
65
            if (Extensible::has_extension($class, ElementalAreasExtension::class)) {
66
                $elementalClasses[] = $class;
67
            }
68
        }
69
70
        return $elementalClasses;
71
    }
72
73
    /**
74
     * @return DBHTMLText
75
     */
76
    public function forTemplate()
77
    {
78
        return $this->renderWith(static::class);
79
    }
80
81
    /**
82
     * Necessary to display results in CMS site search.
83
     *
84
     * @return DBField
85
     */
86
    public function Breadcrumbs()
87
    {
88
        $ownerClassName = $this->OwnerClassName;
89
90
        if ($owner = $ownerClassName::get()->filter('ElementalAreaID', $this->ID)->first()) {
91
            return DBField::create_field('HTMLText', sprintf(
92
                '<a href="%s">%s</a>',
93
                $owner->CMSEditLink(),
94
                $owner->Title
95
            ));
96
        }
97
98
        return null;
99
    }
100
101
    /**
102
     * Used in template instead of {@link Elements()} to wrap each element in
103
     * its' controller, making it easier to access and process form logic and
104
     * actions stored in {@link ElementController}.
105
     *
106
     * @return ArrayList
107
     * @throws \Exception
108
     */
109
    public function ElementControllers()
110
    {
111
        $controllers = new ArrayList();
112
        $items = $this->Elements();
113
114
        if (!is_null($items)) {
0 ignored issues
show
introduced by
The condition is_null($items) is always false.
Loading history...
115
            foreach ($items as $element) {
116
                $controller = $element->getController();
117
                $controllers->push($controller);
118
            }
119
        }
120
121
        return $controllers;
122
    }
123
124
    /**
125
     * @return null|DataObject
126
     * @throws \Psr\Container\NotFoundExceptionInterface
127
     * @throws \SilverStripe\ORM\ValidationException
128
     */
129
    public function getOwnerPage()
130
    {
131
        if ($this->OwnerClassName) {
132
            $class = $this->OwnerClassName;
133
            $instance = Injector::inst()->get($class);
134
            if (!ClassInfo::hasMethod($instance, 'getElementalRelations')) {
135
                return null;
136
            }
137
            $elementalAreaRelations = $instance->getElementalRelations();
138
139
            foreach ($elementalAreaRelations as $eaRelationship) {
140
                $areaID = $eaRelationship . 'ID';
141
142
                $currentStage = Versioned::get_stage() ?: Versioned::DRAFT;
143
                $page = Versioned::get_by_stage($class, $currentStage)->filter($areaID, $this->ID);
144
145
146
                if ($page && $page->exists()) {
147
                    return $page->first();
148
                }
149
            }
150
        }
151
152
        foreach ($this->supportedPageTypes() as $class) {
153
            $instance = Injector::inst()->get($class);
154
            if (!ClassInfo::hasMethod($instance, 'getElementalRelations')) {
155
                return null;
156
            }
157
            $elementalAreaRelations = $instance->getElementalRelations();
158
159
            foreach ($elementalAreaRelations as $eaRelationship) {
160
                $areaID = $eaRelationship . 'ID';
161
                $page = Versioned::get_by_stage($class, Versioned::DRAFT)->filter($areaID, $this->ID);
162
163
                if ($page && $page->exists()) {
164
                    if ($this->OwnerClassName !== $class) {
165
                        $this->OwnerClassName = $class;
166
                        $this->write();
167
                    }
168
169
                    return $page->first();
170
                }
171
            }
172
        }
173
174
        return null;
175
    }
176
177
    /**
178
     * @param null $member
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $member is correct as it would always require null to be passed?
Loading history...
179
     * @return bool
180
     * @throws \Psr\Container\NotFoundExceptionInterface
181
     * @throws \SilverStripe\ORM\ValidationException
182
     */
183
    public function canEdit($member = null)
184
    {
185
        if (parent::canEdit($member)) {
186
            return true;
187
        }
188
189
        $ownerPage = $this->getOwnerPage();
190
        if ($ownerPage !== null) {
191
            return $this->getOwnerPage()->canEdit($member);
192
        }
193
194
        return false;
195
    }
196
197
    /**
198
     * @param null $member
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $member is correct as it would always require null to be passed?
Loading history...
199
     * @return bool
200
     * @throws \Psr\Container\NotFoundExceptionInterface
201
     * @throws \SilverStripe\ORM\ValidationException
202
     */
203
    public function canView($member = null)
204
    {
205
        if (parent::canEdit($member)) {
206
            return true;
207
        }
208
209
        $ownerPage = $this->getOwnerPage();
210
        if ($ownerPage !== null) {
211
            return $this->getOwnerPage()->canView($member);
212
        }
213
214
        return false;
215
    }
216
}
217