updateUsageDataObject()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 7
c 1
b 0
f 0
nc 4
nop 1
dl 0
loc 11
rs 10
1
<?php
2
3
namespace DNADesign\Elemental\Extensions;
4
5
use SilverStripe\Admin\Forms\UsedOnTable;
6
use SilverStripe\CMS\Model\SiteTree;
7
use SilverStripe\ORM\DataExtension;
8
use SilverStripe\ORM\ArrayList;
9
use SilverStripe\ORM\DataObject;
10
use SilverStripe\ORM\ValidationException;
11
use DNADesign\Elemental\Models\ElementalArea;
12
use DNADesign\Elemental\Models\BaseElement;
13
14
class ElementalAreaUsedOnTableExtension extends DataExtension
15
{
16
17
    /**
18
     * Hides ElementalArea's from the "Used On" tab when viewing files
19
     *
20
     * @var array $excludedClasses
21
     */
22
    public function updateUsageExcludedClasses(array &$excludedClasses)
23
    {
24
        $excludedClasses[] = ElementalArea::class;
25
    }
26
27
    /**
28
     * Legacy function kept for semver, replaced with updateUsageExcludedClasses above
29
     *
30
     * @return void
31
     * @var ArrayList $usage
32
     * @var DataObject $record
33
     * @see UsedOnTable::updateUsage
34
     * @deprecated 4.5.0 Use self::updateUsageExcludedClasses instead
35
     */
36
    public function updateUsage(ArrayList &$usage, DataObject &$record)
0 ignored issues
show
Unused Code introduced by
The parameter $usage is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

36
    public function updateUsage(/** @scrutinizer ignore-unused */ ArrayList &$usage, DataObject &$record)

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

Loading history...
Unused Code introduced by
The parameter $record is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

36
    public function updateUsage(ArrayList &$usage, /** @scrutinizer ignore-unused */ DataObject &$record)

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

Loading history...
37
    {
38
        // noop
39
    }
40
41
    /**
42
     * Exclude content blocks that aren't linked to a page
43
     *
44
     * @param bool $excludeDataObject
45
     * @param DataObject $dataObject|null
46
     */
47
    public function updateUsageDataObject(?DataObject &$dataObject)
48
    {
49
        if (!($dataObject instanceof BaseElement)) {
50
            return;
51
        }
52
        try {
53
            if (!$dataObject->getPage()) {
54
                $dataObject = null;
55
            }
56
        } catch (ValidationException $e) {
57
            $dataObject = null;
58
        }
59
    }
60
61
    /**
62
     * Link BaseElement's to their parent page
63
     *
64
     * @param array $ancestorDataObjects
65
     * @param DataObject $dataObject
66
     */
67
    public function updateUsageAncestorDataObjects(array &$ancestorDataObjects, DataObject $dataObject)
68
    {
69
        if (!($dataObject instanceof BaseElement)) {
70
            return;
71
        }
72
        try {
73
            // BaseElement::getPage() caches results so there's no performance decrease from
74
            // also calling it in updateUsageExcludeDataObject()
75
            /** @var SiteTree $page */
76
            if ($page = $dataObject->getPage()) {
77
                $ancestorDataObjects[] = $page;
78
            }
79
        } catch (ValidationException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
80
        }
81
    }
82
}
83