Passed
Pull Request — master (#50)
by Daniel
08:24
created

PublishableHelper::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 4
c 0
b 0
f 0
dl 0
loc 6
ccs 5
cts 5
cp 1
rs 10
cc 1
nc 1
nop 4
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\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
    public function isGranted(): bool
40
    {
41
        return $this->authorizationChecker->isGranted(new Expression($this->permission));
42
    }
43
44
    public function isActivePublishedAt(object $object): bool
45
    {
46
        if (!$this->isConfigured($object)) {
47
            throw new \InvalidArgumentException(sprintf('Object of class %s does not implement publishable configuration.', \get_class($object)));
48
        }
49
50
        $value = $this->getClassMetadata($object)->getFieldValue($object, $this->getConfiguration($object)->fieldName);
51
52
        return null !== $value && new \DateTimeImmutable() >= $value;
53
    }
54
55
    public function hasPublicationDate(object $object): bool
56
    {
57
        if (!$this->isConfigured($object)) {
58
            throw new \InvalidArgumentException(sprintf('Object of class %s does not implement publishable configuration.', \get_class($object)));
59
        }
60
61
        return null !== $this->getClassMetadata($object)->getFieldValue($object, $this->getConfiguration($object)->fieldName);
62
    }
63
64
    /**
65
     * @param object|string $class
66
     */
67 9
    public function getConfiguration($class): Publishable
68
    {
69 9
        return $this->getAnnotationConfiguration($class, Publishable::class);
70
    }
71
72
    public function isPublishedRequest(Request $request): bool
73
    {
74
        return $request->query->getBoolean('published', false);
75
    }
76
}
77