Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
duncanmcclean committed May 6, 2024
1 parent 43545a6 commit dcb4467
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/Events/ScriptsSaved.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace DuncanMcClean\CookieNotice\Events;

use Statamic\Contracts\Git\ProvidesCommitMessage;
use Statamic\Events\Event;

class ScriptsSaved extends Event implements ProvidesCommitMessage
{
public function commitMessage()
{
return __('Cookie Notice scripts updated', [], config('statamic.git.locale'));
}
}
3 changes: 3 additions & 0 deletions src/Scripts/Scripts.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace DuncanMcClean\CookieNotice\Scripts;

use DuncanMcClean\CookieNotice\Events\ScriptsSaved;
use Illuminate\Support\Facades\File;
use Statamic\Facades\YAML;

Expand Down Expand Up @@ -35,6 +36,8 @@ public static function save(array $data): void

File::ensureDirectoryExists(dirname(static::path()));
File::put(static::path(), $yaml);

ScriptsSaved::dispatch();
}

private static function path(): string
Expand Down
7 changes: 7 additions & 0 deletions src/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@

namespace DuncanMcClean\CookieNotice;

use DuncanMcClean\CookieNotice\Events\ScriptsSaved;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\File;
use Statamic\Facades\CP\Nav;
use Statamic\Facades\Permission;
use Statamic\Providers\AddonServiceProvider;
use Statamic\Statamic;

class ServiceProvider extends AddonServiceProvider
{
Expand Down Expand Up @@ -38,5 +41,9 @@ public function bootAddon()
'Scripts' => cp_route('cookie-notice.scripts.edit'),
]);
});

if (Statamic::pro() && config('statamic.git.enabled')) {
Event::listen(ScriptsSaved::class, fn ($event) => (new Statamic\Git\Subscriber)->commit($event));
}
}
}
6 changes: 6 additions & 0 deletions tests/Scripts/ScriptsTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php

use DuncanMcClean\CookieNotice\Events\ScriptsSaved;
use DuncanMcClean\CookieNotice\Scripts\Scripts;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\File;
use Statamic\Facades\YAML;

Expand Down Expand Up @@ -97,6 +99,8 @@
});

it('saves the scripts data', function () {
Event::fake();

YAML::shouldReceive('dump')
->with(['necessary' => [
[
Expand All @@ -120,4 +124,6 @@
'gtm_container_id' => 'GTM-123456CN',
],
]]);

Event::assertDispatched(ScriptsSaved::class);
});

0 comments on commit dcb4467

Please sign in to comment.