Completed
Push — master ( 0ebf95...33622c )
by Damian
12s
created

SiteConfigSubsites   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 60
Duplicated Lines 5 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 3
loc 60
rs 10
wmc 13

4 Methods

Rating   Name   Duplication   Size   Complexity  
C augmentSQL() 0 29 7
A onBeforeWrite() 2 4 4
A updateCMSFields() 0 3 1
A cacheKeyComponent() 0 3 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace SilverStripe\Subsites\Extensions;
4
5
use SilverStripe\Forms\FieldList;
6
use SilverStripe\Forms\HiddenField;
7
use SilverStripe\ORM\DataExtension;
8
use SilverStripe\ORM\DataQuery;
9
use SilverStripe\ORM\Queries\SQLSelect;
10
use SilverStripe\SiteConfig\SiteConfig;
11
use SilverStripe\Subsites\Model\Subsite;
12
use SilverStripe\Subsites\State\SubsiteState;
13
14
/**
15
 * Extension for the SiteConfig object to add subsites support
16
 */
17
class SiteConfigSubsites extends DataExtension
18
{
19
    private static $has_one = [
0 ignored issues
show
introduced by
The private property $has_one is not used, and could be removed.
Loading history...
20
        'Subsite' => Subsite::class, // The subsite that this page belongs to
21
    ];
22
23
    /**
24
     * Update any requests to limit the results to the current site
25
     * @param SQLSelect $query
26
     * @param DataQuery|null $dataQuery
27
     */
28
    public function augmentSQL(SQLSelect $query, DataQuery $dataQuery = null)
29
    {
30
        if (Subsite::$disable_subsite_filter) {
31
            return;
32
        }
33
34
        // If you're querying by ID, ignore the sub-site - this is a bit ugly...
35
        if ($query->filtersOnID()) {
36
            return;
37
        }
38
        $regexp = '/^(.*\.)?("|`)?SubsiteID("|`)?\s?=/';
39
        foreach ($query->getWhereParameterised($parameters) as $predicate) {
40
            if (preg_match($regexp, $predicate)) {
41
                return;
42
            }
43
        }
44
45
        $subsiteID = SubsiteState::singleton()->getSubsiteId();
46
        if ($subsiteID === null) {
47
            return;
48
        }
49
50
        $froms = $query->getFrom();
51
        $froms = array_keys($froms);
52
        $tableName = array_shift($froms);
53
        if ($tableName !== SiteConfig::getSchema()->tableName(SiteConfig::class)) {
54
            return;
55
        }
56
        $query->addWhere("\"$tableName\".\"SubsiteID\" IN ($subsiteID)");
57
    }
58
59
    public function onBeforeWrite()
60
    {
61 View Code Duplication
        if ((!is_numeric($this->owner->ID) || !$this->owner->ID) && !$this->owner->SubsiteID) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
62
            $this->owner->SubsiteID = SubsiteState::singleton()->getSubsiteId();
63
        }
64
    }
65
66
    /**
67
     * Return a piece of text to keep DataObject cache keys appropriately specific
68
     */
69
    public function cacheKeyComponent()
70
    {
71
        return 'subsite-' . SubsiteState::singleton()->getSubsiteId();
72
    }
73
74
    public function updateCMSFields(FieldList $fields)
75
    {
76
        $fields->push(HiddenField::create('SubsiteID', 'SubsiteID', SubsiteState::singleton()->getSubsiteId()));
0 ignored issues
show
Bug introduced by
'SubsiteID' of type string is incompatible with the type array expected by parameter $args of SilverStripe\View\ViewableData::create(). ( Ignorable by Annotation )

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

76
        $fields->push(HiddenField::create(/** @scrutinizer ignore-type */ 'SubsiteID', 'SubsiteID', SubsiteState::singleton()->getSubsiteId()));
Loading history...
77
    }
78
}
79