Completed
Pull Request — master (#46)
by Jitendra
01:52
created

ExportCommand::execute()   A

Complexity

Conditions 5
Paths 6

Size

Total Lines 31
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 18
nc 6
nop 0
dl 0
loc 31
rs 9.3554
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the PHINT package.
5
 *
6
 * (c) Jitendra Adhikari <[email protected]>
7
 *     <https://github.com/adhocore>
8
 *
9
 * Licensed under MIT license.
10
 */
11
12
namespace Ahc\Phint\Console;
13
14
use Ahc\Phint\Generator\TwigGenerator;
15
16
class ExportCommand extends BaseCommand
17
{
18
    /** @var string Command name */
19
    protected $_name = 'export';
20
21
    /** @var string Command description */
22
    protected $_desc = 'Export factory templates so you can customize and use them';
23
24
    /**
25
     * Configure the command options/arguments.
26
     *
27
     * @return void
28
     */
29
    protected function onConstruct()
30
    {
31
        $this
32
            ->option('-t --to <directory>', 'Output directory')
33
            ->option('-o --overwrite', 'Overwrite if target file exists', 'boolval', false)
34
            ->usage(
35
                '<bold>  phint export</end> -t .        Exports to current dir<eol/>' .
36
                '<bold>  phint e</end> <comment>--to ~/myphint</end>   Exports to ~/myphint dir<eol/>'
37
            );
38
    }
39
40
    /**
41
     * Generate test stubs.
42
     *
43
     * @return void
44
     */
45
    public function execute()
46
    {
47
        $io  = $this->app()->io();
48
        $res = \realpath(__DIR__ . '/../../resources');
49
        $dir = $this->_pathUtil->expand($this->to, $this->_workDir);
0 ignored issues
show
Bug Best Practice introduced by
The property to does not exist on Ahc\Phint\Console\ExportCommand. Since you implemented __get, consider adding a @property annotation.
Loading history...
Bug introduced by
It seems like $this->to can also be of type null; however, parameter $path of Ahc\Phint\Util\Path::expand() 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

49
        $dir = $this->_pathUtil->expand(/** @scrutinizer ignore-type */ $this->to, $this->_workDir);
Loading history...
50
51
        $this->_pathUtil->ensureDir($dir);
52
53
        $count     = 0;
54
        $templates = $this->_pathUtil->findFiles([$res], '.twig', true);
55
56
        $io->comment('Exporting ...', true);
57
58
        foreach ($templates as $template) {
59
            $target = \str_replace($res, $dir, $template);
60
61
            if (\is_file($target) && !$this->overwrite) {
0 ignored issues
show
Bug Best Practice introduced by
The property overwrite does not exist on Ahc\Phint\Console\ExportCommand. Since you implemented __get, consider adding a @property annotation.
Loading history...
62
                continue;
63
            }
64
65
            $content = \file_get_contents($template);
66
            $count  += (int) $this->_pathUtil->writeFile($target, $content);
67
        }
68
69
        $io->cyan("$count template(s) copied to {$this->to}", true);
70
        if ($count) {
71
            $io->comment('Now you can customize those templates and use like so:', true);
72
            $io->bold('  phint init --template ' . $this->_pathUtil->expand($this->to), true);
73
        }
74
75
        $io->ok('Done', true);
76
    }
77
}
78