From d2fadfea25fd77b24f863d17565c1423b11de8f5 Mon Sep 17 00:00:00 2001 From: Dan Barrett Date: Sun, 8 Dec 2019 22:24:59 +1100 Subject: [PATCH] Started adding in new ping notify feature --- .env.example | 2 + app/Events/PostContentEvent.php | 29 +++++++ app/Events/RebuildSiteEvent.php | 19 ----- app/Http/Controllers/MediaController.php | 4 +- app/Http/Controllers/PostMethodController.php | 19 +++-- app/Listeners/PostCreatedPingListener.php | 76 +++++++++++++++++++ app/Listeners/RebuildSiteListener.php | 6 +- app/Service/MediaService.php | 2 +- app/ServiceProviders/EventServiceProvider.php | 6 +- bootstrap/app.php | 2 +- phpunit.xml | 1 + tests/PostMediaTest.php | 33 ++++---- 12 files changed, 147 insertions(+), 52 deletions(-) create mode 100644 app/Events/PostContentEvent.php delete mode 100644 app/Events/RebuildSiteEvent.php create mode 100644 app/Listeners/PostCreatedPingListener.php diff --git a/.env.example b/.env.example index ce6bdf6..5c4e5f9 100644 --- a/.env.example +++ b/.env.example @@ -16,3 +16,5 @@ BASE_UPLOAD_URL= BASE_CONTENT_ENTRY_PATH= # Command to build your static site, e.g. "sh /path/to/build.sh" SITE_BUILD_COMMAND= +# URL to notify about a new post. +POST_CREATED_PING_URL= diff --git a/app/Events/PostContentEvent.php b/app/Events/PostContentEvent.php new file mode 100644 index 0000000..d135111 --- /dev/null +++ b/app/Events/PostContentEvent.php @@ -0,0 +1,29 @@ +url = $url; + } + + public function getUrl(): ?string + { + return $this->url; + } +} diff --git a/app/Events/RebuildSiteEvent.php b/app/Events/RebuildSiteEvent.php deleted file mode 100644 index a7700be..0000000 --- a/app/Events/RebuildSiteEvent.php +++ /dev/null @@ -1,19 +0,0 @@ -eventDispatcher->dispatch( - new RebuildSiteEvent() + new PostContentEvent() ); return new JsonResponse( diff --git a/app/Http/Controllers/PostMethodController.php b/app/Http/Controllers/PostMethodController.php index ba8b866..9bdd78f 100644 --- a/app/Http/Controllers/PostMethodController.php +++ b/app/Http/Controllers/PostMethodController.php @@ -2,13 +2,14 @@ namespace App\Http\Controllers; -use App\Events\RebuildSiteEvent; +use App\Events\PostContentEvent; use App\Providers\AbstractProvider; use App\Service\ItemWriterService; use App\ValueObjects\ItemRequestValueObjectInterface; use Illuminate\Contracts\Events\Dispatcher as DispatcherContract; use Illuminate\Http\Request; use Illuminate\Http\Response; +use Illuminate\Support\Str; /** * Class PostMethodController @@ -111,7 +112,7 @@ private function handleCreateItem(ItemRequestValueObjectInterface $newItem): str $firstThreeWords = implode(' ', $firstThreeWordsArray); - $slug = str_slug($firstThreeWords, '-'); + $slug = Str::slug($firstThreeWords, '-'); } if (!array_key_exists('mp-slug', $commands) && '' !== $frontMatterProperties->get('name', '')) { @@ -122,7 +123,7 @@ private function handleCreateItem(ItemRequestValueObjectInterface $newItem): str $slug = $commands['mp-slug']; } - $frontMatter['slug'] = str_slug($slug, '-'); + $frontMatter['slug'] = Str::slug($slug, '-'); $frontMatter['date'] = $now->format(\DateTime::W3C); @@ -145,16 +146,18 @@ private function handleCreateItem(ItemRequestValueObjectInterface $newItem): str $pathToWriteTo.'/'.$filename ); - $this->eventDispatcher->dispatch( - new RebuildSiteEvent() - ); - - return sprintf( + $url = sprintf( '%s%d/%02d/%s/', env('ME_URL'), $now->format('Y'), $now->format('m'), $frontMatter['slug'] ); + + $this->eventDispatcher->dispatch( + new PostContentEvent($url) + ); + + return $url; } } diff --git a/app/Listeners/PostCreatedPingListener.php b/app/Listeners/PostCreatedPingListener.php new file mode 100644 index 0000000..1ccb193 --- /dev/null +++ b/app/Listeners/PostCreatedPingListener.php @@ -0,0 +1,76 @@ +client = new Client(); + $this->logger = app('logger'); + } + + /** + * @param PostContentEvent $event + * + * @return void + */ + public function handle(PostContentEvent $event): void + { + $newPostUrl = $event->getUrl(); + + $notifyUrl = env('POST_CREATED_PING_URL', null); + + // If no notification URL has been set then obviously we can't notify anyone. + if (null === $newPostUrl || null === $notifyUrl) { + return; + } + + try { + $this->client->post( + $notifyUrl, + [ + 'url' => $newPostUrl, + ] + ); + + $this->logger->info( + "Notified URL '{notifyUrl}' about new post", + [ + 'notifyUrl' => $notifyUrl, + 'newPostUrl' => $newPostUrl, + ] + ); + } catch (RequestException $e) { + $this->logger->error( + "Unable to notify URL '{notifyUrl}' due to a request exception", + [ + 'notifyUrl' => $notifyUrl, + 'newPostUrl' => $newPostUrl, + 'e' => $e, + ] + ); + } + } +} diff --git a/app/Listeners/RebuildSiteListener.php b/app/Listeners/RebuildSiteListener.php index d37ec01..3457f89 100644 --- a/app/Listeners/RebuildSiteListener.php +++ b/app/Listeners/RebuildSiteListener.php @@ -2,7 +2,7 @@ namespace App\Listeners; -use App\Events\RebuildSiteEvent; +use App\Events\PostContentEvent; use Illuminate\Contracts\Console\Kernel; /** @@ -28,11 +28,11 @@ public function __construct(Kernel $console) /** * Handle the event. * - * @param RebuildSiteEvent $event + * @param PostContentEvent $event * * @return void */ - public function handle(RebuildSiteEvent $event): void + public function handle(PostContentEvent $event): void { $this->console->call('site:build'); diff --git a/app/Service/MediaService.php b/app/Service/MediaService.php index 873e454..e727655 100644 --- a/app/Service/MediaService.php +++ b/app/Service/MediaService.php @@ -173,7 +173,7 @@ public function uploadPhotos(array $photos): array */ public function uploadPhoto(UploadedFile $file): string { - $filenameAndExtension = str_random(40).'.'.$file->guessExtension(); + $filenameAndExtension = $file->hashName(); $this ->imageManager diff --git a/app/ServiceProviders/EventServiceProvider.php b/app/ServiceProviders/EventServiceProvider.php index 76d427a..db66a76 100644 --- a/app/ServiceProviders/EventServiceProvider.php +++ b/app/ServiceProviders/EventServiceProvider.php @@ -2,7 +2,8 @@ namespace App\ServiceProviders; -use App\Events\RebuildSiteEvent; +use App\Events\PostContentEvent; +use App\Listeners\PostCreatedPingListener; use App\Listeners\RebuildSiteListener; use Laravel\Lumen\Providers\EventServiceProvider as ServiceProvider; @@ -17,7 +18,8 @@ class EventServiceProvider extends ServiceProvider * @var array */ protected $listen = [ - RebuildSiteEvent::class => [ + PostContentEvent::class => [ + PostCreatedPingListener::class, RebuildSiteListener::class, ], ]; diff --git a/bootstrap/app.php b/bootstrap/app.php index 23bd29a..0c14e77 100644 --- a/bootstrap/app.php +++ b/bootstrap/app.php @@ -111,7 +111,7 @@ function($app) { ], ], 'image' => [ - 'driver' => 'Imagick', + 'driver' => env('IMAGE_LIBRARY', 'gd'), ] ]); diff --git a/phpunit.xml b/phpunit.xml index 2103da6..2b92ad6 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -27,5 +27,6 @@ + diff --git a/tests/PostMediaTest.php b/tests/PostMediaTest.php index dea59ce..78bdc96 100644 --- a/tests/PostMediaTest.php +++ b/tests/PostMediaTest.php @@ -1,9 +1,11 @@ assertFileExists($this->generateFilename('entry', $now, $slug)); - $this->assertResponseStatus(201); + $this->assertResponseStatus(Response::HTTP_CREATED); $this->seeHeader('Location', $this->generateUrl($now, $slug)); } @@ -58,10 +60,10 @@ public function testNewPostIgnoresUrlsInPostBodyForSlug() $now = new \DateTime(); - Event::assertDispatched(RebuildSiteEvent::class); + Event::assertDispatched(PostContentEvent::class); $this->assertFileExists($this->generateFilename('entry', $now, $expectedSlug)); - $this->assertResponseStatus(201); + $this->assertResponseStatus(Response::HTTP_CREATED); $this->seeHeader('Location', $this->generateUrl($now, $expectedSlug)); } @@ -82,10 +84,10 @@ public function testNewPostWithCustomSlugUsingFormInput() $now = new \DateTime(); - Event::assertDispatched(RebuildSiteEvent::class); + Event::assertDispatched(PostContentEvent::class); $this->assertFileExists($this->generateFilename('entry', $now, $slug)); - $this->assertResponseStatus(201); + $this->assertResponseStatus(Response::HTTP_CREATED); $this->seeHeader('Location', $this->generateUrl($now, $slug)); } @@ -107,10 +109,10 @@ public function testNewPostWithCustomTitleUsingFormInput() $now = new \DateTime(); - Event::assertDispatched(RebuildSiteEvent::class); + Event::assertDispatched(PostContentEvent::class); $this->assertFileExists($this->generateFilename('entry', $now, $slug)); - $this->assertResponseStatus(201); + $this->assertResponseStatus(Response::HTTP_CREATED); $this->seeHeader('Location', $this->generateUrl($now, $slug)); $this->assertContains( "title: '${title}'", @@ -134,7 +136,7 @@ public function testNewPostWithImageAndNoText() $now = new \DateTime(); - Event::assertDispatched(RebuildSiteEvent::class); + Event::assertDispatched(PostContentEvent::class); $this->assertFileExists($this->generateFilename('entry', $now, strtolower($now->format('l-jS')))); } @@ -145,12 +147,11 @@ public function testCanUploadImage() $file = UploadedFile::fake()->image('test.jpg'); - $this->post( - '/media', - [ - 'file' => $file, - ] - ); + $this->call('POST', '/media', [], [], ['file' => $file], []); + + $this->assertResponseStatus(Response::HTTP_CREATED); + Storage::disk('local')->assertExists('2019/'.$file->hashName().'.original'); + Storage::disk('local')->assertExists('2019/'.$file->hashName()); } private function generateUrl(