Passed
Pull Request — master (#54)
by Vincent
06:15
created

PublishableHelper::isGranted()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
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\Helper;
15
16
use Doctrine\Common\Annotations\Reader;
17
use Doctrine\Persistence\ManagerRegistry;
18
use Silverback\ApiComponentBundle\Annotation\Publishable;
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
 */
26
final class PublishableHelper extends AbstractHelper
27
{
28
    private AuthorizationCheckerInterface $authorizationChecker;
29
    private string $permission;
30
31 9
    public function __construct(Reader $reader, ManagerRegistry $registry, AuthorizationCheckerInterface $authorizationChecker, string $permission)
32
    {
33 9
        $this->authorizationChecker = $authorizationChecker;
34 9
        $this->permission = $permission;
35 9
        $this->initRegistry($registry);
36 9
        $this->initReader($reader);
37 9
    }
38
39
    /**
40
     * @param object|string $class
41
     */
42
    public function isGranted($class): bool
43
    {
44
        return $this->authorizationChecker->isGranted(new Expression($this->getConfiguration($class)->isGranted ?? $this->permission));
45
    }
46
47
    public function isActivePublishedAt(object $object): bool
48
    {
49
        if (!$this->isConfigured($object)) {
50
            throw new \InvalidArgumentException(sprintf('Object of class %s does not implement publishable configuration.', \get_class($object)));
51
        }
52
53
        $value = $this->getClassMetadata($object)->getFieldValue($object, $this->getConfiguration($object)->fieldName);
54
55
        return null !== $value && new \DateTimeImmutable() >= $value;
56
    }
57
58
    public function hasPublicationDate(object $object): bool
59
    {
60
        if (!$this->isConfigured($object)) {
61
            throw new \InvalidArgumentException(sprintf('Object of class %s does not implement publishable configuration.', \get_class($object)));
62
        }
63
64
        return null !== $this->getClassMetadata($object)->getFieldValue($object, $this->getConfiguration($object)->fieldName);
65
    }
66
67
    /**
68
     * @param object|string $class
69
     */
70 9
    public function getConfiguration($class): Publishable
71
    {
72 9
        return $this->getAnnotationConfiguration($class, Publishable::class);
73
    }
74
75
    public function isPublishedRequest(Request $request): bool
76
    {
77
        return $request->query->getBoolean('published', false);
78
    }
79
}
80