Passed
Pull Request — master (#326)
by Robbie
04:32
created

InitStateMiddleware   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 84
rs 10
wmc 11

3 Methods

Rating   Name   Duplication   Size   Complexity  
A process() 0 23 3
A getIsAdmin() 0 11 3
B detectSubsiteId() 0 17 5
1
<?php
2
3
namespace SilverStripe\Subsites\Middleware;
4
5
use SilverStripe\Admin\AdminRootController;
6
use SilverStripe\Control\HTTPRequest;
7
use SilverStripe\Control\Middleware\HTTPMiddleware;
8
use SilverStripe\Core\Config\Configurable;
9
use SilverStripe\Core\Injector\Injector;
10
use SilverStripe\ORM\Connect\DatabaseException;
11
use SilverStripe\Subsites\Model\Subsite;
12
use SilverStripe\Subsites\State\SubsiteState;
13
14
class InitStateMiddleware implements HTTPMiddleware
15
{
16
    use Configurable;
17
18
    /**
19
     * URL paths that should be considered as admin only, i.e. not frontend
20
     *
21
     * @config
22
     * @var array
23
     */
24
    private static $admin_url_paths = [
0 ignored issues
show
introduced by
The private property $admin_url_paths is not used, and could be removed.
Loading history...
25
        'dev/',
26
        'graphql/',
27
    ];
28
29
    public function process(HTTPRequest $request, callable $delegate)
30
    {
31
        try {
32
            // Initialise and register the State
33
            $state = SubsiteState::create();
34
            Injector::inst()->registerService($state);
35
36
            // Detect whether the request was made in the CMS area (or other admin-only areas)
37
            $isAdmin = $this->getIsAdmin($request);
38
            $state->setUseSessions($isAdmin);
39
40
            // Detect the subsite ID
41
            $subsiteId = $this->detectSubsiteId($request);
42
            $state->setSubsiteId($subsiteId);
43
44
            return $delegate($request);
45
        } catch (DatabaseException $ex) {
46
            // Database is not ready
47
            return $delegate($request);
48
        } finally {
49
            // Persist to the session if using the CMS
50
            if ($state->getUseSessions()) {
51
                $request->getSession()->set('SubsiteID', $state->getSubsiteId());
52
            }
53
        }
54
    }
55
56
    /**
57
     * Determine whether the website is being viewed from an admin protected area or not
58
     *
59
     * @param  HTTPRequest $request
60
     * @return bool
61
     */
62
    public function getIsAdmin(HTTPRequest $request)
63
    {
64
        $adminPaths = static::config()->get('admin_url_paths');
65
        $adminPaths[] = AdminRootController::config()->get('url_base') . '/';
66
        $currentPath = rtrim($request->getURL(), '/') . '/';
67
        foreach ($adminPaths as $adminPath) {
68
            if (substr($currentPath, 0, strlen($adminPath)) === $adminPath) {
69
                return true;
70
            }
71
        }
72
        return false;
73
    }
74
75
    /**
76
     * Use the given request to detect the current subsite ID
77
     *
78
     * @param  HTTPRequest $request
79
     * @return int
80
     */
81
    protected function detectSubsiteId(HTTPRequest $request)
82
    {
83
        if ($request->getVar('SubsiteID') !== null) {
84
            return (int) $request->getVar('SubsiteID');
85
        }
86
87
        if (SubsiteState::singleton()->getUseSessions() && $request->getSession()->get('SubsiteID') !== null) {
88
            return (int) $request->getSession()->get('SubsiteID');
89
        }
90
91
        $subsiteIdFromDomain = Subsite::getSubsiteIDForDomain();
92
        if ($subsiteIdFromDomain !== null) {
93
            return (int) $subsiteIdFromDomain;
94
        }
95
96
        // Default fallback
97
        return 0;
98
    }
99
}
100