Installable   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 106
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 18
c 0
b 0
f 0
lcom 1
cbo 3
dl 0
loc 106
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
B install() 0 15 7
A confirmOverwrite() 0 8 2
A createParentDirectory() 0 6 2
A publishFile() 0 6 1
A publishDirectory() 0 13 5
A status() 0 7 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
trait Installable
13
{
14
    /**
15
     * Install files from array.
16
     *
17
     * @param $files
18
     */
19
    private function install($files)
20
    {
21
        foreach ($files as $fileSrc => $fileDst) {
22
            if (file_exists($fileDst) && !$this->force && !$this->confirmOverwrite(basename($fileDst))) {
0 ignored issues
show
Bug introduced by Sergi Tur Badenas
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...
23
                return;
24
            }
25
            if ($this->files->isFile($fileSrc)) {
26
                $this->publishFile($fileSrc, $fileDst);
27
            } elseif ($this->files->isDirectory($fileSrc)) {
0 ignored issues
show
Bug introduced by Sergi Tur Badenas
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...
28
                $this->publishDirectory($fileSrc, $fileDst);
29
            } else {
30
                $this->error("Can't locate path: <{$fileSrc}>");
0 ignored issues
show
Bug introduced by Sergi Tur Badenas
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...
31
            }
32
        }
33
    }
34
35
    /**
36
     * @param $fileName
37
     * @param string $prompt
38
     *
39
     * @return bool
40
     */
41
    protected function confirmOverwrite($fileName, $prompt = '')
42
    {
43
        $prompt = (empty($prompt))
44
            ? $fileName.' already exists. Do you want to overwrite it? [y|N]'
45
            : $prompt;
46
47
        return $this->confirm($prompt, false);
0 ignored issues
show
Bug introduced by Sergi Tur Badenas
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...
48
    }
49
50
    /**
51
     * Create the directory to house the published files if needed.
52
     *
53
     * @param string $directory
54
     *
55
     * @return void
56
     */
57
    protected function createParentDirectory($directory)
58
    {
59
        if (!$this->files->isDirectory($directory)) {
60
            $this->files->makeDirectory($directory, 0755, true);
61
        }
62
    }
63
64
    /**
65
     * Publish the file to the given path.
66
     *
67
     * @param string $from
68
     * @param string $to
69
     *
70
     * @return void
71
     */
72
    protected function publishFile($from, $to)
73
    {
74
        $this->createParentDirectory(dirname($to));
75
        $this->files->copy($from, $to);
76
        $this->status($from, $to, 'File');
77
    }
78
79
    /**
80
     * Publish the directory to the given directory.
81
     *
82
     * @param string $from
83
     * @param string $to
84
     *
85
     * @return void
86
     */
87
    protected function publishDirectory($from, $to)
88
    {
89
        $manager = new MountManager([
90
            'from' => new Flysystem(new LocalAdapter($from)),
91
            'to'   => new Flysystem(new LocalAdapter($to)),
92
        ]);
93
        foreach ($manager->listContents('from://', true) as $file) {
94
            if ($file['type'] === 'file' && (!$manager->has('to://'.$file['path']) || $this->force)) {
95
                $manager->put('to://'.$file['path'], $manager->read('from://'.$file['path']));
96
            }
97
        }
98
        $this->status($from, $to, 'Directory');
99
    }
100
101
    /**
102
     * Write a status message to the console.
103
     *
104
     * @param string $from
105
     * @param string $to
106
     * @param string $type
107
     *
108
     * @return void
109
     */
110
    protected function status($from, $to, $type)
111
    {
112
        $from = str_replace(base_path(), '', realpath($from));
113
        $to = str_replace(base_path(), '', realpath($to));
114
        $this->line('<info>Copied '. $type. '</info> <comment>['. $from.
0 ignored issues
show
Bug introduced by Sergi Tur Badenas
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...
115
            ']</comment> <info>To</info> <comment>['.$to.']</comment>');
116
    }
117
}
118