Completed
Push — master ( 793b46...adfa72 )
by Damian
12s
created

ErrorPageSubsite::updateErrorFilename()   C

Complexity

Conditions 7
Paths 12

Size

Total Lines 36
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 36
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 20
nc 12
nop 2
1
<?php
2
3
namespace SilverStripe\Subsites\Extensions;
4
5
use SilverStripe\CMS\Model\SiteTree;
6
use SilverStripe\Core\Config\Config;
7
use SilverStripe\ORM\DataExtension;
8
use SilverStripe\ORM\DataObject;
9
use SilverStripe\Subsites\Model\Subsite;
10
11
class ErrorPageSubsite extends DataExtension
12
{
13
    /**
14
     * Alter file path to generated a static (static) error page file to handle error page template
15
     * on different sub-sites
16
     *
17
     * @see ErrorPage::get_error_filename()
18
     *
19
     * FIXME since {@link Subsite::currentSubsite()} partly relies on Session, viewing other sub-site (including
20
     * main site) between opening ErrorPage in the CMS and publish ErrorPage causes static error page to get
21
     * generated incorrectly.
22
     *
23
     * @param string $name
24
     * @param int $statusCode
25
     */
26
    public function updateErrorFilename(&$name, &$statusCode)
27
    {
28
        $static_filepath = Config::inst()->get($this->owner->ClassName, 'static_filepath');
29
        $subdomainPart = '';
30
31
        // Try to get current subsite from session
32
        $subsite = Subsite::currentSubsite();
33
34
        // since this function is called from Page class before the controller is created, we have
35
        // to get subsite from domain instead
36
        if (!$subsite) {
37
            $subsiteID = Subsite::getSubsiteIDForDomain();
38
            if ($subsiteID != 0) {
39
                $subsite = DataObject::get_by_id(Subsite::class, $subsiteID);
40
            } else {
41
                $subsite = null;
42
            }
43
        }
44
45
        if ($subsite) {
46
            $subdomain = $subsite->domain();
47
            $subdomainPart = "-{$subdomain}";
48
        }
49
50
        // @todo implement Translatable namespace
51
        if (singleton(SiteTree::class)->hasExtension('Translatable')
52
            && $locale
53
            && $locale != Translatable::default_locale()
54
        ) {
55
            $filepath = $static_filepath . "/error-{$statusCode}-{$locale}{$subdomainPart}.html";
0 ignored issues
show
Bug introduced by
The variable $locale does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
56
        } else {
57
            $filepath = $static_filepath . "/error-{$statusCode}{$subdomainPart}.html";
58
        }
59
60
        $name = $filepath;
61
    }
62
}
63