Skip to content

Commit 15693a0

Browse files
committed
Refactor service provider + tests
1 parent e33568b commit 15693a0

File tree

8 files changed

+111
-169
lines changed

8 files changed

+111
-169
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "rias/statamic-redirect",
33
"require": {
44
"ext-json": "*",
5-
"php": "^7.4|^8.0",
5+
"php": "^8.2",
66
"spatie/simple-excel": "^1.0|^2.0|^3.0",
77
"statamic/cms": "^4.10|^5.0",
88
"doctrine/dbal": "^4.2"

src/RedirectServiceProvider.php

Lines changed: 72 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Rias\StatamicRedirect;
44

55
use Illuminate\Routing\Router;
6+
use Illuminate\Support\Facades\Config;
67
use Illuminate\Support\Facades\DB;
78
use Illuminate\Support\Facades\File;
89
use Illuminate\Support\Facades\Schema;
@@ -80,13 +81,11 @@ public function register()
8081
$this->registerAddonConfig();
8182

8283
$this->app->singleton(RedirectRepository::class, function () {
83-
$class = $this->getRedirectRepository();
84-
85-
return new $class($this->app['stache']);
84+
return $this->app->get($this->getRedirectRepository());
8685
});
8786
}
8887

89-
public function boot()
88+
public function boot(): void
9089
{
9190
parent::boot();
9291

@@ -108,11 +107,14 @@ public function boot()
108107
Git::listen(RedirectSaved::class);
109108
}
110109

110+
File::ensureDirectoryExists(storage_path('redirect'));
111+
112+
$this->loadViewsFrom(__DIR__ . '/../resources/views', 'redirect');
113+
111114
$this
112-
->bootAddonViews()
113115
->bootAddonNav()
114-
->bootStores()
115-
->bootDatabase()
116+
->bootErrors()
117+
->bootRedirects()
116118
->bootPermissions();
117119
});
118120

@@ -145,14 +147,7 @@ protected function getRedirectRepository()
145147
return StacheRedirectRepository::class;
146148
}
147149

