Last active
          March 21, 2019 11:53 
        
      - 
      
- 
        Save JavierCane/e7a12b68d867ce2b7c2092a87e25e1e7 to your computer and use it in GitHub Desktop. 
  
    
      This 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 characters
    
  
  
    
  | <?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); | |
| } | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment