Skip to content

Instantly share code, notes, and snippets.

@mangelsnc
Created August 28, 2019 08:06
Show Gist options
  • Save mangelsnc/16d7b189e0d21e7a5d61e7b805db9614 to your computer and use it in GitHub Desktop.
Save mangelsnc/16d7b189e0d21e7a5d61e7b805db9614 to your computer and use it in GitHub Desktop.
Without Demeter
<?php declare(strict_types=1);
interface EmailService
{
public function sendEmail(Email $email): void;
}
<?php declare(strict_types=1);
class MySqlUserRepository implements UserRepository
{
public function save(User $user): void
{
// Implementation
}
public function get(string $userId): User
{
// Implementation
}
}
<?php declare(strict_types=1);
class RegisterUserHandler
{
private $userRepository;
private $notifications
public function __construct(UserRepository $userRepository, WelcomeEmailSender $notificationService)
{
$this->userRepository = $userRepository;
$this->notificationService = $notificationService;
}
public function handle($name, $email)
{
$user = new User($email, $name);
$this->userRepository->save($user);
$this->notificationService->send($user->getEmail(), $user->getName());
}
}
<?php declare(strict_types=1);
class SmtpEmailService implements EmailService
{
public function sendEmail(Email $email): void
{
// implementation
}
}
<?php declare(strict_types=1);
interface UserRepository
{
public function save(User $user): void;
public function get(string $userId): User;
}
<?php declare(strict_types=1);
final class WelcomeEmailSender
{
private $emailService;
public function __construct(EmailService $emailService)
{
$this->emailService = $emailService;
}
public function send(string $emailAddress, string $username): void
{
$body = $this->composeBody($username);
$email = new Email($emailAddress, 'Welcome to our platform', $body);
}
private function composeBody(string $username)
{
// Code to compose body
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment