UserServiceProviderResolver::getServices()   A
last analyzed

Complexity

Conditions 6
Paths 13

Size

Total Lines 36
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 9.0146

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 16
c 1
b 0
f 0
nc 13
nop 1
dl 0
loc 36
ccs 9
cts 16
cp 0.5625
crap 9.0146
rs 9.1111
1
<?php
2
3
namespace App\Service;
4
5
use App\Entity\ServiceProvider;
6
use App\Entity\User;
7
use App\Entity\UserRole;
8
use Doctrine\Common\Collections\ArrayCollection;
9
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
10
use Symfony\Component\Security\Core\User\UserInterface;
11
12
/**
13
 * Resolves services which users are enabled for.
14
 */
15
class UserServiceProviderResolver {
16
    public function __construct(private TokenStorageInterface $tokenStorage)
17 14
    {
18 14
    }
19 14
20
    private function getUser(): ?UserInterface {
21 5
        $token = $this->tokenStorage->getToken();
22 5
23
        if($token === null) {
24 5
            return null;
25
        }
26
27
        $user = $token->getUser();
28 5
        return $user;
29 5
    }
30
31
    /**
32
     * Returns the list of services the currently loggedin user is enabled for.
33
     *
34
     * @return ArrayCollection
35
     */
36
    public function getServicesForCurrentUser(): iterable {
37 5
        $user = $this->getUser();
38 5
39
        if(!$user instanceof User) {
40 5
            return new ArrayCollection();
41
        }
42
43
        return $this->getServices($user);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->getServices($user) returns the type iterable which is incompatible with the documented return type Doctrine\Common\Collections\ArrayCollection.
Loading history...
44 5
    }
45
46
    /**
47
     * Returns a list of services (ServiceProvider) the given user is enabled for.
48
     *
49
     * @param User|null $user
50
     * @return ArrayCollection
51
     */
52
    public function getServices(User $user = null): iterable {
53 6
        if($user === null) {
54 6
            return new ArrayCollection();
55
        }
56
57
        /** @var ServiceProvider[] $userServices */
58
        $userServices = $user->getEnabledServices();
59 6
        /** @var ServiceProvider[] $typeServices */
60
        $typeServices = $user->getType()->getEnabledServices();
61 6
62
        $services = [ ];
63 6
64
        foreach($userServices as $service) {
65 6
            $services[$service->getId()] = $service;
66
        }
67
68
        foreach($typeServices as $service) {
69 6
            $services[$service->getId()] = $service;
70
        }
71
72
        /** @var UserRole[] $userRoles */
73
        $userRoles = $user->getUserRoles();
74 6
75
        foreach($userRoles as $role) {
76 6
            /** @var ServiceProvider[] $roleServices */
77
            $roleServices = $role->getEnabledServices();
78
79
            foreach($roleServices as $service) {
80
                $services[$service->getId()] = $service;
81
            }
82
        }
83
84
        // sort by name
85
        usort($services, fn(ServiceProvider $serviceProviderA, ServiceProvider $serviceProviderB) => strcmp($serviceProviderA->getName(), $serviceProviderB->getName()));
86
87
        return new ArrayCollection(array_values($services));
88
    }
89
}