Passed
Push — fallback-intl ( 2f4c17...1f6a03 )
by Arnaud
03:32
created

Import::process()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 19
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 14
nc 4
nop 0
dl 0
loc 19
rs 9.7998
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of Cecil.
7
 *
8
 * Copyright (c) Arnaud Ligny <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Cecil\Step\Themes;
15
16
use Cecil\Exception\RuntimeException;
17
use Cecil\Step\AbstractStep;
18
use Cecil\Util;
19
use Symfony\Component\Yaml\Yaml;
20
21
/**
22
 * Imports (themes) configuration.
23
 */
24
class Import extends AbstractStep
25
{
26
    const THEME_CONFIG_FILE = 'config.yml';
27
28
    /**
29
     * {@inheritdoc}
30
     */
31
    public function getName(): string
32
    {
33
        return 'Importing themes configuration';
34
    }
35
36
    /**
37
     * {@inheritdoc}
38
     */
39
    public function init(array $options): void
40
    {
41
        if ($this->config->hasTheme()) {
42
            $this->canProcess = true;
43
        }
44
    }
45
46
    /**
47
     * {@inheritdoc}
48
     *
49
     * @throws RuntimeException
50
     */
51
    public function process(): void
52
    {
53
        $themes = array_reverse((array) $this->config->getTheme());
54
        $count = 0;
55
        $max = count($themes);
56
        foreach ($themes as $theme) {
57
            $count++;
58
            $themeConfigFile = $this->config->getThemesPath().'/'.$theme.'/'.self::THEME_CONFIG_FILE;
59
            $message = \sprintf('"%s": no configuration file', $theme);
60
            if (Util\File::getFS()->exists($themeConfigFile)) {
61
                if (false === $config = Util\File::fileGetContents($themeConfigFile)) {
62
                    throw new RuntimeException('Can\'t read the configuration file.');
63
                }
64
                $themeConfig = Yaml::parse($config);
65
                $this->config->import($themeConfig);
66
                $message = \sprintf('Theme "%s" imported', $theme);
67
            }
68
69
            $this->builder->getLogger()->info($message, ['progress' => [$count, $max]]);
70
        }
71
    }
72
}
73