ConfigProvider::getDependencies()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 40
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2.0932

Importance

Changes 0
Metric Value
eloc 19
dl 0
loc 40
ccs 5
cts 7
cp 0.7143
rs 9.6333
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 2.0932
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
     * @psalm-suppress TypeDoesNotContainNull
54
     *
55
     * @throws DomainException
56
     *
57 8
     * @return array<string, array>
58
     */
59 8
    public function getDependencies(): array
60 8
    {
61
        $arr = array_replace_recursive(
62
            (new VideoConfigProvider())->getDependencies(),
63 8
            [
64
                'abstract_factories' => [
65
                    ReflectionBasedAbstractFactory::class,
66
                ],
67
68
                'factories'  => [
69
                    // Services
70
                    MediaToolsServiceInterface::class => MediaToolsServiceFactory::class,
71
                    PresetLoader::class               => PresetLoaderFactory::class,
72
73
                    // Commands
74
                    ConvertDirCommand::class => ConvertDirCommandFactory::class,
75
                    ScanCommand::class       => ScanCommandFactory::class,
76
77
                    // Infra
78
                    LoggerInterface::class      => StandardFileLoggerFactory::class,
79
                    CacheInterface::class       => StandardFileCacheFactory::class,
80
81
                    // Media Utils
82
                    FileExtensions::class       => FileExtenstionsFactory::class,
83
84
                    // Presets
85
                    GoogleVod2019Preset::class  => ReflectionBasedAbstractFactory::class,
86
                    StreamableH264Preset::class => ReflectionBasedAbstractFactory::class,
87
                    ResolvePreset::class        => ReflectionBasedAbstractFactory::class,
88
                ],
89
            ]
90 8
        );
91
92
        if ($arr === null) {
93
            throw new DomainException(sprintf(
94
                'Could not merge ConfigProvider configurations.'
95
            ));
96 8
        }
97
98
        return $arr;
99
    }
100
101
    /**
102
     * Return build console commands class names.
103
     *
104 1
     * @return string[]
105
     */
106
    public function getConsoleCommands(): array
107 1
    {
108
        return [
109
            ScanCommand::class,
110
            ConvertDirCommand::class,
111
        ];
112
    }
113
114
    /**
115 7
     * @throws \RuntimeException
116
     */
117 7
    public static function getProjectBaseDirectory(): string
118 7
    {
119
        $baseDir = dirname(__FILE__, 4);
120
        if (!is_dir($baseDir)) {
121
            throw new \RuntimeException(sprintf('Cannot get project base directory %s', $baseDir));
122 7
        }
123
124
        return $baseDir;
125
    }
126
127
    /**
128 7
     * @throws \RuntimeException
129
     */
130 7
    public static function getProjectCacheDirectory(): string
131
    {
132 7
        $cacheDir = implode(DIRECTORY_SEPARATOR, [self::getProjectBaseDirectory(), 'data', 'cache']);
133
134
        if (!is_dir($cacheDir)) {
135
            throw new \RuntimeException(sprintf('Cache directory %s does not exists or invalid.', $cacheDir));
136 7
        }
137
138
        if (!is_readable($cacheDir) || !is_writable($cacheDir)) {
139
            throw new \RuntimeException(sprintf('Cache directory %s must have read/write access.', $cacheDir));
140 7
        }
141
142
        return $cacheDir;
143
    }
144
145
    /**
146 7
     * @throws \RuntimeException
147
     */
148 7
    public static function getProjectLogDirectory(): string
149
    {
150 7
        $logDir = implode(DIRECTORY_SEPARATOR, [self::getProjectBaseDirectory(), 'data', 'logs']);
151
152
        if (!is_dir($logDir)) {
153
            throw new \RuntimeException(sprintf('Log directory %s does not exists or invalid.', $logDir));
154 7
        }
155
156
        if (!is_readable($logDir) || !is_writable($logDir)) {
157
            throw new \RuntimeException(sprintf('Cache directory %s must have read/write access.', $logDir));
158 7
        }
159
160
        return $logDir;
161
    }
162
}
163