1

I don't really know, how to call this approach, but question is - is it ok doing something like this. I create an entity Messages and set Connection inside this entity.

<?php

class Message {
   private $connection;

   public function setName();
   public function setSubject();

   public function send()
   {
       $this->connection->send($this);
   }
}

It looks like violation of single responsibility principle.

I have service Mailer which can create Message entity (already with Connection inside). Also Mailer can send Message entity by itself.

And there are two options, how I can use it;

<?php
// First
$mailer->send($message);

// Second
$message->send();

Is it not ok, and I should use only first approach?

Mikhail Stroev
  • 105
  • 1
  • 7

1 Answers1

0

The answer to this strongly depends on how strong of an adherent you are to the various camps of architecture (DDD vs an Anemic Domain model). If you are an adherent of DDD, you likely would advocate that the message can send itself, that would allow it to do all sorts of things like validate it's own state, change that state based on the result of the sending etc. If you are using a thin/anemic domain model, you would make the message dumb, and rely on a service to send the message.

Each path has tradeoffs, you should figure out which one is right for your application and stick to it. Mixing them turns your application into a mess.

Community
  • 1
  • 1
Rob Conklin
  • 8,806
  • 1
  • 19
  • 23