Passed
Push — master ( 23ad8b...41e1cb )
by Bruno
05:36
created

WriterTrait::writeFile()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 11
dl 0
loc 18
rs 9.6111
c 1
b 0
f 0
cc 5
nc 5
nop 3
1
<?php declare(strict_types=1);
2
3
namespace Modelarium\Laravel\Console\Commands;
4
5
use Modelarium\GeneratedCollection;
6
7
trait WriterTrait
8
{
9
    public function writeFiles(GeneratedCollection $collection, string $basepath, bool $overwrite = true): self
10
    {
11
        foreach ($collection as $element) {
12
            /**
13
             * @var GeneratedItem $element
14
             */
15
            $path = $basepath . '/' . $element->filename;
16
            $this->writeFile(
17
                $path,
18
                ($element->onlyIfNewFile ? false : $overwrite),
19
                $element->contents
20
            );
21
        }
22
        return $this;
23
    }
24
25
    /**
26
     * Takes a stub file and generates the target file with replacements.
27
     *
28
     * @param string $targetPath The path for the stub file.
29
     * @param boolean $overwrite
30
     * @param string $data The data to write
31
     * @return void
32
     */
33
    protected function writeFile(string $targetPath, bool $overwrite, string $data)
34
    {
35
        if (file_exists($targetPath) && !$overwrite) {
36
            $this->comment("File $targetPath already exists, not overwriting.");
0 ignored issues
show
Bug introduced by
It seems like comment() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

36
            $this->/** @scrutinizer ignore-call */ 
37
                   comment("File $targetPath already exists, not overwriting.");
Loading history...
37
            return;
38
        }
39
40
        $dir = dirname($targetPath);
41
        if (!is_dir($dir)) {
42
            \Safe\mkdir($dir, 0777, true);
43
        }
44
45
        $ret = \Safe\file_put_contents($targetPath, $data);
46
        if (!$ret) {
47
            $this->error("Cannot write to $targetPath");
0 ignored issues
show
Bug introduced by
It seems like error() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

47
            $this->/** @scrutinizer ignore-call */ 
48
                   error("Cannot write to $targetPath");
Loading history...
48
            return;
49
        }
50
        $this->line("Wrote $targetPath");
0 ignored issues
show
Bug introduced by
It seems like line() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

50
        $this->/** @scrutinizer ignore-call */ 
51
               line("Wrote $targetPath");
Loading history...
51
    }
52
}
53