Test Failed
Pull Request — feature/publishable (#31)
by Vincent
06:22
created

PublishableHelper   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 11
c 1
b 0
f 0
dl 0
loc 35
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A isGranted() 0 3 1
A __construct() 0 5 1
A getConfiguration() 0 4 1
A isPublishable() 0 3 1
1
<?php
2
3
/*
4
 * This file is part of the Silverback API Component Bundle Project
5
 *
6
 * (c) Daniel West <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace Silverback\ApiComponentBundle\Publishable;
15
16
use Doctrine\Common\Annotations\Reader;
17
use Silverback\ApiComponentBundle\Annotation\Publishable;
18
use Symfony\Component\ExpressionLanguage\Expression;
19
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
20
21
/**
22
 * @author Vincent Chalamon <[email protected]>
23
 */
24
final class PublishableHelper
25
{
26
    use ClassMetadataTrait;
0 ignored issues
show
Bug introduced by
The trait Silverback\ApiComponentB...able\ClassMetadataTrait requires the property $registry which is not provided by Silverback\ApiComponentB...hable\PublishableHelper.
Loading history...
27
28
    private Reader $reader;
29
    private AuthorizationCheckerInterface $authorizationChecker;
30
    private string $permission;
31
32
    public function __construct(Reader $reader, AuthorizationCheckerInterface $authorizationChecker, string $permission)
33
    {
34
        $this->reader = $reader;
35
        $this->authorizationChecker = $authorizationChecker;
36
        $this->permission = $permission;
37
    }
38
39
    public function isGranted(): bool
40
    {
41
        return $this->authorizationChecker->isGranted(new Expression($this->permission));
42
    }
43
44
    /**
45
     * @param object|string $class
46
     */
47
    public function isPublishable($class): bool
48
    {
49
        return null !== $this->getConfiguration($class);
50
    }
51
52
    /**
53
     * @param object|string $class
54
     */
55
    public function getConfiguration($class): ?Publishable
56
    {
57
        /** @var Publishable|null $configuration */
58
        return $this->reader->getClassAnnotation(new \ReflectionClass($class), Publishable::class);
59
    }
60
}
61