Passed
Push — main ( 09d243...04aeb5 )
by Daniel
04:48
created

PublishableStatusChecker::isGranted()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 4
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 6
ccs 0
cts 4
cp 0
crap 6
rs 10
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\Helper\Publishable;
15
16
use Doctrine\Persistence\ManagerRegistry;
17
use Silverback\ApiComponentsBundle\AttributeReader\PublishableAttributeReader;
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
use Symfony\Component\Security\Core\Exception\AuthenticationCredentialsNotFoundException;
23
24
/**
25
 * @author Vincent Chalamon <[email protected]>
26
 * @author Daniel West <[email protected]>
27
 */
28
class PublishableStatusChecker
29
{
30
    use ClassMetadataTrait;
31
32
    private string $permission;
33
34
    public function __construct(
35
        ManagerRegistry $registry,
36
        private readonly PublishableAttributeReader $attributeReader,
37
        private readonly AuthorizationCheckerInterface $authorizationChecker,
38
        string $permission
39
    ) {
40
        $this->initRegistry($registry);
41
        $this->permission = $permission;
42
    }
43
44
    /**
45
     * @param object|string $class
46
     */
47
    public function isGranted($class): bool
48
    {
49
        try {
50
            return $this->authorizationChecker->isGranted(new Expression($this->attributeReader->getConfiguration($class)->isGranted ?? $this->permission));
51
        } catch (AuthenticationCredentialsNotFoundException $e) {
52
            return false;
53
        }
54
    }
55
56
    public function isActivePublishedAt(object $object): bool
57
    {
58
        if (!$this->attributeReader->isConfigured($object)) {
59
            throw new \InvalidArgumentException(sprintf('Object of class %s does not implement publishable configuration.', \get_class($object)));
60
        }
61
62
        $value = $this->getClassMetadata($object)->getFieldValue($object, $this->attributeReader->getConfiguration($object)->fieldName);
63
64
        return null !== $value && new \DateTimeImmutable() >= $value;
65
    }
66
67
    public function hasPublicationDate(object $object): bool
68
    {
69
        if (!$this->attributeReader->isConfigured($object)) {
70
            throw new \InvalidArgumentException(sprintf('Object of class %s does not implement publishable configuration.', \get_class($object)));
71
        }
72
73
        return null !== $this->getClassMetadata($object)->getFieldValue($object, $this->attributeReader->getConfiguration($object)->fieldName);
74
    }
75
76
    public function isRequestForPublished(Request $request): bool
77
    {
78
        return $request->query->getBoolean('published', false);
79
    }
80
81
    public function getAttributeReader(): PublishableAttributeReader
82
    {
83
        return $this->attributeReader;
84
    }
85
}
86