Completed
Push — master ( 26640c...8751ed )
by Sebastian
13s
created

Backup::validateConfig()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 8
cts 8
cp 1
rs 9.7333
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 2
1
<?php
2
namespace phpbu\Laravel\Cmd;
3
4
use Illuminate\Console\Command;
5
use phpbu\App\Configuration\Loader\Factory as PhpbuConfigLoaderFactory;
6
use phpbu\App\Factory as PhpbuFactory;
7
use phpbu\App\Runner;
8
use phpbu\Laravel\Configuration;
9
use phpbu\Laravel\Configuration\Translator;
10
use phpbu\Laravel\Configuration\Proxy;
11
use Symfony\Component\Console\Input\InputOption;
12
13
/**
14
 * Class Backup
15
 *
16
 * @package    phpbu\Laravel
17
 * @author     Sebastian Feldmann <[email protected]>
18
 * @copyright  Sebastian Feldmann <[email protected]>
19
 * @license    http://www.opensource.org/licenses/MIT The MIT License (MIT)
20
 * @link       http://phpbu.de/
21
 */
22
class Backup extends Command
23
{
24
    /**
25
     * The console command name.
26
     *
27
     * @var string
28
     */
29
    protected $name = 'phpbu:backup';
30
31
    /**
32
     * The console command description.
33
     *
34
     * @var string
35
     */
36
    protected $description = 'Execute the backup';
37
38
    /**
39
     * phpbu App runner.
40
     *
41
     * @var \phpbu\App\Runner
42
     */
43
    private $runner;
44
45
    /**
46
     * @var \phpbu\Laravel\Configuration\Proxy
47
     */
48
    private $configProxy;
49
50
    /**
51
     * Constructor.
52
     *
53
     * @param \phpbu\App\Runner                  $runner      Runner to execute the backups
54
     * @param \phpbu\Laravel\Configuration\Proxy $configProxy Laravel configuration proxy
55
     */
56 3
    public function __construct(Runner $runner, Proxy $configProxy)
57
    {
58 3
        $this->runner      = $runner;
59 3
        $this->configProxy = $configProxy;
60
61 3
        parent::__construct();
62 3
    }
63
64
    /**
65
     * Execute the console command.
66
     *
67
     * @throws \Exception
68
     * @return bool
69
     */
70 3
    public function fire()
71
    {
72 3
        $configuration = $this->createConfiguration();
73
74
        // add a printer for some output
75 2
        $configuration->addLogger($this->createPrinter());
76
77
        // finally execute the backup
78 2
        $result = $this->runner->run($configuration);
79
80 2
        return $result->wasSuccessful();
81
    }
82
83
    /**
84
     * @throws \Exception
85
     * @return bool
86
     */
87 3
    public function handle()
88
    {
89 3
        return $this->fire();
90
    }
91
92
    /**
93
     * Creates a phpbu configuration.
94
     *
95
     * @return \phpbu\App\Configuration
96
     */
97 3
    protected function createConfiguration()
98
    {
99
        // check if a phpbu xml/json config file is configured
100 3
        $phpbuConfigFile = $this->configProxy->get('phpbu.phpbu');
101 3
        if (!empty($phpbuConfigFile)) {
102
            // load xml or json configurations
103 1
            $configLoader  = PhpbuConfigLoaderFactory::createLoader($phpbuConfigFile);
104 1
            $configuration = $configLoader->getConfiguration($this->runner->getFactory());
105
        } else {
106 2
            $this->validateConfig();
107
            // no phpbu config so translate the laravel settings
108 1
            $translator    = new Translator();
109 1
            $configuration = $translator->translate($this->configProxy);
110 1
            $configuration->setSimulate((bool) $this->option('phpbu-simulate'));
111
            // in laravel mode we sync everything using the Laravel Filesystems
112 1
            PhpbuFactory::register('sync', 'laravel-storage', '\\phpbu\\Laravel\\Backup\\Sync\\LaravelStorage');
113
        }
114 2
        return $configuration;
115
    }
116
117
    /**
118
     * Make sure we have a valid configuration.
119
     *
120
     * @throws \phpbu\Laravel\Configuration\Exception
121
     */
122 2
    protected function validateConfig()
123
    {
124 2
        if (!$this->configProxy->keysExist(
125
            [
126 2
                'phpbu.config',
127
                'phpbu.directories',
128
                'phpbu.databases'
129
            ]
130
        )) {
131 1
            throw new Configuration\Exception(
132 1
                'invalid configuration' . PHP_EOL .
133 1
                'please use the \'phpbu.php\' configuration file provided by \'phpbu-laravel\'' . PHP_EOL .
134 1
                'for details visit phpbu.de'
135
            );
136
        }
137 1
    }
138
139
    /**
140
     * Create a logger/printer to do some output.
141
     *
142
     * @return \phpbu\Laravel\Cmd\Printer
143
     */
144 2
    protected function createPrinter()
145
    {
146 2
        $verbose  = (bool) $this->option('phpbu-verbose');
147 2
        $debug    = (bool) $this->option('phpbu-debug');
148 2
        $simulate = (bool) $this->option('phpbu-simulate');
149 2
        return new Printer($this, $verbose, ($debug || $simulate));
150
    }
151
152
    /**
153
     * Get the console command options.
154
     *
155
     * @return array
156
     */
157 3
    protected function getOptions()
158
    {
159
        return [
160 3
            ['phpbu-simulate', null, InputOption::VALUE_NONE, 'Perform a trial run with no changes made.'],
161 3
            ['phpbu-verbose', null, InputOption::VALUE_NONE, 'Output more verbose information.'],
162 3
            ['phpbu-debug', null, InputOption::VALUE_NONE, 'Display debugging information during backup generation.'],
163
        ];
164
    }
165
}
166