CreateFilesStep::createFile()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 18
ccs 9
cts 9
cp 1
rs 9.6666
cc 3
nc 3
nop 1
crap 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Zalas\Symfify\Composer\SymfifyStep;
6
7
use Symfony\Component\Console\Output\OutputInterface;
8
use Zalas\Symfify\Composer\SymfifyStep;
9
10
class CreateFilesStep implements SymfifyStep
11
{
12
    /**
13
     * @var OutputInterface
14
     */
15
    private $output;
16
17
    /**
18
     * @var array
19
     */
20
    private $files = ['src/AppKernel.php', 'web/index.php'];
21
22 6
    public function __construct(OutputInterface $output, array $files = [])
23
    {
24 6
        $this->files = $files ?: $this->files;
25 6
        $this->output = $output;
26
    }
27
28 4
    public function __invoke()
29
    {
30 4
        foreach ($this->files as $file) {
31 4
            $this->createFile($file);
32
        }
33
    }
34
35
    /**
36
     * @param string $file
37
     */
38 4
    private function createFile($file)
39
    {
40 4
        if (\file_exists($file)) {
41 1
            $this->output->writeln(\sprintf('The file already exists: <comment>%s</comment>', $file));
42
43 1
            return;
44
        }
45
46 4
        $templateFile = __DIR__ . '/../../templates/' . $file;
47
48 4
        if (!\file_exists($templateFile)) {
49 1
            throw new \InvalidArgumentException(\sprintf('The template file not found: "%s".', $templateFile));
50
        }
51
52 3
        $this->output->writeln(\sprintf('Creating <comment>%s</comment>', $file));
53
54 3
        \file_put_contents($file, \file_get_contents($templateFile));
55
    }
56
}
57