Passed
Pull Request — feature/publishable (#43)
by Vincent
09:50 queued 02:52
created

PublishableHelper::getConfiguration()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
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
        return $this->reader->getClassAnnotation(new \ReflectionClass($class), Publishable::class);
90
    }
91
}
92