Passed
Push — master ( 70860f...a39796 )
by Sébastien
02:15
created

ConfigProvider::getProjectLogDirectory()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 4.3731

Importance

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