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; |
|
|
|
|
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
|
|
|
|