Passed
Push — feature/publishable ( 5afc24...ce6968 )
by Daniel
30:22 queued 23:47
created

TimestampedHelper::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 1
b 0
f 0
cc 1
nc 1
nop 2
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\Timestamped;
15
16
use Doctrine\Common\Annotations\Reader;
17
use Doctrine\Persistence\ManagerRegistry;
18
use Silverback\ApiComponentBundle\Annotation\Timestamped;
19
use Silverback\ApiComponentBundle\Exception\InvalidArgumentException;
20
use Silverback\ApiComponentBundle\Utility\ClassMetadataTrait;
21
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
22
23
/**
24
 * @author Daniel West <[email protected]>
25
 */
26
final class TimestampedHelper
27
{
28
    use ClassMetadataTrait;
29
30
    private Reader $reader;
31
    private AuthorizationCheckerInterface $authorizationChecker;
0 ignored issues
show
introduced by
The private property $authorizationChecker is not used, and could be removed.
Loading history...
32
    private string $permission;
0 ignored issues
show
introduced by
The private property $permission is not used, and could be removed.
Loading history...
33
34 9
    public function __construct(Reader $reader, ManagerRegistry $registry)
35
    {
36 9
        $this->reader = $reader;
37 9
        $this->initRegistry($registry);
38 9
    }
39
40
    /**
41
     * @param object|string $class
42
     */
43 9
    public function isTimestamped($class): bool
44
    {
45
        try {
46 9
            $this->getConfiguration($class);
47 9
        } catch (InvalidArgumentException $e) {
48 9
            return false;
49
        }
50
51 9
        return true;
52
    }
53
54
    /**
55
     * @param object|string $class
56
     */
57 9
    public function getConfiguration($class): Timestamped
58
    {
59 9
        if (null === $class || (\is_string($class) && !class_exists($class))) {
60
            throw new InvalidArgumentException(sprintf('$class passed to %s must be a valid class FQN or object', __CLASS__));
61
        }
62
63 9
        $reflection = new \ReflectionClass($class);
64
        /** @var Timestamped|null $timestamped */
65
        while (
66 9
            !($timestamped = $this->reader->getClassAnnotation($reflection, Timestamped::class)) &&
67 9
            ($reflection = $reflection->getParentClass())
68
        ) {
69 9
            continue;
70
        }
71 9
        if (!$timestamped) {
72 9
            throw new InvalidArgumentException(sprintf('%s does not have Publishable annotation', \is_object($class) ? \get_class($class) : $class));
73
        }
74
75 9
        return $timestamped;
76
    }
77
}
78