1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Silverback API Components 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\ApiComponentsBundle\Publishable; |
15
|
|
|
|
16
|
|
|
use Doctrine\Persistence\ManagerRegistry; |
17
|
|
|
use Silverback\ApiComponentsBundle\AnnotationReader\PublishableAnnotationReader; |
18
|
|
|
use Silverback\ApiComponentsBundle\Utility\ClassMetadataTrait; |
19
|
|
|
use Symfony\Component\ExpressionLanguage\Expression; |
20
|
|
|
use Symfony\Component\HttpFoundation\Request; |
21
|
|
|
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @author Vincent Chalamon <[email protected]> |
25
|
|
|
* @author Daniel West <[email protected]> |
26
|
|
|
*/ |
27
|
|
|
class PublishableHelper |
28
|
|
|
{ |
29
|
|
|
use ClassMetadataTrait; |
30
|
|
|
|
31
|
|
|
private PublishableAnnotationReader $annotationReader; |
32
|
|
|
private AuthorizationCheckerInterface $authorizationChecker; |
33
|
|
|
private string $permission; |
34
|
|
|
|
35
|
|
|
public function __construct(ManagerRegistry $registry, PublishableAnnotationReader $annotationReader, AuthorizationCheckerInterface $authorizationChecker, string $permission) |
36
|
|
|
{ |
37
|
|
|
$this->initRegistry($registry); |
38
|
|
|
$this->annotationReader = $annotationReader; |
39
|
|
|
$this->authorizationChecker = $authorizationChecker; |
40
|
|
|
$this->permission = $permission; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param object|string $class |
45
|
|
|
*/ |
46
|
|
|
public function isGranted($class): bool |
47
|
|
|
{ |
48
|
|
|
return $this->authorizationChecker->isGranted(new Expression($this->annotationReader->getConfiguration($class)->isGranted ?? $this->permission)); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function isActivePublishedAt(object $object): bool |
52
|
|
|
{ |
53
|
|
|
if (!$this->annotationReader->isConfigured($object)) { |
54
|
|
|
throw new \InvalidArgumentException(sprintf('Object of class %s does not implement publishable configuration.', \get_class($object))); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
$value = $this->getClassMetadata($object)->getFieldValue($object, $this->annotationReader->getConfiguration($object)->fieldName); |
58
|
|
|
|
59
|
|
|
return null !== $value && new \DateTimeImmutable() >= $value; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function hasPublicationDate(object $object): bool |
63
|
|
|
{ |
64
|
|
|
if (!$this->annotationReader->isConfigured($object)) { |
65
|
|
|
throw new \InvalidArgumentException(sprintf('Object of class %s does not implement publishable configuration.', \get_class($object))); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
return null !== $this->getClassMetadata($object)->getFieldValue($object, $this->annotationReader->getConfiguration($object)->fieldName); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function isPublishedRequest(Request $request): bool |
72
|
|
|
{ |
73
|
|
|
return $request->query->getBoolean('published', false); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function getAnnotationReader(): PublishableAnnotationReader |
77
|
|
|
{ |
78
|
|
|
return $this->annotationReader; |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|