Passed
Push — logger ( ec2b41...fe50cc )
by Arnaud
02:58
created

AbstractCommand   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 112
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 52
dl 0
loc 112
rs 10
c 0
b 0
f 0
wmc 16

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getPath() 0 3 1
A run() 0 13 4
A initialize() 0 28 6
A getBuilder() 0 23 5
1
<?php
2
/**
3
 * This file is part of the Cecil/Cecil package.
4
 *
5
 * Copyright (c) Arnaud Ligny <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Cecil\Command;
12
13
use Cecil\Builder;
14
use Cecil\Logger\ConsoleLogger;
15
use Cecil\Util;
16
use Symfony\Component\Console\Command\Command as BaseCommand;
17
use Symfony\Component\Console\Input\InputInterface;
18
use Symfony\Component\Console\Output\OutputInterface;
19
use Symfony\Component\Console\Style\SymfonyStyle;
20
use Symfony\Component\Filesystem\Filesystem;
21
use Symfony\Component\Yaml\Exception\ParseException;
22
use Symfony\Component\Yaml\Yaml;
23
24
class AbstractCommand extends BaseCommand
25
{
26
    const CONFIG_FILE = 'config.yml';
27
    const TMP_DIR = '.cecil';
28
29
    /** @var InputInterface */
30
    protected $input;
31
    /** @var OutputInterface */
32
    protected $output;
33
    /** @var SymfonyStyle */
34
    protected $io;
35
    /** @var Filesystem */
36
    protected $fs;
37
    /** @var string */
38
    protected $path;
39
    /** @var string */
40
    protected $configFile;
41
    /** @var Builder */
42
    protected $builder;
43
44
    /**
45
     * {@inheritdoc}
46
     */
47
    protected function initialize(InputInterface $input, OutputInterface $output)
48
    {
49
        $this->input = $input;
50
        $this->output = $output;
51
        $this->io = new SymfonyStyle($input, $output);
52
        $this->fs = new Filesystem();
53
54
        if (!in_array($this->getName(), ['self-update'])) {
55
            $this->path = (string) $input->getArgument('path');
56
            if (null === $this->getPath()) {
0 ignored issues
show
introduced by
The condition null === $this->getPath() is always false.
Loading history...
57
                $this->path = getcwd();
58
            }
59
            if (false === realpath($this->getPath())) {
60
                $this->fs->mkdir($this->getPath());
61
            }
62
            $this->path = realpath($this->getPath());
63
            $this->configFile = Util::joinFile($this->getPath(), self::CONFIG_FILE);
64
65
            if (!in_array($this->getName(), ['new:site'])) {
66
                if (!file_exists($this->configFile)) {
67
                    $message = sprintf('Could not find "%s" file in "%s"', self::CONFIG_FILE, $this->getPath());
68
69
                    throw new \InvalidArgumentException($message);
70
                }
71
            }
72
        }
73
74
        parent::initialize($input, $output);
75
    }
76
77
    /**
78
     * {@inheritdoc}
79
     */
80
    public function run(InputInterface $input, OutputInterface $output)
81
    {
82
        if ($output->isDebug()) {
83
            parent::run($input, $output);
84
        }
85
        // simplifying error message
86
        try {
87
            parent::run($input, $output);
88
        } catch (\Exception $e) {
89
            if ($this->io === null) {
90
                $this->io = new SymfonyStyle($input, $output);
91
            }
92
            $this->io->error($e->getMessage());
93
        }
94
    }
95
96
    /**
97
     * Returns the working directory.
98
     *
99
     * @return string|null
100
     */
101
    protected function getPath(): ?string
102
    {
103
        return $this->path;
104
    }
105
106
    /**
107
     * Creates or returns a Builder instance.
108
     *
109
     * @param array $config
110
     *
111
     * @return Builder
112
     */
113
    protected function getBuilder(array $config = []): Builder
114
    {
115
        if (!file_exists($this->configFile)) {
116
            throw new \Exception(sprintf('Configuration file not found in "%s"!', $this->getPath()));
117
        }
118
119
        try {
120
            $configContent = Util::fileGetContents($this->configFile);
121
            if ($configContent === false) {
122
                throw new \Exception('Can\'t read the configuration file.');
123
            }
124
            $siteConfig = Yaml::parse($configContent);
125
            $config = array_replace_recursive($siteConfig, $config);
126
            $this->builder = (new Builder($config, new ConsoleLogger($this->output)))
127
                ->setSourceDir($this->getPath())
128
                ->setDestinationDir($this->getPath());
129
        } catch (ParseException $e) {
130
            throw new \Exception(sprintf('Configuration file parse error: %s', $e->getMessage()));
131
        } catch (\Exception $e) {
132
            throw new \Exception($e->getMessage());
133
        }
134
135
        return $this->builder;
136
    }
137
}
138