PageDomainSwitch::main()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 34
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 18
c 1
b 0
f 0
dl 0
loc 34
rs 9.6666
cc 4
nc 4
nop 0
1
<?php
2
/******************************************************************************
3
 * Wikipedia Account Creation Assistance tool                                 *
4
 *                                                                            *
5
 * All code in this file is released into the public domain by the ACC        *
6
 * Development Team. Please see team.json for a list of contributors.         *
7
 ******************************************************************************/
8
9
namespace Waca\Pages;
10
11
use Waca\DataObjects\Domain;
12
use Waca\DataObjects\User;
13
use Waca\Router\RequestRouter;
14
use Waca\Tasks\InternalPageBase;
15
use Waca\WebRequest;
16
17
class PageDomainSwitch extends InternalPageBase
18
{
19
    /**
20
     * @inheritDoc
21
     */
22
    protected function main()
23
    {
24
        if (!WebRequest::wasPosted()) {
25
            $this->redirect('/');
26
27
            return;
28
        }
29
30
        $database = $this->getDatabase();
31
        $currentUser = User::getCurrent($database);
32
33
        /** @var Domain|false $newDomain */
34
        $newDomain = Domain::getById(WebRequest::postInt('newdomain'), $database);
35
36
        if ($newDomain === false) {
0 ignored issues
show
introduced by
The condition $newDomain === false is always false.
Loading history...
37
            $this->redirect('/');
38
39
            return;
40
        }
41
42
        $this->getDomainAccessManager()->switchDomain($currentUser, $newDomain);
43
44
        // try to stay on the same page if possible.
45
        // This only checks basic ACLs and not domain privileges, so this may still result in a 403.
46
47
        $referrer = WebRequest::postString('referrer');
48
        $priorPath = explode('/', $referrer);
0 ignored issues
show
Bug introduced by
It seems like $referrer can also be of type null; however, parameter $string of explode() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

48
        $priorPath = explode('/', /** @scrutinizer ignore-type */ $referrer);
Loading history...
49
        $router = new RequestRouter();
50
        $route = $router->getRouteFromPath($priorPath);
51
52
        if ($this->barrierTest($route[1], $currentUser, $route[0])) {
53
            $this->redirect('/' . $referrer);
54
        } else {
55
            $this->redirect('/');
56
        }
57
    }
58
}