Sometimes we need to retrieve some kind of handler class that depending on an attribute is different.
Thanks to Factory pattern we can solve this scenario in an elegant way... but we can improve it through the Service Container.
In this case we are going to see how to do it with Symfony Service Container, but this could be achieved by any other Service Container implementation.
Example scenario
Let's imagine a (more or less) real situation and work with it: We have an endpoint that depending on payload parameter "channel", we will send a "message" through different possible communication channels (email, sms, letter...).
To make things easier, we will do not cover additional parameters as to
or phone_number
etc.
Example payload:
{
"channel": "email",
"message": "Hello world"
}
Using a simple factory
The easiest way to solve this is creating a factory class that solves that logic:
final class ChannelFactory
{
public static function create(string $type): ChannelInterface
{
switch($type) {
case 'email':
return new EmailChannelHandler();
break;
case 'sms':
return new SmsChannelHandler();
break;
}
throw new InvalidArgumentException('Unknown channel given');
}
}
Pros
- Easy to read if only 2 or 3 different types
Cons
- Difficult to read if lot of types
- You have to add code to add a new type
Using a simple factory through service container
Let's make it more scalable and maintainable!
We can have an array of compatible handlers and decide which one is the correct. You need to add the related type to the Channel handler with some method like getType()
.
final class ChannelFactory
{
public function __construct(private ChannelInterface ...$channels)
{
}
public function create(string $type): ChannelInterface
{
foreach($this->channels as $channel) {
if ($type === $channel::getType()) {
return new $channel;
}
}
throw new InvalidArgumentException('Unknown channel given');
}
}
And now, in our services.yml
file we initialize it as follows:
App\ChannelFactory:
arguments:
- App\EmailChannelHandler
- App\SmsChannelHandler
Pros
- You can add as many handlers as you want without changing php code
- Easy to add new handlers
Cons
- Maybe a little bit less easy to read for a noob
Conclusion
Service Container is a pattern really powerful and nowadays we have some amazing implementations that can help us a lot.
It's worth to use them and give to our application more flexibility even adding a little complexity for newcomers.
Updated! (2022-03-07)
As Valentin Silvestre suggested, there is a better way to set up the container to make it even more automagically:
_instanceof:
App\ChannelHandlerInterface:
tags: ['app.channel_handler']
App\ChannelFactory:
arguments:
- !tagged_iterator app.channel_handler
This is an awesome way to tell to service container that all implementations of ChannelHandlerInterface
should be loaded in our factory, so we don't need to write each one in a list.
Thanks Valentin!
Top comments (11)
I love this pattern ! It's really clean.
You could even simplify it using
tagged_iterator
.Thanks to DI feature, you do not have to declare anything manually !
The idea come from Thibault Richard (github.com/t-richard) :)
Wow! looks awesome! even better, thanks for sharing
I think you could update your post with it ;)
Done! many thanks :)
How about?
And this one ? ^^
You can also take it one step further, avoid tags and any YAML coding by using annotation autowire:
Yep, but then you are placing something from the Framework (infrastructure layer) to the factory (domain layer).
It's also a valid approach, but I prefer keeping things separated (depends on the architecture of your app, of course).
Anyway, thanks for the idea!
Hi, how do you use those class property in a static method? I try to call
$this->channels
and my IDE show errorCannot use '$this' in non-object context
.And do I still need to inject the factory class or I can just directly use it like
ChannelFactory::create()
?This was an error! just fixed... this shouldn't be a static class, we want to use service container to manage that data. Thanks & fixed!
I would suggest minor optimization.
Do not create new
$channel
, since all channels are already initialized.Just return it: