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

DomainAccessManager   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 11
dl 0
loc 29
ccs 0
cts 18
cp 0
rs 10
c 1
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getAllowedDomains() 0 7 2
A switchDomain() 0 13 2
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
}