Last active
March 21, 2019 11:53
-
-
Save JavierCane/e7a12b68d867ce2b7c2092a87e25e1e7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
declare(strict_types = 1); | |
// ****************************************************** | |
// ****************************************************** | |
// 💩🔴✋ HERENCIA ✋🔴💩 | |
// ****************************************************** | |
// ****************************************************** | |
// namespace MySompany\MyProject\Infrastructure; | |
abstract class Controller | |
{ | |
protected function sendEmail(string $to, string $subject, string $content): void | |
{ | |
// Envía email vía API Sendgrid | |
} | |
protected function saveOnDb(array $data): void | |
{ | |
// Guarda datos en MySQL vía PDO | |
} | |
// … | |
} | |
// namespace MySompany\MyProject\Infrastructure; | |
// use MySompany\MyProject\Infrastructure\Controller; | |
// POST miwebmolona.com/users/ | |
/** | |
* @method(result) | |
*/ | |
final class UserPostController extends Controller | |
{ | |
public function __invoke(array $request): void | |
{ | |
parent::saveOnDb($request); | |
parent::sendEmail($request['user_email']); | |
} | |
} | |
// ****************************************************** | |
// ****************************************************** | |
// 🤔☝️ ¿COMPOSICIÓN? ☝️🤔 | |
// ****************************************************** | |
// ****************************************************** | |
// namespace MySompany\MyProject\Infrastructure; | |
final class SendgridEmailSender | |
{ | |
public function __invoke(string $to, string $subject, string $content): void | |
{ | |
// Envía email vía API Sendgrid | |
} | |
} | |
// namespace MySompany\MyProject\Infrastructure; | |
// use MySompany\MyProject\Infrastructure\SendgridEmailSender; | |
// use MySompany\MyProject\Infrastructure\DbConnection; | |
final class VideoPutController | |
{ | |
private $emailSender; | |
private $dbConnection; | |
public function __construct() | |
{ | |
$this->emailSender = new SendgridEmailSender(); | |
$this->dbConnection = new DbConnection( | |
[ | |
'user' => 'root', | |
'pass' => 'toor', | |
'port' => '3306' | |
] | |
); | |
} | |
public function __invoke(array $request): void | |
{ | |
$this->dbConnection->save($request); | |
$this->emailSender->__invoke($request['creator_email']); | |
} | |
} | |
array_map($userPostController, $requests); | |
// ****************************************************** | |
// ****************************************************** | |
// 😬👌 COMPOSICIÓN INYECTANDO DEPS 👌😬 | |
// ****************************************************** | |
// ****************************************************** | |
// namespace MySompany\MyProject\Infrastructure; | |
// use MySompany\MyProject\Infrastructure\SendgridEmailSender; // ☝️ Nos acoplamos a la implementación concreta | |
// use MySompany\MyProject\Infrastructure\DbConnection; | |
final class UserPatchController | |
{ | |
private $emailSender; | |
private $dbConnection; | |
public function __construct( | |
SendgridEmailSender $emailSender, | |
DbConnection $dbConnection | |
) { | |
$this->emailSender = $emailSender; | |
$this->dbConnection = $dbConnection; | |
} | |
public function __invoke(array $request): void | |
{ | |
$this->dbConnection->save($request); | |
$this->emailSender->__invoke($request['user_email']); | |
} | |
} | |
// ****************************************************** | |
// ****************************************************** | |
// 🤟🔝🙌 COMPOSICIÓN INVIRTIENDO DEPS 🔝🙌🤟 | |
// ****************************************************** | |
// ****************************************************** | |
// namespace MySompany\MyProject\Domain; | |
interface EmailSender | |
{ | |
public function __invoke(string $to, string $subject, string $content): void; | |
} | |
// namespace MySompany\MyProject\Infrastructure; | |
// use MySompany\MyProject\Domain\EmailSender; | |
final class SendgridEmailSender implements EmailSender | |
{ | |
public function __invoke(string $to, string $subject, string $content): void | |
{ | |
// Envía email vía API Sendgrid | |
} | |
} | |
// namespace MySompany\MyProject\Infrastructure; | |
// use MySompany\MyProject\Domain\EmailSender; // ☝️ Nos acoplamos a la interface, no a la implementación concreta | |
final class UserPatchController | |
{ | |
private $emailSender; | |
private $dbConnection; | |
public function __construct( | |
EmailSender $emailSender, | |
DbConnection $dbConnection | |
) { | |
$this->emailSender = $emailSender; | |
$this->dbConnection = $dbConnection; | |
} | |
public function __invoke(array $request): void | |
{ | |
$this->dbConnection->save($request); | |
$this->emailSender->__invoke($request['user_email']); | |
} | |
} | |
final class ProductionContainer | |
{ | |
//$emailSender = new SendgridEmailSender(); // ☝️ Podemos cambiar la implementación de la interface que usamos sin modificar cliente (UserPatchController) | |
$emailSender = new MandrillEmailSender(); | |
$dbConnection = new DbConnection(['…']); | |
$userPostController = new UserPatchController($emailSender, $dbConnection); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment