DEV Community

Marc Guinea
Marc Guinea

Posted on • Updated on

Improve your factories in Symfony

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"
}
Enter fullscreen mode Exit fullscreen mode

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');
    }
}
Enter fullscreen mode Exit fullscreen mode

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');
    }
}
Enter fullscreen mode Exit fullscreen mode

And now, in our services.yml file we initialize it as follows:

App\ChannelFactory:
    arguments:
      - App\EmailChannelHandler
      - App\SmsChannelHandler
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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!

Latest comments (11)

Collapse
 
dziugasdraksas profile image
DziugasDraksas

I would suggest minor optimization.
Do not create new $channel, since all channels are already initialized.
Just return it:

foreach($this->channels as $channel) {
    if ($type === $channel::getType()) {
        return $channel;
     }
}
Enter fullscreen mode Exit fullscreen mode
Collapse
 
fd6130 profile image
fd6130 • Edited

Hi, how do you use those class property in a static method? I try to call $this->channels and my IDE show error Cannot 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()?

Collapse
 
mguinea profile image
Marc Guinea

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!

Collapse
 
tomasvotruba profile image
Tomáš Votruba

You can also take it one step further, avoid tags and any YAML coding by using annotation autowire:

final class ChannelFactory
{
    /**
     * @param ChannelInterface[] $channels
     */
    public function __construct(private array $channels)
    {
    }

    // ...
}
Enter fullscreen mode Exit fullscreen mode
Collapse
 
mguinea profile image
Marc Guinea

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!

Collapse
 
vasilvestre profile image
Valentin Silvestre • Edited

I love this pattern ! It's really clean.
You could even simplify it using tagged_iterator.

_instanceof:
        App\ChannelHandlerInterface:
            tags: ['app.channel_handler']

App\ChannelFactory:
    arguments:
      - !tagged_iterator app.channel_handler
Enter fullscreen mode Exit fullscreen mode

Thanks to DI feature, you do not have to declare anything manually !

The idea come from Thibault Richard (github.com/t-richard) :)

Collapse
 
mguinea profile image
Marc Guinea

Wow! looks awesome! even better, thanks for sharing

Collapse
 
vasilvestre profile image
Valentin Silvestre

I think you could update your post with it ;)

Thread Thread
 
mguinea profile image
Marc Guinea

Done! many thanks :)

Collapse
 
aheiland profile image
Andreas Rau • Edited

How about?

final class ChannelFactory
{
    /** @var array<string, ChannelInterface> */
    private array $lookUp = [];

    public function __construct(...ChannelInterface channels)
    {
        foreach($channels as $channel) {
            $this->lookUp[$channel->getType()] = $channel;
        }
    }

    public function create(string $type): ChannelInteface
    {
        if (!array_key_exists($type, $this->lookUp)) {
            throw new InvalidArgumentException('Unknown channel given');
        }
        return $this->lookUp[$type];
    }
}
Enter fullscreen mode Exit fullscreen mode
Collapse
 
aless673 profile image
Alessandro GIULIANI • Edited

And this one ? ^^

final class ChannelFactory
{
    /** @var array<string, ChannelInterface> */
    private array $lookUp;

    public function __construct(ChannelInterface ...$channels)
    {
        $this->lookUp = array_combine(array_column($channels, 'type'), $channels);
    }

    public function create(string $type): ChannelInteface
    {
        return $this->lookUp[$type] ?? throw new InvalidArgumentException('Unknown channel given');
    }
}
Enter fullscreen mode Exit fullscreen mode