Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -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=
29 changes: 29 additions & 0 deletions app/Events/PostContentEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace App\Events;

/**
* Class PostContentEvent
*/
class PostContentEvent extends Event
{
/**
* @var string|null
*/
private $url;

/**
* PostContentEvent constructor.
*
* @param string|null $url
*/
public function __construct(?string $url = null)
{
$this->url = $url;
}

public function getUrl(): ?string
{
return $this->url;
}
}
19 changes: 0 additions & 19 deletions app/Events/RebuildSiteEvent.php

This file was deleted.

4 changes: 2 additions & 2 deletions app/Http/Controllers/MediaController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace App\Http\Controllers;

use App\Events\RebuildSiteEvent;
use App\Events\PostContentEvent;
use App\Service\MediaService;
use Illuminate\Contracts\Events\Dispatcher as DispatcherContract;
use Illuminate\Http\JsonResponse;
Expand Down Expand Up @@ -85,7 +85,7 @@ public function upload(Request $request): JsonResponse

// Even though we're only uploading a file we need to rebuild the site because it copies the actual file across.
$this->eventDispatcher->dispatch(
new RebuildSiteEvent()
new PostContentEvent()
);

return new JsonResponse(
Expand Down
19 changes: 11 additions & 8 deletions app/Http/Controllers/PostMethodController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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', '')) {
Expand All @@ -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);

Expand All @@ -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;
}
}
76 changes: 76 additions & 0 deletions app/Listeners/PostCreatedPingListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

namespace App\Listeners;

use App\Events\PostContentEvent;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
use Illuminate\Log\Logger;

/**
* Class PostCreatedPingListener
*/
class PostCreatedPingListener
{
/**
* @var Client
*/
private $client;

/**
* @var Logger
*/
private $logger;

/**
* PostCreatedPingListener constructor.
*/
public function __construct()
{
$this->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,
]
);
}
}
}
6 changes: 3 additions & 3 deletions app/Listeners/RebuildSiteListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace App\Listeners;

use App\Events\RebuildSiteEvent;
use App\Events\PostContentEvent;
use Illuminate\Contracts\Console\Kernel;

/**
Expand All @@ -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');

Expand Down
2 changes: 1 addition & 1 deletion app/Service/MediaService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions app/ServiceProviders/EventServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -17,7 +18,8 @@ class EventServiceProvider extends ServiceProvider
* @var array
*/
protected $listen = [
RebuildSiteEvent::class => [
PostContentEvent::class => [
PostCreatedPingListener::class,
RebuildSiteListener::class,
],
];
Expand Down
2 changes: 1 addition & 1 deletion bootstrap/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ function($app) {
],
],
'image' => [
'driver' => 'Imagick',
'driver' => env('IMAGE_LIBRARY', 'gd'),
]
]);

Expand Down
1 change: 1 addition & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,6 @@
<env name="BASE_CONTENT_ENTRY_PATH" value="tests/blog/"/>
<env name="BASE_UPLOAD_PATH" value="tests/blog/images/" />
<env name="ME_URL" value="http://micropub.test"/>
<env name="POST_CREATED_PING_URL" value=""/>
</php>
</phpunit>
33 changes: 17 additions & 16 deletions tests/PostMediaTest.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
<?php

use App\Events\RebuildSiteEvent;
use App\Events\PostContentEvent;
use App\Providers\AbstractProvider;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Storage;
use Symfony\Component\HttpFoundation\Response;

class PostMediaTest extends TestCase
{
Expand Down Expand Up @@ -35,10 +37,10 @@ public function testNewPostUsingFormInput()

$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));
}

Expand All @@ -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));
}

Expand All @@ -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));
}

Expand All @@ -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}'",
Expand All @@ -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'))));
}
Expand All @@ -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(
Expand Down