InstallCommand   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 5 1
B execute() 0 41 5
A getSrcDir() 0 17 3
A getDestDir() 0 4 1
1
<?php
2
3
namespace Braincrafted\Bundle\BootstrapBundle\Command;
4
5
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
6
use Symfony\Component\Console\Input\InputInterface;
7
use Symfony\Component\Console\Output\OutputInterface;
8
use Symfony\Component\Finder\Finder;
9
use Symfony\Component\Filesystem\Filesystem;
10
use Symfony\Component\Filesystem\Exception\IOException;
11
12
/**
13
 * InstallCommand
14
 *
15
 * @package    BraincraftedBootstrapBundle
16
 * @subpackage Command
17
 * @author     Florian Eckerstorfer <[email protected]>
18
 * @copyright  2012-2013 Florian Eckerstorfer
19
 * @license    http://opensource.org/licenses/MIT The MIT License
20
 * @link       http://bootstrap.braincrafted.com BraincraftedBootst
21
 */
22
class InstallCommand extends ContainerAwareCommand
23
{
24
    /**
25
     * {@inheritDoc}
26
     */
27
    protected function configure()
28
    {
29
        $this->setName('braincrafted:bootstrap:install')
30
            ->setDescription('Installs the icon font');
31
    }
32
33
    /**
34
     * {@inheritDoc}
35
     */
36
    protected function execute(InputInterface $input, OutputInterface $output)
37
    {
38
        $destDir = $this->getDestDir();
39
40
        $finder = new Finder;
41
        $fs = new Filesystem;
42
43
        try {
44
            $fs->mkdir($destDir);
45
        } catch (IOException $e) {
46
            $output->writeln(sprintf('<error>Could not create directory %s.</error>', $destDir));
47
48
            return;
49
        }
50
51
        $srcDir = $this->getSrcDir();
52
        if (false === file_exists($srcDir)) {
53
            $output->writeln(sprintf(
54
                '<error>Fonts directory "%s" does not exist. Did you install twbs/bootstrap? '.
55
                'If you used something other than Composer you need to manually change the path in '.
56
                '"braincrafted_bootstrap.assets_dir". If you want to use Font Awesome you need to install the font and change the option "braincrafted_bootstrap.fontawesome_dir".</error>',
57
                $srcDir
58
            ));
59
60
            return;
61
        }
62
        $finder->files()->in($srcDir);
63
64
        foreach ($finder as $file) {
65
            $dest = sprintf('%s/%s', $destDir, $file->getBaseName());
66
            try {
67
                $fs->copy($file, $dest);
68
            } catch (IOException $e) {
69
                $output->writeln(sprintf('<error>Could not copy %s</error>', $file->getBaseName()));
70
71
                return;
72
            }
73
        }
74
75
        $output->writeln(sprintf('Copied icon fonts to <comment>%s</comment>.', $destDir));
76
    }
77
78
    /**
79
     * @return string
80
     */
81
    protected function getSrcDir()
82
    {
83
        if ('fa' === $this->getContainer()->getParameter('braincrafted_bootstrap.icon_prefix')) {
84
            return sprintf('%s/fonts', $this->getContainer()->getParameter('braincrafted_bootstrap.fontawesome_dir'));
85
        }
86
87
        return sprintf(
88
            '%s/%s',
89
            $this->getContainer()->getParameter('braincrafted_bootstrap.assets_dir'),
90
            (
91
                // Sass version stores fonts in a different directory
92
                in_array($this->getContainer()->getParameter('braincrafted_bootstrap.css_preprocessor'), array('sass', 'scssphp')) ?
93
                'fonts/bootstrap' :
94
                'fonts'
95
            )
96
        );
97
    }
98
99
    /**
100
     * @return string
101
     */
102
    protected function getDestDir()
103
    {
104
        return $this->getContainer()->getParameter('braincrafted_bootstrap.fonts_dir');
105
    }
106
}
107