ExportCommand   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 27
c 1
b 0
f 1
dl 0
loc 60
rs 10
wmc 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A onConstruct() 0 8 1
A execute() 0 31 5
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
class ExportCommand extends BaseCommand
15
{
16
    /** @var string Command name */
17
    protected $_name = 'export';
18
19
    /** @var string Command description */
20
    protected $_desc = 'Export factory templates so you can customize and use them';
21
22
    /**
23
     * Configure the command options/arguments.
24
     *
25
     * @return void
26
     */
27
    protected function onConstruct()
28
    {
29
        $this
30
            ->option('-t --to <directory>', 'Output directory')
31
            ->option('-o --overwrite', 'Overwrite if target file exists', 'boolval', false)
32
            ->usage(
33
                '<bold>  phint export</end> -t .        Exports to current dir<eol/>' .
34
                '<bold>  phint e</end> <comment>--to ~/myphint</end>   Exports to ~/myphint dir<eol/>'
35
            );
36
    }
37
38
    /**
39
     * Generate test stubs.
40
     *
41
     * @return void
42
     */
43
    public function execute()
44
    {
45
        $io  = $this->app()->io();
46
        $res = __DIR__ . '/../../resources';
47
        $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

47
        $dir = $this->_pathUtil->expand(/** @scrutinizer ignore-type */ $this->to, $this->_workDir);
Loading history...
48
49
        $this->_pathUtil->ensureDir($dir);
50
51
        $count     = 0;
52
        $templates = $this->_pathUtil->findFiles([$res], '.twig', true);
53
54
        $io->comment('Exporting ...', true);
55
56
        foreach ($templates as $template) {
57
            $target = \str_replace($res, $dir, $template);
58
59
            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...
60
                continue;
61
            }
62
63
            $content = \file_get_contents($template);
64
            $count += (int) $this->_pathUtil->writeFile($target, $content);
65
        }
66
67
        $io->cyan("$count template(s) copied to {$this->to}", true);
68
        if ($count) {
69
            $io->comment('Now you can customize those templates and use like so:', true);
70
            $io->bold('  phint init --template ' . $this->_pathUtil->expand($this->to), true);
71
        }
72
73
        $io->ok('Done', true);
74
    }
75
}
76