Failed Conditions
Push — multiproject/domainswitch ( 54b9a3 )
by Simon
04:31
created

DomainAccessManager::switchDomain()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 7
dl 0
loc 13
ccs 0
cts 11
cp 0
rs 10
c 1
b 0
f 0
cc 2
nc 2
nop 2
crap 6
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\Security;
10
11
use Waca\DataObject;
12
use Waca\DataObjects\Domain;
13
use Waca\DataObjects\User;
14
use Waca\Exceptions\AccessDeniedException;
15
use Waca\WebRequest;
16
17
class DomainAccessManager
18
{
19
    /**
20
     * @param User $user
21
     *
22
     * @return Domain[]
23
     */
24
    public function getAllowedDomains(User $user): array
25
    {
26
        if ($user->isCommunityUser()) {
27
            return [];
28
        }
29
30
        return Domain::getDomainByUser($user->getDatabase(), $user, true);
31
    }
32
33
    public function switchDomain(User $user, Domain $newDomain): void
34
    {
35
        $mapToId = function(DataObject $object) {
36
            return $object->getId();
37
        };
38
39
        $allowed = in_array($newDomain->getId(), array_map($mapToId, self::getAllowedDomains($user)));
0 ignored issues
show
Bug Best Practice introduced by
The method Waca\Security\DomainAcce...er::getAllowedDomains() is not static, but was called statically. ( Ignorable by Annotation )

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

39
        $allowed = in_array($newDomain->getId(), array_map($mapToId, self::/** @scrutinizer ignore-call */ getAllowedDomains($user)));
Loading history...
40
41
        if ($allowed) {
42
            WebRequest::setActiveDomain($newDomain);
43
        }
44
        else {
45
            throw new AccessDeniedException();
46
        }
47
    }
48
}