Last active
          March 21, 2019 11:53 
        
      - 
      
- 
        Save JavierCane/e7a12b68d867ce2b7c2092a87e25e1e7 to your computer and use it in GitHub Desktop. 
Revisions
- 
        JavierCane revised this gist Jan 17, 2019 . 1 changed file with 62 additions and 22 deletions.There are no files selected for viewingThis file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,21 +1,34 @@ <?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/ /** @@ -30,7 +43,26 @@ public function __invoke(array $request): void } } // ****************************************************** // ****************************************************** // 🤔☝️ ¿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 { @@ -39,7 +71,7 @@ final class VideoPutController public function __construct() { $this->emailSender = new SendgridEmailSender(); $this->dbConnection = new DbConnection( [ 'user' => 'root', @@ -58,27 +90,24 @@ public function __invoke(array $request): void 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; @@ -92,13 +121,23 @@ public function __invoke(array $request): void } } // ****************************************************** // ****************************************************** // 🤟🔝🙌 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 @@ -107,7 +146,9 @@ public function __invoke(string $to, string $subject, string $content): void } } // namespace MySompany\MyProject\Infrastructure; // use MySompany\MyProject\Domain\EmailSender; // ☝️ Nos acoplamos a la interface, no a la implementación concreta final class UserPatchController { @@ -131,10 +172,9 @@ public function __invoke(array $request): void 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); } 
- 
        JavierCane created this gist Jan 16, 2019 .There are no files selected for viewingThis file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,140 @@ <?php // HERENCIA ✋🔴💩 abstract class Controller { protected function sendEmail(string $to, string $subject, string $content): void { // se envía } protected function saveOnDb(array $data): void { // MySQL } // … } // 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? 👌🔝🙌 final class VideoPutController { private $emailSender; private $dbConnection; public function __construct() { $this->emailSender = new EmailSender(); $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 SIN INVERSIÓn (sólo inyección) 👌🔝🙌 final class EmailSender { public function __invoke(string $to, string $subject, string $content): void { // Envía email } } // use LaSalle\StudentsWebsite\Infrastructure\EmailSender; 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']); } } // COMPOSICIÓN CON INVERSIÓN 👌🔝🙌 interface EmailSender { public function __invoke(string $to, string $subject, string $content): void; } final class SendgridEmailSender implements EmailSender { public function __invoke(string $to, string $subject, string $content): void { // Envía email vía API Sendgrid } } // use LaSalle\StudentsWebsite\Domain\EmailSender; 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(); $emailSender = new MandrillEmailSender(); $dbConnection = new DbConnection(['…']); $userPostController = new UserPatchController($emailSender, $dbConnection); }