Completed
Push — master ( 621062...103aa8 )
by Nicolas
62:19 queued 20:21
created

Publisher   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 188
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 58.33%

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 3
dl 0
loc 188
ccs 21
cts 36
cp 0.5833
rs 10
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A showMessage() 0 6 1
A hideMessage() 0 6 1
A getModule() 0 4 1
A setRepository() 0 6 1
A getRepository() 0 4 1
A setConsole() 0 6 1
A getConsole() 0 4 1
A getFilesystem() 0 4 1
getDestinationPath() 0 1 ?
getSourcePath() 0 1 ?
B publish() 0 24 6
1
<?php
2
3
namespace Nwidart\Modules\Publishing;
4
5
use Illuminate\Console\Command;
6
use Nwidart\Modules\Contracts\PublisherInterface;
7
use Nwidart\Modules\Module;
8
use Nwidart\Modules\Repository;
9
10
abstract class Publisher implements PublisherInterface
11
{
12
    /**
13
     * The name of module will used.
14
     *
15
     * @var string
16
     */
17
    protected $module;
18
19
    /**
20
     * The modules repository instance.
21
     *
22
     * @var \Nwidart\Modules\Repository
23
     */
24
    protected $repository;
25
26
    /**
27
     * The laravel console instance.
28
     *
29
     * @var \Illuminate\Console\Command
30
     */
31
    protected $console;
32
33
    /**
34
     * The success message will displayed at console.
35
     *
36
     * @var string
37
     */
38
    protected $success;
39
40
    /**
41
     * The error message will displayed at console.
42
     *
43
     * @var string
44
     */
45
    protected $error = '';
46
47
    /**
48
     * Determine whether the result message will shown in the console.
49
     *
50
     * @var bool
51
     */
52
    protected $showMessage = true;
53
54
    /**
55
     * The constructor.
56
     *
57
     * @param Module $module
58
     */
59 3
    public function __construct(Module $module)
60
    {
61 3
        $this->module = $module;
0 ignored issues
show
Documentation Bug introduced by
It seems like $module of type object<Nwidart\Modules\Module> is incompatible with the declared type string of property $module.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
62 3
    }
63
64
    /**
65
     * Show the result message.
66
     *
67
     * @return self
68
     */
69
    public function showMessage()
70
    {
71
        $this->showMessage = true;
72
73
        return $this;
74
    }
75
76
    /**
77
     * Hide the result message.
78
     *
79
     * @return self
80
     */
81
    public function hideMessage()
82
    {
83
        $this->showMessage = false;
84
85
        return $this;
86
    }
87
88
    /**
89
     * Get module instance.
90
     *
91
     * @return \Nwidart\Modules\Module
92
     */
93 2
    public function getModule()
94
    {
95 2
        return $this->module;
96
    }
97
98
    /**
99
     * Set modules repository instance.
100
     *
101
     * @param \Nwidart\Modules\Repository $repository
102
     *
103
     * @return $this
104
     */
105 3
    public function setRepository(Repository $repository)
106
    {
107 3
        $this->repository = $repository;
108
109 3
        return $this;
110
    }
111
112
    /**
113
     * Get modules repository instance.
114
     *
115
     * @return \Nwidart\Modules\Repository
116
     */
117
    public function getRepository()
118
    {
119
        return $this->repository;
120
    }
121
122
    /**
123
     * Set console instance.
124
     *
125
     * @param \Illuminate\Console\Command $console
126
     *
127
     * @return $this
128
     */
129 3
    public function setConsole(Command $console)
130
    {
131 3
        $this->console = $console;
132
133 3
        return $this;
134
    }
135
136
    /**
137
     * Get console instance.
138
     *
139
     * @return \Illuminate\Console\Command
140
     */
141
    public function getConsole()
142
    {
143
        return $this->console;
144
    }
145
146
    /**
147
     * Get laravel filesystem instance.
148
     *
149
     * @return \Illuminate\Filesystem\Filesystem
150
     */
151 3
    public function getFilesystem()
152
    {
153 3
        return $this->repository->getFiles();
154
    }
155
156
    /**
157
     * Get destination path.
158
     *
159
     * @return string
160
     */
161
    abstract public function getDestinationPath();
162
163
    /**
164
     * Get source path.
165
     *
166
     * @return string
167
     */
168
    abstract public function getSourcePath();
169
170
    /**
171
     * Publish something.
172
     */
173 3
    public function publish()
174
    {
175 3
        if (!$this->console instanceof Command) {
176
            $message = "The 'console' property must instance of \\Illuminate\\Console\\Command.";
177
178
            throw new \RuntimeException($message);
179
        }
180
181 3
        if (!$this->getFilesystem()->isDirectory($sourcePath = $this->getSourcePath())) {
182
            return;
183
        }
184
185 3
        if (!$this->getFilesystem()->isDirectory($destinationPath = $this->getDestinationPath())) {
186 2
            $this->getFilesystem()->makeDirectory($destinationPath, 0775, true);
187
        }
188
189 3
        if ($this->getFilesystem()->copyDirectory($sourcePath, $destinationPath)) {
190 3
            if ($this->showMessage === true) {
191
                $this->console->line("<info>Published</info>: {$this->module->getStudlyName()}");
0 ignored issues
show
Bug introduced by
The method getStudlyName cannot be called on $this->module (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
192
            }
193
        } else {
194
            $this->console->error($this->error);
195
        }
196 3
    }
197
}
198