I have a wrapper class, to get email templates:
class Email
{
public function getWellcome()
{
return 'body';
}
public function getRegistration()
{
return 'body';
}
public function getCommentSent()
{
return 'body';
}
}
for my class, it is just easy to pass it:
class User
{
private $email;
public function __construct($email)
{
$this->email = $email;
}
public function asd()
{
$m = $this->email->getRegistration();
MailHandler::send($m);
}
}
new User(new Email());
but I feel somewhat violating LoD. What if I refactor it like that:
class User
{
private $registrationEmail;
public function __construct($registrationEmail)
{
$this->registrationEmail= $registrationEmail;
}
public function asd()
{
$m = $this->registrationEmail;
MailHandler::send($m);
}
}
new User(new Email()->getRegistration());
this looks a bit nicer, but I would then need to inject all neccessary objects one by one, not to mention what if I need to cycle through all emails? Which way is preferable?
The way I would do it look something like this:
$user = new User;
$user->asd(new Template('/path/to/registration/template.html.twig'));
As I see it, you have two issues:
Email
class has multiple reasons to change (and seems mislabeled)User
class has a dependency, that isn't actually required most of timeP.S.
I am not all that convinced, that yourUser
class should be responsible for sending registration confirmation emails.