Passed
Pull Request — feature/publishable (#47)
by Daniel
22:19
created

PublishableHelper   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Test Coverage

Coverage 59.38%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 16
eloc 30
c 1
b 0
f 0
dl 0
loc 76
ccs 19
cts 32
cp 0.5938
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A isGranted() 0 3 1
A isPublishedRequest() 0 3 1
A isActivePublishedAt() 0 9 3
A getConfiguration() 0 13 6
A isPublishable() 0 9 2
A hasPublicationDate() 0 7 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 Doctrine\Common\Annotations\Reader;
17
use Doctrine\Persistence\ManagerRegistry;
18
use Silverback\ApiComponentBundle\Annotation\Publishable;
19
use Silverback\ApiComponentBundle\Exception\InvalidArgumentException;
20
use Silverback\ApiComponentBundle\Utility\ClassMetadataTrait;
21
use Symfony\Component\ExpressionLanguage\Expression;
22
use Symfony\Component\HttpFoundation\Request;
23
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
24
25
/**
26
 * @author Vincent Chalamon <[email protected]>
27
 */
28
final class PublishableHelper
29
{
30
    use ClassMetadataTrait;
31
32
    private Reader $reader;
33
    private AuthorizationCheckerInterface $authorizationChecker;
34
    private string $permission;
35
36 9
    public function __construct(Reader $reader, ManagerRegistry $registry, AuthorizationCheckerInterface $authorizationChecker, string $permission)
37
    {
38 9
        $this->reader = $reader;
39 9
        $this->authorizationChecker = $authorizationChecker;
40 9
        $this->permission = $permission;
41 9
        $this->initRegistry($registry);
42 9
    }
43
44
    public function isGranted(): bool
45
    {
46
        return $this->authorizationChecker->isGranted(new Expression($this->permission));
47
    }
48
49
    public function isActivePublishedAt(object $object): bool
50
    {
51
        if (!$this->isPublishable($object)) {
52
            throw new \InvalidArgumentException(sprintf('Object of class %s does not implement publishable configuration.', \get_class($object)));
53
        }
54
55
        $value = $this->getClassMetadata($object)->getFieldValue($object, $this->getConfiguration($object)->fieldName);
56
57
        return null !== $value && new \DateTimeImmutable() >= $value;
58
    }
59
60
    public function hasPublicationDate(object $object): bool
61
    {
62
        if (!$this->isPublishable($object)) {
63
            throw new \InvalidArgumentException(sprintf('Object of class %s does not implement publishable configuration.', \get_class($object)));
64
        }
65
66
        return null !== $this->getClassMetadata($object)->getFieldValue($object, $this->getConfiguration($object)->fieldName);
67
    }
68
69
    /**
70
     * @param object|string $class
71
     */
72 9
    public function isPublishable($class): bool
73
    {
74
        try {
75 9
            $this->getConfiguration($class);
76 9
        } catch (InvalidArgumentException $exception) {
77 9
            return false;
78
        }
79
80 9
        return true;
81
    }
82
83
    /**
84
     * @param object|string $class
85
     */
86 9
    public function getConfiguration($class): Publishable
87
    {
88 9
        $configuration = null;
89 9
        if (\is_string($class) || \is_object($class)) {
90 9
            $configuration = $this->reader->getClassAnnotation(new \ReflectionClass($class), Publishable::class);
91
        }
92
93 9
        if (!$configuration || !$configuration instanceof Publishable) {
94 9
            $className = \is_string($class) ? $class : \get_class($class);
95 9
            throw new InvalidArgumentException(sprintf('Could not get publishable configuration for %s', $className));
96
        }
97
98 9
        return $configuration;
99
    }
100
101
    public function isPublishedRequest(Request $request): bool
102
    {
103
        return $request->query->getBoolean('published', false);
104
    }
105
}
106