Completed
Push — master ( 7a3330...a6bb7a )
by Dorian
01:45
created

Kernel   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 8

Importance

Changes 0
Metric Value
wmc 8
lcom 0
cbo 8
dl 0
loc 75
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
B __invoke() 0 30 5
A getProjectDir() 0 8 2
A getRootPathPart() 0 12 1
1
<?php declare(strict_types=1);
2
3
namespace App;
4
5
use App\Domain\Content;
6
use App\Domain\PathPart;
7
use App\UI\UserInterface;
8
use Symfony\Component\Filesystem\Filesystem;
9
use Symfony\Component\Yaml\Yaml;
10
11
final class Kernel
12
{
13
    /**
14
     * @param \App\UI\UserInterface $ui
15
     *
16
     * @throws \RuntimeException
17
     * @throws \Symfony\Component\Filesystem\Exception\IOException
18
     * @throws \Symfony\Component\Yaml\Exception\ParseException
19
     */
20
    public function __invoke(UserInterface $ui): void
21
    {
22
        $config = (array) Yaml::parseFile($this->getProjectDir().DIRECTORY_SEPARATOR.'config/app.yml');
23
        $rootPathPart = $this->getRootPathPart($config['path_part']);
24
25
        foreach ((array) $config['sources'] as $sources) {
26
            foreach ((array) $sources as $sourceClassName => $sourceData) {
27
28
                /** @var \App\Domain\Source $source */
29
                $source = new $sourceClassName($ui, $sourceData['config'] ?? []);
30
31
                // Add the root path part to the contents' path
32
                $contents = $source->getContents()
33
                    ->map(function (Content $content) use ($rootPathPart) {
34
                        $content->getPath()->add($rootPathPart);
35
36
                        return $content;
37
                    });
38
39
                foreach ((array) $sourceData['platforms'] as $platforms) {
40
                    foreach ((array) $platforms as $platformClassName => $platformConfig) {
41
42
                        /** @var \App\Domain\Platform $platform */
43
                        $platform = new $platformClassName($ui, $platformConfig ?? []);
44
                        $platform->synchronizeContents($contents, $rootPathPart);
1 ignored issue
show
Bug introduced by
It seems like $contents defined by $source->getContents()->... return $content; }) on line 32 can also be of type array<integer,object<App\Domain\Content>>; however, App\Domain\Platform::synchronizeContents() does only seem to accept object<App\Domain\Collection\Contents>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
45
                    }
46
                }
47
            }
48
        }
49
    }
50
51
    /**
52
     * @return string
53
     *
54
     * @throws \RuntimeException
55
     */
56
    private function getProjectDir(): string
57
    {
58
        if (!($projectRoot = getenv('PROJECT_ROOT'))) {
59
            throw new \RuntimeException('The environment variable "PROJECT_ROOT" has not been defined.');
60
        }
61
62
        return rtrim($projectRoot, DIRECTORY_SEPARATOR);
63
    }
64
65
    /**
66
     * @param array $config
67
     *
68
     * @return \App\Domain\PathPart
69
     *
70
     * @throws \RuntimeException
71
     * @throws \Symfony\Component\Filesystem\Exception\IOException
72
     */
73
    private function getRootPathPart(array $config): PathPart
74
    {
75
        $config['substitutions'] = [
76
                '%project_root%' => $this->getProjectDir()
77
            ] + ($config['substitutions'] ?? []);
78
        $rootPathPart = new PathPart($config);
79
80
        // Try to create the root directory... 'cause if it fails, nothing will work.
81
        (new Filesystem())->mkdir($rootPathPart->getPath());
82
83
        return $rootPathPart;
84
    }
85
}
86