Completed
Push — code ( eb95c2 )
by Arnaud
02:14
created

ConfigImport   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 6
dl 0
loc 45
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 6 2
A process() 0 27 3
1
<?php
2
/*
3
 * Copyright (c) Arnaud Ligny <[email protected]>
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
9
namespace Cecil\Step;
10
11
use Cecil\Exception\Exception;
12
use Cecil\Util;
13
use Symfony\Component\Yaml\Yaml;
14
15
/**
16
 * Import (themes) config.
17
 */
18
class ConfigImport extends AbstractStep
19
{
20
    const THEME_CONFIG_FILE = 'config.yml';
21
22
    /**
23
     * {@inheritdoc}
24
     */
25
    public function init($options)
26
    {
27
        if ($this->config->hasTheme()) {
28
            $this->process = true;
29
        }
30
    }
31
32
    /**
33
     * {@inheritdoc}
34
     */
35
    public function process()
36
    {
37
        call_user_func_array($this->builder->getMessageCb(), ['CONFIG', 'Importing config']);
38
39
        $themes = array_reverse($this->config->getTheme());
40
        $count = 0;
41
        $max = count($themes);
42
        foreach ($themes as $theme) {
43
            $count++;
44
            $themeConfigFile = $this->config->getThemesPath().'/'.$theme.'/'.self::THEME_CONFIG_FILE;
45
            if (Util::getFS()->exists($themeConfigFile)) {
46
                set_error_handler(
47
                    function ($severity, $message, $file, $line) {
48
                        throw new \ErrorException($message, $severity, $severity, $file, $line);
49
                    }
50
                );
51
                $config = file_get_contents($themeConfigFile);
52
                restore_error_handler();
53
                $themeConfig = Yaml::parse($config);
54
                $this->config->import($themeConfig);
55
                $message = sprintf('%s: config imported', $theme);
56
            } else {
57
                $message = sprintf('%s: no config file', $theme);
58
            }
59
            call_user_func_array($this->builder->getMessageCb(), ['CONFIG_PROGRESS', $message, $count, $max]);
60
        }
61
    }
62
}
63