Completed
Push — master ( 6f8365...5411bf )
by Sergi Tur
01:55
created

Installable::install()   B

Complexity

Conditions 7
Paths 5

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 15
rs 8.2222
cc 7
eloc 10
nc 5
nop 1
1
<?php
2
3
namespace Acacha\AdminLTETemplateLaravel\Console;
4
5
use League\Flysystem\Adapter\Local as LocalAdapter;
6
use League\Flysystem\Filesystem as Flysystem;
7
use League\Flysystem\MountManager;
8
9
/**
10
 * Class Installable.
11
 * 
12
 * @package Acacha\AdminLTETemplateLaravel\Console
13
 */
14
trait Installable
15
{
16
    /**
17
     * Install files from array.
18
     *
19
     * @param $files
20
     */
21
    private function install($files)
22
    {
23
        foreach ($files as $fileSrc => $fileDst) {
24
            if (file_exists($fileDst) && !$this->force && !$this->confirmOverwrite(basename($fileDst))) {
0 ignored issues
show
Bug introduced by
The property force does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
25
                return;
26
            }
27
            if ($this->files->isFile($fileSrc)) {
28
                $this->publishFile($fileSrc, $fileDst);
29
            } elseif ($this->files->isDirectory($fileSrc)) {
0 ignored issues
show
Bug introduced by
The property files does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
30
                $this->publishDirectory($fileSrc, $fileDst);
31
            } else {
32
                $this->error("Can't locate path: <{$fileSrc}>");
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?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
33
            }
34
        }
35
    }
36
37
    /**
38
     * @param $fileName
39
     * @param string $prompt
40
     *
41
     * @return bool
42
     */
43
    protected function confirmOverwrite($fileName, $prompt = '')
44
    {
45
        $prompt = (empty($prompt))
46
            ? $fileName.' already exists. Do you want to overwrite it? [y|N]'
47
            : $prompt;
48
49
        return $this->confirm($prompt, false);
0 ignored issues
show
Bug introduced by
The method confirm() does not exist on Acacha\AdminLTETemplateLaravel\Console\Installable. Did you maybe mean confirmOverwrite()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
50
    }
51
52
    /**
53
     * Create the directory to house the published files if needed.
54
     *
55
     * @param string $directory
56
     *
57
     * @return void
58
     */
59
    protected function createParentDirectory($directory)
60
    {
61
        if (!$this->files->isDirectory($directory)) {
62
            $this->files->makeDirectory($directory, 0755, true);
63
        }
64
    }
65
66
    /**
67
     * Publish the file to the given path.
68
     *
69
     * @param string $from
70
     * @param string $to
71
     *
72
     * @return void
73
     */
74
    protected function publishFile($from, $to)
75
    {
76
        $this->createParentDirectory(dirname($to));
77
        $this->files->copy($from, $to);
78
        $this->status($from, $to, 'File');
79
    }
80
81
    /**
82
     * Publish the directory to the given directory.
83
     *
84
     * @param string $from
85
     * @param string $to
86
     *
87
     * @return void
88
     */
89
    protected function publishDirectory($from, $to)
90
    {
91
        $manager = new MountManager([
92
            'from' => new Flysystem(new LocalAdapter($from)),
93
            'to'   => new Flysystem(new LocalAdapter($to)),
94
        ]);
95
        foreach ($manager->listContents('from://', true) as $file) {
96
            if ($file['type'] === 'file' && (!$manager->has('to://'.$file['path']) || $this->force)) {
97
                $manager->put('to://'.$file['path'], $manager->read('from://'.$file['path']));
98
            }
99
        }
100
        $this->status($from, $to, 'Directory');
101
    }
102
103
    /**
104
     * Write a status message to the console.
105
     *
106
     * @param string $from
107
     * @param string $to
108
     * @param string $type
109
     *
110
     * @return void
111
     */
112
    protected function status($from, $to, $type)
113
    {
114
        $from = str_replace(base_path(), '', realpath($from));
115
        $to = str_replace(base_path(), '', realpath($to));
116
        $this->line('<info>Copied '.$type.'</info> <comment>['.$from.']</comment> <info>To</info> <comment>['.$to.']</comment>');
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?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
117
    }
118
119
}