Completed
Push — master ( dcc042...9e46d5 )
by Nicolas
16:30
created

src/Publishing/Publisher.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Nwidart\Modules\Publishing;
4
5
use Illuminate\Console\Command;
6
use Nwidart\Modules\Contracts\PublisherInterface;
7
use Nwidart\Modules\Contracts\RepositoryInterface;
8
use Nwidart\Modules\Module;
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
     * @var RepositoryInterface
22
     */
23
    protected $repository;
24
25
    /**
26
     * The laravel console instance.
27
     *
28
     * @var \Illuminate\Console\Command
29
     */
30
    protected $console;
31
32
    /**
33
     * The success message will displayed at console.
34
     *
35
     * @var string
36
     */
37
    protected $success;
38
39
    /**
40
     * The error message will displayed at console.
41
     *
42
     * @var string
43
     */
44
    protected $error = '';
45
46
    /**
47
     * Determine whether the result message will shown in the console.
48
     *
49
     * @var bool
50
     */
51
    protected $showMessage = true;
52
53
    /**
54
     * The constructor.
55
     *
56
     * @param Module $module
57
     */
58 3
    public function __construct(Module $module)
59
    {
60 3
        $this->module = $module;
61 3
    }
62
63
    /**
64
     * Show the result message.
65
     *
66
     * @return self
67
     */
68
    public function showMessage()
69
    {
70
        $this->showMessage = true;
71
72
        return $this;
73
    }
74
75
    /**
76
     * Hide the result message.
77
     *
78
     * @return self
79
     */
80
    public function hideMessage()
81
    {
82
        $this->showMessage = false;
83
84
        return $this;
85
    }
86
87
    /**
88
     * Get module instance.
89
     *
90
     * @return \Nwidart\Modules\Module
91
     */
92 2
    public function getModule()
93
    {
94 2
        return $this->module;
95
    }
96
97
    /**
98
     * Set modules repository instance.
99
     * @param RepositoryInterface $repository
100
     * @return $this
101
     */
102 3
    public function setRepository(RepositoryInterface $repository)
103
    {
104 3
        $this->repository = $repository;
105
106 3
        return $this;
107
    }
108
109
    /**
110
     * Get modules repository instance.
111
     *
112
     * @return RepositoryInterface
113
     */
114
    public function getRepository()
115
    {
116
        return $this->repository;
117
    }
118
119
    /**
120
     * Set console instance.
121
     *
122
     * @param \Illuminate\Console\Command $console
123
     *
124
     * @return $this
125
     */
126 3
    public function setConsole(Command $console)
127
    {
128 3
        $this->console = $console;
129
130 3
        return $this;
131
    }
132
133
    /**
134
     * Get console instance.
135
     *
136
     * @return \Illuminate\Console\Command
137
     */
138
    public function getConsole()
139
    {
140
        return $this->console;
141
    }
142
143
    /**
144
     * Get laravel filesystem instance.
145
     *
146
     * @return \Illuminate\Filesystem\Filesystem
147
     */
148 3
    public function getFilesystem()
149
    {
150 3
        return $this->repository->getFiles();
151
    }
152
153
    /**
154
     * Get destination path.
155
     *
156
     * @return string
157
     */
158
    abstract public function getDestinationPath();
159
160
    /**
161
     * Get source path.
162
     *
163
     * @return string
164
     */
165
    abstract public function getSourcePath();
166
167
    /**
168
     * Publish something.
169
     */
170 3
    public function publish()
171
    {
172 3
        if (!$this->console instanceof Command) {
0 ignored issues
show
The class Illuminate\Console\Command does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
173
            $message = "The 'console' property must instance of \\Illuminate\\Console\\Command.";
174
175
            throw new \RuntimeException($message);
176
        }
177
178 3
        if (!$this->getFilesystem()->isDirectory($sourcePath = $this->getSourcePath())) {
179
            return;
180
        }
181
182 3
        if (!$this->getFilesystem()->isDirectory($destinationPath = $this->getDestinationPath())) {
183 2
            $this->getFilesystem()->makeDirectory($destinationPath, 0775, true);
184
        }
185
186 3
        if ($this->getFilesystem()->copyDirectory($sourcePath, $destinationPath)) {
187 3
            if ($this->showMessage === true) {
188 3
                $this->console->line("<info>Published</info>: {$this->module->getStudlyName()}");
189
            }
190
        } else {
191
            $this->console->error($this->error);
192
        }
193 3
    }
194
}
195