mercredi 28 novembre 2018

Use Connection inside Entity

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?

Aucun commentaire:

Enregistrer un commentaire