When Easy Is Hard

When Easy Is Hard

...and when hard is easy. Building great applications in PHP is easy - with frameworks like Symfony, Laravel, Yii - it is easier than ever. But the easier you try to make developing applications, the harder it becomes to keep developing one continuously. In my talk I will discuss what easy and hard means at different stages of the project and how we
can make it easier for ourselves to deliver software that matters.

Presented at PHP London meetup http://phplondon.org/2015/03/when-easy-is-hard/

2bd48651cd01e0ca2e0a255a63da77aa?s=128

Marek Matulka

March 12, 2015
Tweet

Transcript

  1. 6.
  2. 8.
  3. 10.

    <?php namespace App\Http\Controllers; use App\Http\Controllers\Controller; class UserController extends Controller {

    /** * Show the profile for the given user. * * @param int $id * @return Response */ public function showProfile($id) { return view('user.profile', ['user' => User::findOrFail($id)]); } }
  4. 11.

    <?php namespace Acme\ConferenceBundle\Controller; use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Symfony\Component\HttpFoundation\Response; class RegistrationController extends

    Controller { public function registerAction($id) { $conference = $this->getDoctrine() ->getRepository('AcmeConferenceBundle:Conference') ->find($id); $this->get('acme_conference.enrolment.service') ->enrol($conference, $this->getUser())) { return new Response('Success!'); } }
  5. 12.
  6. 17.

    <?php namespace Acme\ConferenceBundle\Controller; use Symfony\Bundle\FrameworkBundle\Controller\Controller; class RegistrationController extends Controller {

    public function registerAction($id) { $conference = $this->getDoctrine() ->getRepository('AcmeConferenceBundle:Conference')->find($id); if ( $enrolment = $this->get('acme_conference.enrolment.service') ->enrol($conference, $this->getUser()) ) { $em = $this->getDoctrine()->getEntityManager(); $em->persist($enrolment); $em->flush(); $this->get('acme_conference.notification.service') ->notify($this->getUser(), 'success'); return $this->redirect('registration_successful') } return $this->redirect('registration_failed'); } } © http://warrenmars.com/
  7. 25.
  8. 29.

    Do you expect the plumber to “apply the workaround” to

    your leaking pipe? © http://www.asbestosjustice.co.uk/wp-content/uploads/2014/11/jobs-to-consider-7-plumber-1093362-TwoByOne.jpg
  9. 31.

    What has happened? - too close to the framework -

    no separation of concerns - mixed layers - no tests!
  10. 36.
  11. 39.

    class RegistrationController { public function registerAction($id) { $learner = Session::getUser();

    $conference = Conference::findOrFail($id); Enrolment::enrolLearnerInConference($learner, $conference); Mail::send('emails.welcome', array('key' => 'value'), function($message) use ($learner, $conference) { $message->to($learner->email(), $learner->name()) ->subject('Thanks for registering for '.$conference->name().'!'); } ); } }
  12. 41.

    class RegistrationController { public function __construct( Enrolment $enrolment, Learner $learner,

    Notifier $notifier ) { $this->enrolment = $enrolment; $this->learner = $learner; $this->notifier = $notifier; } public function registerAction(Conference $conference) { $enrolment = $this->enrolment->enrolLearnerInConference($this->learner, $conference); $this->notifier->successfulEnrolment($enrolment); } }
  13. 43.

    class RegistrationController { public function __construct( EnrolmentHandler $enrolmentHandler, Learner $learner

    ) { $this->enrolmentHandler = $enrolmentHandler; $this->learner = $learner; } public function registerAction(Conference $conference) { $this->enrolmentHandler->handle( new EnrolLearnerInConference($this->learner, $conference); ); } }
  14. 44.

    class RegistrationController { public function __construct( EnrolmentHandler $enrolmentHandler, Learner $learner

    ) { $this->enrolmentHandler = $enrolmentHandler; $this->learner = $learner; } public function registerAction(Conference $conference) { $this->enrolmentHandler->handle( new EnrolLearnerInConference($this->learner, $conference); ); } }
  15. 45.

    class RegistrationController { public function registerAction($id) { $learner = Session::getUser();

    $conference = Conference::findOrFail($id); Enrolment::enrolLearnerInConference($learner, $conference); Mail::send('emails.welcome', array('key' => 'value'), function($message) use ($learner, $conference) { $message->to($learner->email(), $learner->name()) ->subject('Thanks for registering for '.$conference->name().'!'); } ); } }
  16. 46.

    class EnrolmentHandler { public function __construct(EnrolmentRepository $enrolmentRepository, Mailer $mailer) {

    $this->repository = $enrolmentRepository; $this->mailer = $mailer; } public function handle(EnrolLearnerInConference $enrolmentCommand) { $enrolment = $enrolmentCommand->execute(); $this->repository>save($enrolment); $this->mailer->send('emails.welcome', array('key' => 'value'), function($message) use ($enrolment) { $message->to($enrolment->learner()->email(), $enrolment->learner()->name()) ->subject('Thanks for registering for '.$enrolment->conference->name().'!'); }); }
  17. 48.

    class EnrolmentHandler { public function __construct(EnrolmentRepository $enrolmentRepository, Mailer $mailer) {

    $this->repository = $enrolmentRepository; $this->mailer = $mailer; } public function handle(EnrolLearnerInConference $enrolmentCommand) { $enrolment = $enrolmentCommand->execute(); $this->repository>save($enrolment); $this->mailer->send('emails.welcome', array('key' => 'value'), function($message) use ($enrolment) { $message->to($enrolment->learner()->email(), $enrolment->learner()->name()) ->subject('Thanks for registering for '.$enrolment->conference->name().'!'); }); }
  18. 49.

    class EnrolmentHandler { public function __construct( EnrolmentRepository $enrolmentRepository, EnrolmentNotifier $notifier

    ) { $this->repository = $enrolmentRepository; $this->notifier = $notifier; } public function handle(EnrolLearnerInConference $enrolmentCommand) { if ($enrolment = $enrolmentCommand->execute()) { $this->repository->save($enrolment); $this->notifier->successfulEnrolment($enrolment); } } }
  19. 50.

    interface EnrolmentNotifier { /** * @param Enrolment $enrolment * *

    @throws FaildToNotify */ public function successfulEnrolment(Enrolment $enrolment); }
  20. 51.

    interface EnrolmentRepository { /** * @param integer $id * @return

    Enrolment */ public function findOneById($id); /** * @param Enrolment $enrolment */ public function save(Enrolment $enrolment); }
  21. 61.

    UI Adapter Log Adapter Data Storage Adapter Domain External Data

    Adapter UI client port log adapter port persistence layer port data provider port
  22. 64.

    Why bother writing clean code? - decoupled - reusable -

    testable - maintainable - changeable
  23. 66.

    class EnrolmentHandler { public function __construct( EnrolmentRepository $enrolmentRepository, EnrolmentNotifier $notifier

    ) { $this->repository = $enrolmentRepository; $this->notifier = $notifier; } public function handle(EnrolLearnerInConference $enrolmentCommand) { if ($enrolment = $enrolmentCommand->execute()) { $this->repository->save($enrolment); $this->notifier->successfulEnrolment($enrolment); } } }
  24. 67.

    class DoctrineEnrolmentRepository implements EnrolmentRepository { public function __construct(EntityManager $entityManager) {

    $this->em = $entityManager; } public function findOneById($id) { return $this->em->find(Enrolment::class, $id); } public function save(Enrolment $enrolment) { $this->em->persist($enrolment); $this->em->flush($enrolment); } }
  25. 68.

    class InMemoryEnrolmentRepository implements EnrolmentRepository { public function __construct() { $this->repository

    = new InMemoryRepository(); } public function findOneById($id) { return $this->repository->findById($id); } public function save(Enrolment $enrolment) { $this->repository->save($enrolment); } }
  26. 71.