Completed
Push — include-lib ( fd24a6...4173d3 )
by Arnaud
13:24
created

ImportConfig   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 6 2
A process() 0 26 4
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 ImportConfig extends AbstractStep
19
{
20
    const THEME_CONFIG_FILE = 'config.yml';
21
22
    /**
23
     * {@inheritdoc}
24
     *
25
     * @throws Exception
26
     */
27
    public function init($options)
28
    {
29
        if ($this->config->hasTheme()) {
30
            $this->process = true;
31
        }
32
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37
    public function process()
38
    {
39
        call_user_func_array($this->builder->getMessageCb(), ['CONFIG', 'Importing config']);
40
41
        try {
42
            $themes = array_reverse($this->config->getTheme());
43
            $count = 0;
44
            $max = count($themes);
45
            foreach ($themes as $theme) {
46
                $count++;
47
                $themeConfigFile = $this->config->getThemesPath().'/'.$theme.'/'.self::THEME_CONFIG_FILE;
48
                if (Util::getFS()->exists($themeConfigFile)) {
49
                    $themeConfig = Yaml::parse(
50
                        file_get_contents($themeConfigFile)
51
                    );
52
                    $this->config->import($themeConfig);
53
                    $message = sprintf('%s: config imported', $theme);
54
                } else {
55
                    $message = sprintf('%s: no config file', $theme);
56
                }
57
                call_user_func_array($this->builder->getMessageCb(), ['CONFIG_PROGRESS', $message, $count, $max]);
58
            }
59
        } catch (Exception $e) {
60
            echo $e->getMessage()."\n";
61
        }
62
    }
63
}
64