Passed
Push — master ( 864fd9...c2eae6 )
by Sébastien
03:26
created

ConfigProvider   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Test Coverage

Coverage 86.36%

Importance

Changes 0
Metric Value
wmc 9
eloc 33
dl 0
loc 99
ccs 19
cts 22
cp 0.8636
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getDependencies() 0 5 1
A getAliases() 0 12 1
A getFactories() 0 16 1
A __invoke() 0 4 1
A getBaseDir() 0 11 2
A getDefaultConfiguration() 0 9 3
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * @see       https://github.com/soluble-io/soluble-mediatools for the canonical repository
7
 *
8
 * @copyright Copyright (c) 2018-2019 Sébastien Vanvelthem. (https://github.com/belgattitude)
9
 * @license   https://github.com/soluble-io/soluble-mediatools/blob/master/LICENSE.md MIT
10
 */
11
12
namespace Soluble\MediaTools\Video\Config;
13
14
use Soluble\MediaTools\Video\Cache\CacheInterface;
15
use Soluble\MediaTools\Video\Cache\NullCacheFactory;
16
use Soluble\MediaTools\Video\Logger\LoggerInterface;
17
use Soluble\MediaTools\Video\Logger\NullLoggerFactory;
18
use Soluble\MediaTools\Video\VideoAnalyzer;
19
use Soluble\MediaTools\Video\VideoAnalyzerFactory;
20
use Soluble\MediaTools\Video\VideoAnalyzerInterface;
21
use Soluble\MediaTools\Video\VideoConverter;
22
use Soluble\MediaTools\Video\VideoConverterFactory;
23
use Soluble\MediaTools\Video\VideoConverterInterface;
24
use Soluble\MediaTools\Video\VideoInfoReader;
25
use Soluble\MediaTools\Video\VideoInfoReaderFactory;
26
use Soluble\MediaTools\Video\VideoInfoReaderInterface;
27
use Soluble\MediaTools\Video\VideoThumbGenerator;
28
use Soluble\MediaTools\Video\VideoThumbGeneratorFactory;
29
use Soluble\MediaTools\Video\VideoThumbGeneratorInterface;
30
31
class ConfigProvider
32
{
33
    /**
34
     * @return array<string, array<string,array<string,mixed>>>
35
     */
36 1
    public function __invoke(): array
37
    {
38
        return [
39 1
            'dependencies' => $this->getDependencies(),
40
        ];
41
    }
42
43
    /**
44
     * @return array<string, array<string,string>>
45
     */
46 48
    public function getDependencies(): array
47
    {
48
        return [
49 48
            'aliases'   => $this->getAliases(),
50 48
            'factories' => $this->getFactories(),
51
        ];
52
    }
53
54
    /**
55
     * Return concrete implementation aliases if needed.
56
     *
57
     * @return array<string, string>
58
     */
59 48
    public function getAliases(): array
60
    {
61
        return [
62
            // Configuration holders
63 48
            FFMpegConfig::class  => FFMpegConfigInterface::class,
64
            FFProbeConfig::class => FFProbeConfigInterface::class,
65
66
            // Services
67
            VideoConverter::class      => VideoConverterInterface::class,
68
            VideoInfoReader::class     => VideoInfoReaderInterface::class,
69
            VideoAnalyzer::class       => VideoAnalyzerInterface::class,
70
            VideoThumbGenerator::class => VideoThumbGeneratorInterface::class,
71
        ];
72
    }
73
74
    /**
75
     * Return interface based factories.
76
     *
77
     * @return array<string, string>
78
     */
79 48
    public function getFactories(): array
80
    {
81
        return [
82
            // Configuration holders
83 48
            FFMpegConfigInterface::class  => FFMpegConfigFactory::class,
84
            FFProbeConfigInterface::class => FFProbeConfigFactory::class,
85
86
            // Services classes
87
            VideoConverterInterface::class      => VideoConverterFactory::class,
88
            VideoInfoReaderInterface::class     => VideoInfoReaderFactory::class,
89
            VideoAnalyzerInterface::class       => VideoAnalyzerFactory::class,
90
            VideoThumbGeneratorInterface::class => VideoThumbGeneratorFactory::class,
91
92
            // Infrastructure
93
            LoggerInterface::class       => NullLoggerFactory::class,
94
            CacheInterface::class        => NullCacheFactory::class,
95
        ];
96
    }
97
98
    /**
99
     * @throws \RuntimeException
100
     *
101
     * @return array<string, array<string,mixed>>
102
     */
103 2
    public static function getDefaultConfiguration(): array
104
    {
105 2
        $baseDir = static::getBaseDir();
106 2
        $config  = implode(DIRECTORY_SEPARATOR, [$baseDir, 'config', 'soluble-mediatools.config.php']);
107 2
        if (!is_file($config) || !is_readable($config)) {
108 1
            throw new \RuntimeException(sprintf('Missing project default configuration: %s', $config));
109
        }
110
111 1
        return require $config;
112
    }
113
114
    /**
115
     * Get soluble-mediatools base directory.
116
     *
117
     * @throws \RuntimeException
118
     */
119 1
    public static function getBaseDir(): string
120
    {
121 1
        $baseDir = dirname(__DIR__, 3);
122 1
        if (!is_dir($baseDir)) {
123
            throw new \RuntimeException(sprintf(
124
                'Cannot locate library directory: %s',
125
                $baseDir
126
            ));
127
        }
128
129 1
        return $baseDir;
130
    }
131
}
132