Failed Conditions
Push — master ( 438024...09a02f )
by Sébastien
03:04
created

ConfigProvider::getDependencies()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 40
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2.0625

Importance

Changes 0
Metric Value
eloc 19
dl 0
loc 40
ccs 6
cts 8
cp 0.75
rs 9.6333
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 2.0625
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * @see       https://github.com/soluble-io/soluble-mediatools-cli 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-cli/blob/master/LICENSE.md MIT
10
 */
11
12
namespace Soluble\MediaTools\Cli\Config;
13
14
use DomainException;
15
use Soluble\MediaTools\Cli\Command\ConvertDirCommand;
16
use Soluble\MediaTools\Cli\Command\ConvertDirCommandFactory;
17
use Soluble\MediaTools\Cli\Command\ScanCommand;
18
use Soluble\MediaTools\Cli\Command\ScanCommandFactory;
19
use Soluble\MediaTools\Cli\Infra\StandardFileCacheFactory;
20
use Soluble\MediaTools\Cli\Infra\StandardFileLoggerFactory;
21
use Soluble\MediaTools\Cli\Media\FileExtensions;
22
use Soluble\MediaTools\Cli\Media\FileExtenstionsFactory;
23
use Soluble\MediaTools\Cli\Service\MediaToolsServiceFactory;
24
use Soluble\MediaTools\Cli\Service\MediaToolsServiceInterface;
25
use Soluble\MediaTools\Preset\MP4\StreamableH264Preset;
26
use Soluble\MediaTools\Preset\PresetLoader;
27
use Soluble\MediaTools\Preset\PresetLoaderFactory;
28
use Soluble\MediaTools\Preset\Prod\ResolvePreset;
29
use Soluble\MediaTools\Preset\WebM\GoogleVod2019Preset;
30
use Soluble\MediaTools\Video\Cache\CacheInterface;
31
use Soluble\MediaTools\Video\Config\ConfigProvider as VideoConfigProvider;
32
use Soluble\MediaTools\Video\Logger\LoggerInterface;
33
use Zend\ServiceManager\AbstractFactory\ReflectionBasedAbstractFactory;
34
35
class ConfigProvider
36
{
37
    /**
38
     * Returns the configuration array.
39
     *
40
     * To add a bit of a structure, each section is defined in a separate
41
     * method which returns an array with its configuration.
42
     */
43 1
    public function __invoke(): array
44
    {
45
        return [
46 1
            'dependencies' => $this->getDependencies(),
47
        ];
48
    }
49
50
    /**
51
     * Returns the container dependencies.
52
     *
53
     * @throws DomainException
54
     *
55
     * @return array<string, array>
56
     */
57 5
    public function getDependencies(): array
58
    {
59 5
        $arr = array_replace_recursive(
60 5
            (new VideoConfigProvider())->getDependencies(),
61
            [
62
                'abstract_factories' => [
63 5
                    ReflectionBasedAbstractFactory::class,
64
                ],
65
66
                'factories'  => [
67
                    // Services
68
                    MediaToolsServiceInterface::class => MediaToolsServiceFactory::class,
69
                    PresetLoader::class               => PresetLoaderFactory::class,
70
71
                    // Commands
72
                    ConvertDirCommand::class => ConvertDirCommandFactory::class,
73
                    ScanCommand::class       => ScanCommandFactory::class,
74
75
                    // Infra
76
                    LoggerInterface::class      => StandardFileLoggerFactory::class,
77
                    CacheInterface::class       => StandardFileCacheFactory::class,
78
79
                    // Media Utils
80
                    FileExtensions::class       => FileExtenstionsFactory::class,
81
82
                    // Presets
83
                    GoogleVod2019Preset::class  => ReflectionBasedAbstractFactory::class,
84
                    StreamableH264Preset::class => ReflectionBasedAbstractFactory::class,
85
                    ResolvePreset::class        => ReflectionBasedAbstractFactory::class,
86
                ],
87
            ]
88
        );
89
90 5
        if ($arr === null) {
91
            throw new DomainException(sprintf(
92
                'Could not merge ConfigProvider configurations.'
93
            ));
94
        }
95
96 5
        return $arr;
97
    }
98
99
    /**
100
     * Return build console commands class names.
101
     *
102
     * @return string[]
103
     */
104 1
    public function getConsoleCommands(): array
105
    {
106
        return [
107 1
            \Soluble\MediaTools\Cli\Command\ScanCommand::class,
108
            \Soluble\MediaTools\Cli\Command\ConvertDirCommand::class,
109
        ];
110
    }
111
112
    /**
113
     * @throws \RuntimeException
114
     */
115 4
    public static function getProjectBaseDirectory(): string
116
    {
117 4
        $baseDir = dirname(__FILE__, 4);
118 4
        if (!is_dir($baseDir)) {
119
            throw new \RuntimeException(sprintf('Cannot get project base directory %s', $baseDir));
120
        }
121
122 4
        return $baseDir;
123
    }
124
125
    /**
126
     * @throws \RuntimeException
127
     */
128 4
    public static function getProjectCacheDirectory(): string
129
    {
130 4
        $cacheDir = implode(DIRECTORY_SEPARATOR, [self::getProjectBaseDirectory(), 'data', 'cache']);
131
132 4
        if (!is_dir($cacheDir)) {
133
            throw new \RuntimeException(sprintf('Cache directory %s does not exists or invalid.', $cacheDir));
134
        }
135
136 4
        if (!is_readable($cacheDir) || !is_writable($cacheDir)) {
137
            throw new \RuntimeException(sprintf('Cache directory %s must have read/write access.', $cacheDir));
138
        }
139
140 4
        return $cacheDir;
141
    }
142
143
    /**
144
     * @throws \RuntimeException
145
     */
146 4
    public static function getProjectLogDirectory(): string
147
    {
148 4
        $logDir = implode(DIRECTORY_SEPARATOR, [self::getProjectBaseDirectory(), 'data', 'logs']);
149
150 4
        if (!is_dir($logDir)) {
151
            throw new \RuntimeException(sprintf('Log directory %s does not exists or invalid.', $logDir));
152
        }
153
154 4
        if (!is_readable($logDir) || !is_writable($logDir)) {
155
            throw new \RuntimeException(sprintf('Cache directory %s must have read/write access.', $logDir));
156
        }
157
158 4
        return $logDir;
159
    }
160
}
161