Passed
Pull Request — feature/publishable (#31)
by Vincent
06:31
created

PublishableHelper::isGranted()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
cc 1
nc 1
nop 0
crap 2
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 9
    public function __construct(Reader $reader, AuthorizationCheckerInterface $authorizationChecker, string $permission)
33
    {
34 9
        $this->reader = $reader;
35 9
        $this->authorizationChecker = $authorizationChecker;
36 9
        $this->permission = $permission;
37 9
    }
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 9
    public function isPublishable($class): bool
48
    {
49 9
        return null !== $this->getConfiguration($class);
50
    }
51
52
    /**
53
     * @param object|string $class
54
     */
55 9
    public function getConfiguration($class): ?Publishable
56
    {
57
        /* @var Publishable|null $configuration */
58 9
        return $this->reader->getClassAnnotation(new \ReflectionClass($class), Publishable::class);
59
    }
60
}
61