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 Doctrine\Persistence\ManagerRegistry; |
18
|
|
|
use Silverback\ApiComponentBundle\Annotation\Publishable; |
19
|
|
|
use Silverback\ApiComponentBundle\Entity\Utility\PublishableInterface; |
20
|
|
|
use Symfony\Component\ExpressionLanguage\Expression; |
21
|
|
|
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @author Vincent Chalamon <[email protected]> |
25
|
|
|
*/ |
26
|
|
|
final class PublishableHelper |
27
|
|
|
{ |
28
|
|
|
use ClassMetadataTrait; |
29
|
|
|
|
30
|
|
|
private Reader $reader; |
31
|
|
|
private ManagerRegistry $registry; |
32
|
|
|
private AuthorizationCheckerInterface $authorizationChecker; |
33
|
|
|
private string $permission; |
34
|
|
|
|
35
|
9 |
|
public function __construct(Reader $reader, ManagerRegistry $registry, AuthorizationCheckerInterface $authorizationChecker, string $permission) |
36
|
|
|
{ |
37
|
9 |
|
$this->reader = $reader; |
38
|
9 |
|
$this->registry = $registry; |
39
|
9 |
|
$this->authorizationChecker = $authorizationChecker; |
40
|
9 |
|
$this->permission = $permission; |
41
|
9 |
|
} |
42
|
|
|
|
43
|
|
|
public function isGranted(): bool |
44
|
|
|
{ |
45
|
|
|
return $this->authorizationChecker->isGranted(new Expression($this->permission)); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function isPublished(object $object): bool |
49
|
|
|
{ |
50
|
|
|
if (!$this->isPublishable($object)) { |
51
|
|
|
throw new \InvalidArgumentException(sprintf('Object of class %s does not implement publishable configuration.', \get_class($object))); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
if ($object instanceof PublishableInterface) { |
55
|
|
|
return $object->isPublished(); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
$value = $this->getClassMetadata($object)->getFieldValue($object, $this->getConfiguration($object)->fieldName); |
59
|
|
|
|
60
|
|
|
return null !== $value && new \DateTimeImmutable() >= $value; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function hasPublicationDate(object $object): bool |
64
|
|
|
{ |
65
|
|
|
if (!$this->isPublishable($object)) { |
66
|
|
|
throw new \InvalidArgumentException(sprintf('Object of class %s does not implement publishable configuration.', \get_class($object))); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
if ($object instanceof PublishableInterface) { |
70
|
|
|
return $object->isPublished(); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
return null !== $this->getClassMetadata($object)->getFieldValue($object, $this->getConfiguration($object)->fieldName); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param object|string $class |
78
|
|
|
*/ |
79
|
9 |
|
public function isPublishable($class): bool |
80
|
|
|
{ |
81
|
9 |
|
return null !== $this->getConfiguration($class); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param object|string $class |
86
|
|
|
*/ |
87
|
9 |
|
public function getConfiguration($class): ?Publishable |
88
|
|
|
{ |
89
|
9 |
|
if (null === $class || (\is_string($class) && !class_exists($class))) { |
90
|
|
|
return null; |
91
|
|
|
} |
92
|
|
|
|
93
|
9 |
|
return $this->reader->getClassAnnotation(new \ReflectionClass($class), Publishable::class); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|