Completed
Push — master ( bbe544...1bc078 )
by ReliQ
03:23 queued 11s
created

Loader::load()   A

Complexity

Conditions 2
Paths 3

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 8
nc 3
nop 1
dl 0
loc 11
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace ReliqArts\DirectTranslator\Vocabulary;
6
7
use DomainException;
8
use Exception;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, ReliqArts\DirectTranslator\Vocabulary\Exception. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
9
use ReliqArts\DirectTranslator\ConfigProvider;
10
use ReliqArts\DirectTranslator\Vocabulary as VocabularyContract;
11
use ReliqArts\DirectTranslator\Vocabulary\Exception\LoadingFailed;
12
use ReliqArts\DirectTranslator\VocabularyLoader;
13
14
final class Loader implements VocabularyLoader
15
{
16
    private const VOCAB_FILE_EXTENSION = 'json';
17
18
    /**
19
     * @var ConfigProvider
20
     */
21
    private $configProvider;
22
23
    /**
24
     * @var Reader
25
     */
26
    private $reader;
27
28
    /**
29
     * @var Builder
30
     */
31
    private $builder;
32
33
    /**
34
     * VocabularyLoader constructor.
35
     *
36
     * @param ConfigProvider $configProvider
37
     * @param Reader         $reader
38
     * @param Builder        $builder
39
     */
40
    public function __construct(
41
        ConfigProvider $configProvider,
42
        Reader $reader,
43
        Builder $builder
44
    ) {
45
        $this->configProvider = $configProvider;
46
        $this->reader = $reader;
47
        $this->builder = $builder;
48
    }
49
50
    /**
51
     * @param string $key
52
     *
53
     * @throws LoadingFailed
54
     *
55
     * @return VocabularyContract
56
     */
57
    public function load(string $key): VocabularyContract
58
    {
59
        try {
60
            $filePath = $this->getVocabularyFilePath($key);
61
62
            return $this->builder->create($this->reader->read($filePath));
63
        } catch (Exception $exception) {
64
            throw new LoadingFailed(
65
                sprintf('Could not load vocabulary by key: `%s`', $key),
66
                $exception->getCode(),
67
                $exception
68
            );
69
        }
70
    }
71
72
    /**
73
     * @param string $key
74
     *
75
     * @throws DomainException
76
     *
77
     * @return null|string
78
     */
79
    private function getVocabularyFilePath(string $key): ?string
80
    {
81
        $vocabularyDirectories = $this->configProvider->getVocabularyDirectories();
82
83
        foreach ($vocabularyDirectories as $directory) {
84
            $path = sprintf('%s/%s.%s', $directory, $key, self::VOCAB_FILE_EXTENSION);
85
86
            if ($vocabularyPath = realpath($path)) {
87
                return $vocabularyPath;
88
            }
89
        }
90
91
        throw new DomainException(
92
            sprintf('Vocabulary file not found for key: `%s`.', $key)
93
        );
94
    }
95
}
96