gstreamer0.10-ffmpeg
gstreamer0.10-plugins-good
packages.
Our app has one other small superpower. If for some reason you're not happy with your Ponka image... I'm not even sure how that would be possible... you can delete it. When you click that button, it sends an AJAX request that hits this delete()
action.
And... that really does two things. First, $photoManager->deleteImage()
takes care of physically deleting the image from the filesystem. I added a sleep()
for dramatic effect, but deleting something from the filesystem could be a bit heavy if the files were stored in the cloud, like on S3.
And second, the controller deletes the ImagePost
from the database. But... thinking about these two steps... the only thing we need to do immediately is delete the image from the database. If we only did that and the user refreshed the page, it would be gone. And then... if we deleted the actual file a few seconds... or minutes or even days later... that would be totally fine! But... more on doing fancy asynchronous stuff in a few minutes.
Right now, let's refactor all this deleting logic into the command bus pattern we just learned. First, we need the message, or "command" class. Let's copy AddPonkaToImage
, paste and call it DeleteImagePost.php
. Update the class name and then... um... do nothing! Coincidentally, this message class will look exactly the same: the handler will need to know which ImagePost
to delete.
... lines 1 - 2 | |
namespace App\Message; | |
use App\Entity\ImagePost; | |
class DeleteImagePost | |
{ | |
private $imagePost; | |
public function __construct(ImagePost $imagePost) | |
{ | |
$this->imagePost = $imagePost; | |
} | |
public function getImagePost(): ImagePost | |
{ | |
return $this->imagePost; | |
} | |
} |
Time for step 2 - the handler! Create a new PHP class and call it DeleteImagePostHandler
. Like before, give this a public function __invoke()
with a DeleteImagePost
type-hint as the only argument.
... lines 1 - 2 | |
namespace App\MessageHandler; | |
use App\Message\DeleteImagePost; | |
class DeleteImagePostHandler | |
{ | |
public function __invoke(DeleteImagePost $deleteImagePost) | |
{ | |
} | |
} |
Now, it's the same process as before: copy the first three lines of the controller, delete them, and paste them into the handler. This time, we need two services.
... lines 1 - 6 | |
class DeleteImagePostHandler | |
{ | |
public function __invoke(DeleteImagePost $deleteImagePost) | |
{ | |
$photoManager->deleteImage($imagePost->getFilename()); | |
$entityManager->remove($imagePost); | |
$entityManager->flush(); | |
} | |
} |
Add public function __construct()
with PhotoFileManager $photoManager
and EntityManagerInterface $entityManager
. I'll hit Alt + Enter and click initialize fields to create both of those properties and set them.
... lines 1 - 5 | |
use App\Photo\PhotoFileManager; | |
use Doctrine\ORM\EntityManagerInterface; | |
class DeleteImagePostHandler | |
{ | |
private $photoManager; | |
private $entityManager; | |
public function __construct(PhotoFileManager $photoManager, EntityManagerInterface $entityManager) | |
{ | |
$this->photoManager = $photoManager; | |
$this->entityManager = $entityManager; | |
} | |
... lines 19 - 27 | |
} |
Down here, use $this->photoManager
, $this->entityManager
and one more $this->entityManager
. And, like before, we need to know which ImagePost
we're deleting. Prep that with $imagePost = $deleteImagePost->getImagePost()
.
... lines 1 - 8 | |
class DeleteImagePostHandler | |
{ | |
... lines 11 - 19 | |
public function __invoke(DeleteImagePost $deleteImagePost) | |
{ | |
$imagePost = $deleteImagePost->getImagePost(); | |
$this->photoManager->deleteImage($imagePost->getFilename()); | |
$this->entityManager->remove($imagePost); | |
$this->entityManager->flush(); | |
} | |
} |
Ding! That's my... it's done sound! Because, we have a message, a handler and Symfony should know that they're linked together. The last step is to send the message. In the controller... we don't need these last two arguments anymore... we only need MessageBusInterface $messageBus
. And then, this is wonderful, our entire controller is: $messageBus->dispatch(new DeleteImagePost($imagePost))
.
... lines 1 - 6 | |
use App\Message\DeleteImagePost; | |
... lines 8 - 21 | |
class ImagePostController extends AbstractController | |
{ | |
... lines 24 - 69 | |
public function delete(ImagePost $imagePost, MessageBusInterface $messageBus) | |
{ | |
$messageBus->dispatch(new DeleteImagePost($imagePost)); | |
return new Response(null, 204); | |
} | |
... lines 76 - 93 | |
} |
Pretty cool, right? Let's see if it all works. Move over, click the "x" and... hmm... it didn't disappear. And... it looks like it was a 500 error! Through the power of the profiler, we can click the little link to jump straight to a big, beautiful, HTML version of that exception. Interesting:
No handler for message
App\Message\DeleteImagePost
That's interesting. Before we figure out what went wrong, I want to mention one thing: in a command bus, each message normally has exactly one handler: not two and not zero. And that's why Messenger gives us a helpful error if it can't find that handler. We'll talk more about this later and bend these rules when we talk about event buses.
Anyways... why does Messenger think that DeleteImagePost
doesn't have a handler? Can't it see the DeleteImagePostHandler
class? Find your terminal and run:
php bin/console debug:messenger
Woh! It only sees our one handler class! What this command really does is this: it finds all the "handler" classes in the system, then prints the "message" that it handles next to it. So... this confirms that, for some reason, Messenger doesn't see our handler!
And... you may have spotted my mistake! To find all the handlers, Symfony looks in the src/
directory for classes that implement MessageHandlerInterface
. And... I forgot that part! Add implements MessageHandlerInterface
.
... lines 1 - 7 | |
use Symfony\Component\Messenger\Handler\MessageHandlerInterface; | |
class DeleteImagePostHandler implements MessageHandlerInterface | |
{ | |
... lines 12 - 28 | |
} |
Run debug:messenger
again:
php bin/console debug:messenger
Now it sees it! Let's try it again: close up the profiler, try hitting "x" and... this time it works!
Status report: we have two messages and each has a handler that's potentially doing some pretty heavy work, like image manipulation or talking across a network if files are stored in the cloud. It's time to talk about transports: the key concept behind taking this work and doing it asynchronously so that our users don't have to wait for all that heavy work to finish before getting a response.
Hey Mickaël M.
You got a tiny error in your code. In your constructor you have this $this->$photoManager = $photoManager;
, remove the "$" from $this->$photoManager
.
Cheers!
// composer.json
{
"require": {
"php": "^7.1.3",
"ext-ctype": "*",
"ext-iconv": "*",
"composer/package-versions-deprecated": "^1.11", // 1.11.99
"doctrine/annotations": "^1.0", // v1.8.0
"doctrine/doctrine-bundle": "^1.6.10", // 1.11.2
"doctrine/doctrine-migrations-bundle": "^1.3|^2.0", // v2.0.0
"doctrine/orm": "^2.5.11", // v2.6.3
"intervention/image": "^2.4", // 2.4.2
"league/flysystem-bundle": "^1.0", // 1.1.0
"phpdocumentor/reflection-docblock": "^3.0|^4.0", // 4.3.1
"sensio/framework-extra-bundle": "^5.3", // v5.3.1
"symfony/console": "4.3.*", // v4.3.2
"symfony/dotenv": "4.3.*", // v4.3.2
"symfony/flex": "^1.9", // v1.18.7
"symfony/framework-bundle": "4.3.*", // v4.3.2
"symfony/messenger": "4.3.*", // v4.3.4
"symfony/property-access": "4.3.*", // v4.3.2
"symfony/property-info": "4.3.*", // v4.3.2
"symfony/serializer": "4.3.*", // v4.3.2
"symfony/validator": "4.3.*", // v4.3.2
"symfony/webpack-encore-bundle": "^1.5", // v1.6.2
"symfony/yaml": "4.3.*" // v4.3.2
},
"require-dev": {
"easycorp/easy-log-handler": "^1.0.7", // v1.0.7
"symfony/debug-bundle": "4.3.*", // v4.3.2
"symfony/maker-bundle": "^1.0", // v1.12.0
"symfony/monolog-bundle": "^3.0", // v3.4.0
"symfony/stopwatch": "4.3.*", // v4.3.2
"symfony/twig-bundle": "4.3.*", // v4.3.2
"symfony/var-dumper": "4.3.*", // v4.3.2
"symfony/web-profiler-bundle": "4.3.*" // v4.3.2
}
}
Hi guys !
I've got this error :
Catchable Fatal Error: Object of class App\Photo\PhotoFileManager could not be converted to string
`
private $photoManager;
private $entityManager;
public function __construct(PhotoFileManager $photoManager, EntityManagerInterface $entityManager)
{ $this->$photoManager = $photoManager;
}
`
$this->$photoManager = $photoManager is highlight in my profiler.
And my delete function is the same as the video :
`
public function delete(ImagePost $imagePost, MessageBusInterface $messageBus)
`
Any clues ??