Passed
Push — extract ( b893c4 )
by Arnaud
03:51
created

UtilExtractTemplates::execute()   A

Complexity

Conditions 3
Paths 14

Size

Total Lines 21
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 14
c 1
b 0
f 0
nc 14
nop 2
dl 0
loc 21
rs 9.7998
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of Cecil.
7
 *
8
 * Copyright (c) Arnaud Ligny <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Cecil\Command;
15
16
use Cecil\Exception\RuntimeException;
17
use Cecil\Util;
18
use Symfony\Component\Console\Input\InputArgument;
19
use Symfony\Component\Console\Input\InputDefinition;
20
use Symfony\Component\Console\Input\InputInterface;
21
use Symfony\Component\Console\Output\OutputInterface;
22
use Symfony\Component\Finder\Finder;
23
24
/**
25
 * Extract built-in templates.
26
 */
27
class UtilExtractTemplates extends AbstractCommand
28
{
29
    /**
30
     * {@inheritdoc}
31
     */
32
    protected function configure()
33
    {
34
        $this
35
            ->setName('util:extract')
36
            ->setDescription('Extracts built-in templates')
37
            ->setDefinition(
38
                new InputDefinition([
39
                    new InputArgument('path', InputArgument::OPTIONAL, 'Use the given path as working directory'),
40
                ])
41
            )
42
            ->setHelp('Extracts built-in templates in the "layouts" directory.');
43
    }
44
45
    /**
46
     * {@inheritdoc}
47
     *
48
     * @throws RuntimeException
49
     */
50
    protected function execute(InputInterface $input, OutputInterface $output)
51
    {
52
        try {
53
            $phar = new \Phar(Util\Plateform::getPharPath());
54
55
            $templates = Finder::create()
56
            ->files()
57
            ->in($this->getBuilder()->getConfig()->getInternalLayoutsPath());
58
            foreach ($templates as $template) {
59
                $templatesList[] = Util::joinPath($this->getBuilder()->getConfig()->get('layouts.internal.dir'), Util\File::getFS()->makePathRelative($template->getPathname(), $this->getBuilder()->getConfig()->getInternalLayoutsPath()));
0 ignored issues
show
Bug introduced by
It seems like $this->getBuilder()->get...'layouts.internal.dir') can also be of type null; however, parameter $path of Cecil\Util::joinPath() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

59
                $templatesList[] = Util::joinPath(/** @scrutinizer ignore-type */ $this->getBuilder()->getConfig()->get('layouts.internal.dir'), Util\File::getFS()->makePathRelative($template->getPathname(), $this->getBuilder()->getConfig()->getInternalLayoutsPath()));
Loading history...
60
            }
61
62
            $phar->extractTo($this->getBuilder()->getConfig()->getLayoutsPath(), $templatesList);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $templatesList seems to be defined by a foreach iteration on line 58. Are you sure the iterator is never empty, otherwise this variable is not defined?
Loading history...
63
            Util\File::getFS()->mirror(Util::joinPath($this->getBuilder()->getConfig()->getLayoutsPath(), $this->getBuilder()->getConfig()->get('layouts.internal.dir')), $this->getBuilder()->getConfig()->getLayoutsPath());
64
            Util\File::getFS()->remove(Util::joinPath($this->getBuilder()->getConfig()->getLayoutsPath(), 'resources'));
65
            $output->writeln(\sprintf('<info>Built-in templates extracted to "%s".</info>', $this->getBuilder()->getConfig()->get('layouts.dir')));
66
        } catch (\Exception $e) {
67
            throw new RuntimeException(\sprintf($e->getMessage()));
68
        }
69
70
        return 0;
71
    }
72
}
73