AbstractFileDriver::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Metadata\Driver;
6
7
use Metadata\ClassMetadata;
8
9
/**
10
 * Base file driver implementation.
11
 *
12
 * @author Johannes M. Schmitt <[email protected]>
13
 */
14
abstract class AbstractFileDriver implements AdvancedDriverInterface
15
{
16
    /**
17
     * @var FileLocatorInterface|FileLocator
18
     */
19
    private $locator;
20
21 4
    public function __construct(FileLocatorInterface $locator)
22
    {
23 4
        $this->locator = $locator;
24 4
    }
25
26 2
    public function loadMetadataForClass(\ReflectionClass $class): ?ClassMetadata
27
    {
28 2
        if (null === $path = $this->locator->findFileForClass($class, $this->getExtension())) {
29 1
            return null;
30
        }
31
32 1
        return $this->loadMetadataFromFile($class, $path);
33
    }
34
35
    /**
36
     * {@inheritDoc}
37
     */
38 2
    public function getAllClassNames(): array
39
    {
40 2
        if (!$this->locator instanceof AdvancedFileLocatorInterface) {
41 1
            throw new \RuntimeException(sprintf('Locator "%s" must be an instance of "AdvancedFileLocatorInterface".', get_class($this->locator)));
42
        }
43
44 1
        return $this->locator->findAllClasses($this->getExtension());
45
    }
46
47
    /**
48
     * Parses the content of the file, and converts it to the desired metadata.
49
     */
50
    abstract protected function loadMetadataFromFile(\ReflectionClass $class, string $file): ?ClassMetadata;
51
52
    /**
53
     * Returns the extension of the file.
54
     */
55
    abstract protected function getExtension(): string;
56
}
57