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