PackageServiceProvider::checkPackageName()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3

Importance

Changes 0
Metric Value
nc 2
dl 0
loc 6
ccs 4
cts 4
cp 1
c 0
b 0
f 0
cc 3
nop 0
crap 3
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Arcanedev\Support\Providers;
6
7
use Arcanedev\Support\Exceptions\PackageException;
8
use Arcanedev\Support\Providers\Concerns\{
9
    HasAssets, HasConfig, HasFactories, HasMigrations, HasTranslations, HasViews
10
};
11
use Illuminate\Contracts\Foundation\Application;
12
use Illuminate\Support\Str;
13
use ReflectionClass;
14
15
/**
16
 * Class     PackageServiceProvider
17
 *
18
 * @author   ARCANEDEV <[email protected]>
19
 */
20
abstract class PackageServiceProvider extends ServiceProvider
21
{
22
    /* -----------------------------------------------------------------
23
     |  Traits
24
     | -----------------------------------------------------------------
25
     */
26
27
    use HasAssets,
28
        HasConfig,
29
        HasFactories,
30
        HasMigrations,
31
        HasTranslations,
32
        HasViews;
33
34
    /* -----------------------------------------------------------------
35
     |  Properties
36
     | -----------------------------------------------------------------
37
     */
38
39
    /**
40
     * Vendor name.
41
     *
42
     * @var string
43
     */
44
    protected $vendor = 'arcanedev';
45
46
    /**
47
     * Package name.
48
     *
49
     * @var string|null
50
     */
51
    protected $package;
52
53
    /**
54
     * Package base path.
55
     *
56
     * @var string
57
     */
58
    protected $basePath;
59
60
    /* -----------------------------------------------------------------
61
     |  Constructor
62
     | -----------------------------------------------------------------
63
     */
64
65
    /**
66
     * Create a new service provider instance.
67
     *
68
     * @param  \Illuminate\Contracts\Foundation\Application  $app
69
     */
70 18
    public function __construct(Application $app)
71
    {
72 18
        parent::__construct($app);
73
74 18
        $this->basePath = $this->resolveBasePath();
75 18
    }
76
77
    /**
78
     * Resolve the base path of the package.
79
     *
80
     * @return string
81
     */
82 18
    protected function resolveBasePath()
83
    {
84 18
        return dirname(
85 18
            (new ReflectionClass($this))->getFileName(), 2
86
        );
87
    }
88
89
    /* -----------------------------------------------------------------
90
     |  Getters & Setters
91
     | -----------------------------------------------------------------
92
     */
93
94
    /**
95
     * Get the base path of the package.
96
     *
97
     * @return string
98
     */
99 18
    public function getBasePath()
100
    {
101 18
        return $this->basePath;
102
    }
103
104
    /**
105
     * Get the vendor name.
106
     *
107
     * @return string
108
     */
109 18
    protected function getVendorName(): string
110
    {
111 18
        return $this->vendor;
112
    }
113
114
    /**
115
     * Get the package name.
116
     *
117
     * @return string|null
118
     */
119 18
    protected function getPackageName(): ?string
120
    {
121 18
        return $this->package;
122
    }
123
124
    /* -----------------------------------------------------------------
125
     |  Main Methods
126
     | -----------------------------------------------------------------
127
     */
128
129
    /**
130
     * Register the service provider.
131
     */
132 18
    public function register()
133
    {
134 18
        parent::register();
135
136 18
        $this->checkPackageName();
137 18
    }
138
139
    /* -----------------------------------------------------------------
140
     |  Package Methods
141
     | -----------------------------------------------------------------
142
     */
143
144
    /**
145
     * Publish all the package files.
146
     */
147
    protected function publishAll(): void
148
    {
149
        $this->publishAssets();
150
        $this->publishConfig();
151
        $this->publishFactories();
152
        $this->publishMigrations();
153
        $this->publishTranslations();
154
        $this->publishViews();
155
    }
156
157
    /* -----------------------------------------------------------------
158
     |  Check Methods
159
     | -----------------------------------------------------------------
160
     */
161
162
    /**
163
     * Check package name.
164
     *
165
     * @throws \Arcanedev\Support\Exceptions\PackageException
166
     */
167 18
    protected function checkPackageName(): void
168
    {
169 18
        if (empty($this->getVendorName()) || empty($this->getPackageName())) {
170 6
            throw PackageException::unspecifiedName();
171
        }
172 18
    }
173
174
    /* -----------------------------------------------------------------
175
     |  Other Methods
176
     | -----------------------------------------------------------------
177
     */
178
179
    /**
180
     * Get the published tags.
181
     *
182
     * @param  string  $tag
183
     *
184
     * @return array
185
     */
186
    protected function getPublishedTags(string $tag): array
187
    {
188
        $package = $this->getPackageName();
189
190
        return array_map(function ($name) {
191
            return Str::slug($name);
192
        }, [$this->getVendorName(), $package, $tag, $package.'-'.$tag]);
193
    }
194
}
195