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

PublishableHelper   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
eloc 17
c 1
b 0
f 0
dl 0
loc 49
ccs 0
cts 20
cp 0
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A isPublishedRequest() 0 3 1
A getAnnotationReader() 0 3 1
A isGranted() 0 3 1
A __construct() 0 5 1
A isActivePublishedAt() 0 9 3
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 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