148-
protected function bootAddonViews()
149-
{
150-
$this->loadViewsFrom(__DIR__ . '/../resources/views', 'redirect');
151-
152-
return $this;
153-
}
154-
155-
protected function bootAddonNav()
150+
protected function bootAddonNav(): self
156151
{
157152
Nav::extend(function ($nav) {
158153
$items = [];
@@ -178,97 +173,58 @@ protected function bootAddonNav()
178173
return $this;
179174
}
180175

181-
protected function bootStores()
182-
{
183-
$redirectStore = new RedirectStore();
184-
$redirectStore->directory(config('statamic.redirect.redirect_store', base_path('content/redirects')));
185-
app(Stache::class)->registerStore($redirectStore);
186-
187-
return $this;
188-
}
189-
190-
protected function bootDatabase()
176+
protected function bootErrors(): self
191177
{
192178
if (! config('statamic.redirect.log_errors')) {
193179
return $this;
194180
}
195181

196-
File::ensureDirectoryExists(storage_path('redirect'));
197-
198-
$sqlitePath = storage_path('redirect/redirect.sqlite');
199-
$this->ensureDatabaseExists($sqlitePath);
200-
201-
app('config')->set('database.connections.redirect-sqlite', [
202-
'driver' => 'sqlite',
203-
'database' => $sqlitePath,
204-
]);
205-
206-
$this->bootDatabaseForErrors();
207-
$this->bootDatabaseForRedirects();
208-
209-
return $this;
210-
}
211-
212-
protected function ensureDatabaseExists($sqlitePath)
213-
{
214-
$oldSqlitePath = storage_path('redirect/errors.sqlite');
215-
216-
if (! file_exists($sqlitePath) && file_exists($oldSqlitePath)) {
217-
File::move($oldSqlitePath, $sqlitePath);
218-
219-
return;
220-
}
221-
222-
if (! file_exists($sqlitePath)) {
223-
File::put($sqlitePath, '');
224-
225-
$gitIgnorePath = storage_path('redirect/.gitignore');
226-
if (! file_exists($gitIgnorePath)) {
227-
File::put($gitIgnorePath, "*\n!.gitignore");
228-
}
182+
if (config('statamic.redirect.error_connection', 'redirect-sqlite') !== 'redirect-sqlite' && ! $this->generalConnectionIsBuiltinSqlite()) {
183+
return $this;
229184
}
230-
}
231185

232-
protected function bootDatabaseForErrors()
233-
{
234-
if (
235-
config('statamic.redirect.error_connection', 'redirect-sqlite') !== 'redirect-sqlite' &&
236-
! $this->generalConnectionIsBuiltinSqlite()
237-
) {
238-
return;
239-
}
186+
$this->createSqliteConnection();
240187

241188
$defaultConnection = DB::getDefaultConnection();
242189
DB::setDefaultConnection('redirect-sqlite');
243190

244-
if (! Schema::connection('redirect-sqlite')->hasTable('errors')) {
191+
if (! Schema::hasTable('errors')) {
245192
require_once(__DIR__ . '/../database/migrations/create_redirect_error_tables.php.stub');
246193
(new \CreateRedirectErrorTables())->up();
247194
}
248195

249-
if (! Schema::connection('redirect-sqlite')->hasColumn('errors', 'url_md5')) {
196+
if (! Schema::hasColumn('errors', 'url_md5')) {
250197
require_once(__DIR__ . '/../database/migrations/increase_redirect_error_table_url_length.php.stub');
251198
(new \IncreaseRedirectErrorTableUrlLength())->up();
252199
}
253200

254201
DB::setDefaultConnection($defaultConnection);
202+
203+
return $this;
255204
}
256205

257-
protected function bootDatabaseForRedirects()
206+
protected function bootRedirects(): self
258207
{
259-
if (
260-
config('statamic.redirect.redirect_connection', 'stache') !== 'redirect-sqlite' &&
261-
! $this->generalConnectionIsBuiltinSqlite()
262-
) {
263-
return;
208+
$connection = config('statamic.redirect.redirect_connection', 'stache');
209+
210+
if ($connection === 'stache') {
211+
$redirectStore = new RedirectStore();
212+
$redirectStore->directory(config('statamic.redirect.redirect_store', base_path('content/redirects')));
213+
app(Stache::class)->registerStore($redirectStore);
214+
215+
return $this;
264216
}
265217

266-
if (Schema::connection('redirect-sqlite')->hasTable('redirects')) {
267-
return;
218+
if ($this->generalConnectionIsBuiltinSqlite()) {
219+
$this->createSqliteConnection();
220+
}
221+
222+
if (Schema::connection($connection)->hasTable('redirects')) {
223+
return $this;
268224
}
269225

270226
$defaultConnection = DB::getDefaultConnection();
271-
DB::setDefaultConnection('redirect-sqlite');
227+
DB::setDefaultConnection($connection);
272228

273229
require_once(__DIR__ . '/../database/migrations/create_redirect_redirects_table.php.stub');
274230
(new \CreateRedirectRedirectsTable())->up();
@@ -278,6 +234,43 @@ protected function bootDatabaseForRedirects()
278234
(new \IncreaseRedirectRedirectsTableUrlLength())->up();
279235

280236
DB::setDefaultConnection($defaultConnection);
237+
238+
return $this;
239+
}
240+
241+
protected function createSqliteConnection(): void
242+
{
243+
$sqlitePath = storage_path('redirect/redirect.sqlite');
244+
$this->ensureDatabaseExists($sqlitePath);
245+
246+
if (Config::has('database.connections.redirect-sqlite')) {
247+
return;
248+
}
249+
250+
Config::set('database.connections.redirect-sqlite', [
251+
'driver' => 'sqlite',
252+
'database' => $sqlitePath,
253+
]);
254+
}
255+
256+
protected function ensureDatabaseExists($sqlitePath): void
257+
{
258+
$oldSqlitePath = storage_path('redirect/errors.sqlite');
259+
260+
if (! file_exists($sqlitePath) && file_exists($oldSqlitePath)) {
261+
File::move($oldSqlitePath, $sqlitePath);
262+
263+
return;
264+
}
265+
266+
if (! file_exists($sqlitePath)) {
267+
File::put($sqlitePath, '');
268+
269+
$gitIgnorePath = storage_path('redirect/.gitignore');
270+
if (! file_exists($gitIgnorePath)) {
271+
File::put($gitIgnorePath, "*\n!.gitignore");
272+
}
273+
}
281274
}
282275

283276
protected function generalConnectionIsBuiltinSqlite()
@@ -293,7 +286,7 @@ protected function generalConnectionIsBuiltinSqlite()
293286
return false;
294287
}
295288

296-
protected function registerAddonConfig()
289+
protected function registerAddonConfig(): self
297290
{
298291
$this->mergeConfigFrom(__DIR__ . '/../config/redirect.php', 'statamic.redirect');
299292

src/Stache/Redirects/RedirectRepository.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
use Rias\StatamicRedirect\Contracts\RedirectRepository as RepositoryContract;
66
use Rias\StatamicRedirect\Data\Redirect;
7-
use Rias\StatamicRedirect\Eloquent\Redirects\RedirectQueryBuilder;
87
use Rias\StatamicRedirect\Enums\MatchTypeEnum;
98
use Rias\StatamicRedirect\Events\RedirectSaved;
109
use Rias\StatamicRedirect\GenerateUrlVariants;

tests/Feature/Commands/CleanErrorsCommandTest.php

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,14 @@
33
namespace Rias\StatamicRedirect\Tests\Feature\Commands;
44

55
use Illuminate\Support\Facades\Artisan;
6+
use PHPUnit\Framework\Attributes\Test;
67
use Rias\StatamicRedirect\Commands\CleanErrorsCommand;
78
use Rias\StatamicRedirect\Data\Error;
89
use Rias\StatamicRedirect\Tests\TestCase;
910

1011
class CleanErrorsCommandTest extends TestCase
1112
{
12-
/**
13-
* @test
14-
*/
13+
#[Test]
1514
public function it_cleans_errors_older_than_1_month_by_default()
1615
{
1716
Error::create(['url' => 'bla1'])->addHit(now()->subMonth()->subDay()->timestamp);
@@ -25,9 +24,7 @@ public function it_cleans_errors_older_than_1_month_by_default()
2524
$this->assertEquals(2, Error::query()->count());
2625
}
2726

28-
/**
29-
* @test
30-
*/
27+
#[Test]
3128
public function it_looks_at_the_config_file_for_older_than_date()
3229
{
3330
config()->set('statamic.redirect.clean_older_than', '1 week');
@@ -43,9 +40,7 @@ public function it_looks_at_the_config_file_for_older_than_date()
4340
$this->assertEquals(1, Error::query()->count());
4441
}
4542

46-
/**
47-
* @test
48-
*/
43+
#[Test]
4944
public function it_doesnt_clean_errors_if_config_is_false()
5045
{
5146
config()->set('statamic.redirect.clean_errors', false);
@@ -61,9 +56,7 @@ public function it_doesnt_clean_errors_if_config_is_false()
6156
$this->assertEquals(3, Error::query()->count());
6257
}
6358

64-
/**
65-
* @test
66-
*/
59+
#[Test]
6760
public function it_deletes_errors_when_there_are_too_many()
6861
{
6962
config()->set('statamic.redirect.clean_errors', true);

tests/Feature/Controllers/ImportRedirectsControllerTest.php

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,15 @@
33
namespace Rias\StatamicRedirect\Tests\Feature\Controllers;
44

55
use Illuminate\Http\UploadedFile;
6+
use PHPUnit\Framework\Attributes\Test;
67
use Rias\StatamicRedirect\Contracts\Redirect as RedirectContract;
78
use Rias\StatamicRedirect\Controllers\ImportRedirectsController;
89
use Rias\StatamicRedirect\Facades\Redirect;
910
use Rias\StatamicRedirect\Tests\TestCase;
1011

1112
class ImportRedirectsControllerTest extends TestCase
1213
{
13-
/**
14-
* @test
15-
*/
14+
#[Test]
1615
public function it_can_import_redirects()
1716
{
1817
$this->asAdmin();
@@ -34,9 +33,7 @@ public function it_can_import_redirects()
3433
});
3534
}
3635

37-
/**
38-
* @test
39-
*/
36+
#[Test]
4037
public function it_can_set_the_site()
4138
{
4239
$this->asAdmin();
@@ -58,9 +55,7 @@ public function it_can_set_the_site()
5855
});
5956
}
6057

61-
/**
62-
* @test
63-
*/
58+
#[Test]
6459
public function it_can_import_redirects_with_a_txt_file()
6560
{
6661
$this->asAdmin();
@@ -82,9 +77,7 @@ public function it_can_import_redirects_with_a_txt_file()
8277
});
8378
}
8479

85-
/**
86-
* @test
87-
*/
80+
#[Test]
8881
public function it_will_ignore_invalid_redirects()
8982
{
9083
$this->asAdmin();
@@ -106,9 +99,7 @@ public function it_will_ignore_invalid_redirects()
10699
});
107100
}
108101

109-
/**
110-
* @test
111-
*/
102+
#[Test]
112103
public function it_will_update_redirects_with_duplicate_source()
113104
{
114105
$this->asAdmin();

0 commit comments

Comments
 (0)