PlanFeatureVoter   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 14
c 1
b 0
f 0
dl 0
loc 38
rs 10
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A voteOnAttribute() 0 17 4
A supports() 0 7 2
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * Copyright (C) 2020-2025 Iain Cambridge
7
 *
8
 * This program is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU LESSER GENERAL PUBLIC LICENSE as published by
10
 * the Free Software Foundation, either version 2.1 of the License, or
11
 * (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU Lesser General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU General Public License
19
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
20
 */
21
22
namespace Parthenon\Billing\Plan\Security\Voter;
23
24
use Parthenon\Billing\CustomerProviderInterface;
25
use Parthenon\Billing\Plan\CustomerPlanInfoInterface;
26
use Parthenon\Billing\Plan\LimitedUserInterface;
27
use Parthenon\Common\LoggerAwareTrait;
28
use Parthenon\User\Entity\UserInterface;
29
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
30
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
31
32
final class PlanFeatureVoter extends Voter
33
{
34
    use LoggerAwareTrait;
35
36
    public const SUPPORTED_ATTRIBUTES = ['feature_enabled'];
37
38
    public function __construct(
39
        private CustomerProviderInterface $customerProvider,
40
        private CustomerPlanInfoInterface $planInfo,
41
    ) {
42
    }
43
44
    protected function supports(string $attribute, $subject): bool
45
    {
46
        if (!in_array($attribute, self::SUPPORTED_ATTRIBUTES)) {
47
            return false;
48
        }
49
50
        return true;
51
    }
52
53
    protected function voteOnAttribute(string $attribute, $subject, TokenInterface $token): bool
54
    {
55
        $user = $token->getUser();
56
57
        if (!is_string($subject)) {
58
            return true;
59
        }
60
61
        if (!$user instanceof UserInterface) {
62
            return false;
63
        }
64
65
        if (!$user instanceof LimitedUserInterface) {
66
            return true;
67
        }
68
69
        return $this->planInfo->hasFeature($this->customerProvider->getCurrentCustomer(), $subject);
70
    }
71
}
72