Test Setup Failed
Push — master ( 0c8bf0...b71d97 )
by Herberto
05:57
created

PostVoterAdapter   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 4
dl 0
loc 41
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A supports() 0 4 1
A voteOnAttribute() 0 11 2
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Explicit Architecture POC,
7
 * which is created on top of the Symfony Demo application.
8
 *
9
 * (c) Herberto Graça <[email protected]>
10
 *
11
 * For the full copyright and license information, please view the LICENSE
12
 * file that was distributed with this source code.
13
 */
14
15
namespace Acme\App\Infrastructure\Auth\Authorization\Voter;
16
17
use Acme\App\Core\Component\Blog\Application\Auth\PostVoter;
18
use Acme\App\Core\Component\Blog\Domain\Post\Post;
19
use Acme\App\Infrastructure\Auth\Authentication\SecurityUser;
20
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
21
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
22
23
/**
24
 * It grants or denies permissions for actions related to blog posts (such as
25
 * showing, editing and deleting posts).
26
 *
27
 * See https://symfony.com/doc/current/security/voters.html
28
 *
29
 * @author Yonel Ceruto <[email protected]>
30
 */
31
class PostVoterAdapter extends Voter
32
{
33
    /**
34
     * @var PostVoter
35
     */
36
    private $postVoter;
37
38
    public function __construct(PostVoter $postVoter)
39
    {
40
        $this->postVoter = $postVoter;
41
    }
42
43
    /**
44
     * @param string $attribute
45
     * @param Post $subject
46
     *
47
     * {@inheritdoc}
48
     */
49
    protected function supports($attribute, $subject): bool
50
    {
51
        return $this->postVoter->supports($attribute, $subject);
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     *
57
     * @param string $attribute
58
     * @param Post $post
59
     */
60
    protected function voteOnAttribute($attribute, $post, TokenInterface $token): bool
61
    {
62
        $securityUser = $token->getUser();
63
64
        // the user must be logged in; if not, deny permission
65
        if (!$securityUser instanceof SecurityUser) {
66
            return false;
67
        }
68
69
        return $this->postVoter->voteOnAttribute($attribute, $post, $securityUser->getUserId());
70
    }
71
}
72