Passed
Push — feature/publishable ( ce6968...c26268 )
by Daniel
13:37
created

PublishableHelper::getConfiguration()   B

Complexity

Conditions 7
Paths 9

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 7.0368

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
c 1
b 0
f 0
dl 0
loc 17
ccs 10
cts 11
cp 0.9091
rs 8.8333
cc 7
nc 9
nop 1
crap 7.0368
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\Common\Persistence\Proxy;
18
use Doctrine\Persistence\ManagerRegistry;
19
use Silverback\ApiComponentBundle\Annotation\Publishable;
20
use Silverback\ApiComponentBundle\Exception\InvalidArgumentException;
21
use Silverback\ApiComponentBundle\Utility\ClassMetadataTrait;
22
use Symfony\Component\ExpressionLanguage\Expression;
23
use Symfony\Component\HttpFoundation\Request;
24
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
25
26
/**
27
 * @author Vincent Chalamon <[email protected]>
28
 */
29
final class PublishableHelper
30
{
31
    use ClassMetadataTrait;
32
33
    private Reader $reader;
34
    private AuthorizationCheckerInterface $authorizationChecker;
35
    private string $permission;
36
37 9
    public function __construct(Reader $reader, ManagerRegistry $registry, AuthorizationCheckerInterface $authorizationChecker, string $permission)
38
    {
39 9
        $this->reader = $reader;
40 9
        $this->authorizationChecker = $authorizationChecker;
41 9
        $this->permission = $permission;
42 9
        $this->initRegistry($registry);
43 9
    }
44
45
    public function isGranted(): bool
46
    {
47
        return $this->authorizationChecker->isGranted(new Expression($this->permission));
48
    }
49
50
    public function isActivePublishedAt(object $object): bool
51
    {
52
        if (!$this->isPublishable($object)) {
53
            throw new \InvalidArgumentException(sprintf('Object of class %s does not implement publishable configuration.', \get_class($object)));
54
        }
55
56
        $value = $this->getClassMetadata($object)->getFieldValue($object, $this->getConfiguration($object)->fieldName);
57
58
        return null !== $value && new \DateTimeImmutable() >= $value;
59
    }
60
61
    public function hasPublicationDate(object $object): bool
62
    {
63
        if (!$this->isPublishable($object)) {
64
            throw new \InvalidArgumentException(sprintf('Object of class %s does not implement publishable configuration.', \get_class($object)));
65
        }
66
67
        return null !== $this->getClassMetadata($object)->getFieldValue($object, $this->getConfiguration($object)->fieldName);
68
    }
69
70
    /**
71
     * @param object|string $class
72
     */
73 9
    public function isPublishable($class): bool
74
    {
75
        try {
76 9
            $this->getConfiguration($class);
77 9
        } catch (InvalidArgumentException $exception) {
78 9
            return false;
79
        }
80
81 9
        return true;
82
    }
83
84
    /**
85
     * @param object|string $class
86
     */
87 9
    public function getConfiguration($class): Publishable
88
    {
89 9
        $configuration = null;
90 9
        if (\is_string($class) || \is_object($class)) {
91 9
            $reflection = new \ReflectionClass($class);
92 9
            if ($reflection->implementsInterface(Proxy::class)) {
93
                $reflection = $reflection->getParentClass();
94
            }
95 9
            $configuration = $this->reader->getClassAnnotation($reflection, Publishable::class);
0 ignored issues
show
Bug introduced by
It seems like $reflection can also be of type false; however, parameter $class of Doctrine\Common\Annotati...r::getClassAnnotation() does only seem to accept ReflectionClass, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

95
            $configuration = $this->reader->getClassAnnotation(/** @scrutinizer ignore-type */ $reflection, Publishable::class);
Loading history...
96
        }
97
98 9
        if (!$configuration || !$configuration instanceof Publishable) {
99 9
            $className = \is_string($class) ? $class : \get_class($class);
100 9
            throw new InvalidArgumentException(sprintf('Could not get publishable configuration for %s', $className));
101
        }
102
103 9
        return $configuration;
104
    }
105
106
    public function isPublishedRequest(Request $request): bool
107
    {
108
        return $request->query->getBoolean('published', false);
109
    }
110
}
111