Passed
Pull Request — master (#52)
by Daniel
05:34
created

PublishableHelper::getAnnotationReader()   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
c 0
b 0
f 0
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
cc 1
nc 1
nop 0
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\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->annotationReader->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