Completed
Push — master ( 530ce9...1b2ae5 )
by Paweł
23:42 queued 20:31
created

ThemeSetupCommand::configure()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 42
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 42
rs 8.8571
cc 1
eloc 23
nc 1
nop 0
1
<?php
2
3
/**
4
 * This file is part of the Superdesk Web Publisher Fixtures Bundle.
5
 *
6
 * Copyright 2015 Sourcefabric z.u. and contributors.
7
 *
8
 * For the full copyright and license information, please see the
9
 * AUTHORS and LICENSE files distributed with this source code.
10
 *
11
 * @copyright 2015 Sourcefabric z.ú.
12
 * @license http://www.superdesk.org/license
13
 */
14
namespace SWP\Bundle\FixturesBundle\Command;
15
16
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
17
use Symfony\Component\Console\Input\InputArgument;
18
use Symfony\Component\Console\Input\InputInterface;
19
use Symfony\Component\Console\Output\OutputInterface;
20
use Symfony\Component\Console\Input\InputOption;
21
use Symfony\Component\Filesystem\Filesystem;
22
use Symfony\Component\Console\Question\ConfirmationQuestion;
23
24
class ThemeSetupCommand extends ContainerAwareCommand
25
{
26
    const DEFAULT_THEME_NAME = 'theme_1';
27
28
    /**
29
     * {@inheritdoc}
30
     */
31
    protected function configure()
32
    {
33
        $this
34
            ->setName('theme:setup')
35
            ->setDescription('Sets (copies) demo theme for development purposes.')
36
            ->addArgument(
37
                'name',
38
                InputArgument::OPTIONAL,
39
                'Theme name',
40
                null
41
            )
42
            ->addOption(
43
                'delete',
44
                null,
45
                InputOption::VALUE_NONE,
46
                'If set, theme will be removed from the application.'
47
            )
48
            ->addOption(
49
                'force',
50
                'f',
51
                InputOption::VALUE_NONE,
52
                'If set, forces to execute an action without confirmation.'
53
            )
54
            ->setHelp(
55
<<<'EOT'
56
The <info>theme:setup</info> command copies theme to your application themes folder (app/Resources/themes):
57
58
  <info>./app/console theme:setup</info>
59
60
You can also optionally specify the delete (<info>--delete</info>) option to delete theme by name:
61
62
  <info>./app/console theme:setup <name> --delete</info>
63
64
To force an action, you need to add an option: <info>--force</info>:
65
66
  <info>./app/console theme:setup <name> --delete --force</info>
67
68
Demo theme can be found in "SWPFixturesBundle/Resources/themes".
69
EOT
70
            )
71
        ;
72
    }
73
74
    /**
75
     * {@inheritdoc}
76
     */
77
    protected function execute(InputInterface $input, OutputInterface $output)
78
    {
79
        $fileSystem = new Filesystem();
80
        $kernel = $this->getContainer()->get('kernel');
81
        $name = $input->getArgument('name');
82
        $helper = $this->getHelper('question');
83
        $force = true === $input->getOption('force');
84
85
        if (!$name) {
86
            $name = $this->getActiveThemeName();
87
        }
88
89
        try {
90
            if ($input->getOption('delete')) {
91
                $question = new ConfirmationQuestion(
92
                    '<question>This will override your current theme: "'.$name.'", if exists. Continue with this action? (yes/no)<question> <comment>[yes]</comment> ',
93
                    true,
94
                    '/^(y|j)/i'
95
                );
96
97
                if (!$force) {
98
                    if (!$helper->ask($input, $output, $question)) {
99
                        return;
100
                    }
101
                }
102
103
                $fileSystem->remove($kernel->getRootDir().'/Resources/themes/'.$name);
104
105
                $output->writeln('<info>Theme "'.$name.'" has been deleted successfully!</info>');
106
107
                return true;
108
            }
109
110
            $question = new ConfirmationQuestion(
111
                '<question>This will override your current theme: "'.$name.'", if exists. Continue with this action? (yes/no)<question> <comment>[yes]</comment> ',
112
                true,
113
                '/^(y|j)/i'
114
            );
115
116
            if (!$force) {
117
                if (!$helper->ask($input, $output, $question)) {
118
                    return;
119
                }
120
            }
121
122
            $fileSystem->mirror(
123
                $kernel->locateResource('@SWPFixturesBundle/Resources/themes/'.self::DEFAULT_THEME_NAME),
124
                $kernel->getRootDir().'/Resources/themes/'.$name,
125
                null,
126
                ['override' => true, 'delete' => true]
127
            );
128
129
            $output->writeln('<info>Theme "'.$name.'" has been setup successfully!</info>');
130
        } catch (\Exception $e) {
131
            $output->writeln('<error>Theme "'.$name.'" could not be setup!</error>');
132
            $output->writeln('<error>Stacktrace: '.$e->getMessage().'</error>');
133
        }
134
    }
135
136
    private function getActiveThemeName()
137
    {
138
        return $this->getContainer()->getParameter('active_theme');
139
    }
140
}
141