Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Code moi une RH! (PHP tour 2017)

1,504 views

Published on

Est-ce que nous allons parler de gestion des ressources humaines au PHP tour ? Non, je vous vais vous parler des modèles anémiques et des modèles riches. Depuis des années frameworks et ORM nous conditionnent à utiliser des modèles anémiques, une classe avec de simples setters et getters. A travers l’histoire d’Estelle, une RH, qui veut une application pour gérer ses salariés, je vais vous montrer comment changer nos vieilles habitudes afin de nous focaliser sur le métier que nous implémentons. Nous verrons ensemble quels outils utiliser, comme par exemple, le design pattern command bus, l’opérateur NEW de doctrine pour vos Data Transfert Object dans une application Symfony.

Example de code : https://github.com/aRn0D/code-me-hr

Published in: Software
  • DOWNLOAD THIS BOOKS INTO AVAILABLE FORMAT (2019 Update) ......................................................................................................................... ......................................................................................................................... Download Full PDF EBOOK here { https://soo.gd/irt2 } ......................................................................................................................... Download Full EPUB Ebook here { https://soo.gd/irt2 } ......................................................................................................................... Download Full doc Ebook here { https://soo.gd/irt2 } ......................................................................................................................... Download PDF EBOOK here { https://soo.gd/irt2 } ......................................................................................................................... Download EPUB Ebook here { https://soo.gd/irt2 } ......................................................................................................................... Download doc Ebook here { https://soo.gd/irt2 } ......................................................................................................................... ......................................................................................................................... ................................................................................................................................... eBook is an electronic version of a traditional print book THIS can be read by using a personal computer or by using an eBook reader. (An eBook reader can be a software application for use on a computer such as Microsoft's free Reader application, or a book-sized computer THIS is used solely as a reading device such as Nuvomedia's Rocket eBook.) Users can purchase an eBook on diskette or CD, but the most popular method of getting an eBook is to purchase a downloadable file of the eBook (or other reading material) from a Web site (such as Barnes and Noble) to be read from the user's computer or reading device. Generally, an eBook can be downloaded in five minutes or less ......................................................................................................................... .............. Browse by Genre Available eBooks .............................................................................................................................. Art, Biography, Business, Chick Lit, Children's, Christian, Classics, Comics, Contemporary, Cookbooks, Manga, Memoir, Music, Mystery, Non Fiction, Paranormal, Philosophy, Poetry, Psychology, Religion, Romance, Science, Science Fiction, Self Help, Suspense, Spirituality, Sports, Thriller, Travel, Young Adult, Crime, Ebooks, Fantasy, Fiction, Graphic Novels, Historical Fiction, History, Horror, Humor And Comedy, ......................................................................................................................... ......................................................................................................................... .....BEST SELLER FOR EBOOK RECOMMEND............................................................. ......................................................................................................................... Blowout: Corrupted Democracy, Rogue State Russia, and the Richest, Most Destructive Industry on Earth,-- The Ride of a Lifetime: Lessons Learned from 15 Years as CEO of the Walt Disney Company,-- Call Sign Chaos: Learning to Lead,-- StrengthsFinder 2.0,-- Stillness Is the Key,-- She Said: Breaking the Sexual Harassment Story THIS Helped Ignite a Movement,-- Atomic Habits: An Easy & Proven Way to Build Good Habits & Break Bad Ones,-- Everything Is Figureoutable,-- What It Takes: Lessons in the Pursuit of Excellence,-- Rich Dad Poor Dad: What the Rich Teach Their Kids About Money THIS the Poor and Middle Class Do Not!,-- The Total Money Makeover: Classic Edition: A Proven Plan for Financial Fitness,-- Shut Up and Listen!: Hard Business Truths THIS Will Help You Succeed, ......................................................................................................................... .........................................................................................................................
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here

Code moi une RH! (PHP tour 2017)

  1. 1. CODEMEAHR!
  2. 2. HI,IAMARNAUD!
  3. 3. ANEMICMODELVERSUSMODELRICH
  4. 4. PLAYGROUND Business: Human resources management Technical: Symfony / Doctrine ORM
  5. 5. WHATDOWEUSUALLYDO?CRUD! Create Read Update Delete
  6. 6. WESTARTWITHCREATINGANEMICMODEL
  7. 7. namespace AlDomain; final class Employee { /** @var Uuid */ private $id; /** @var string */ private $name; /** @var string */ private $position; /** @var DateTimeInterface */ private $createdAt; /** @var DateTimeInterface */ private $deletedAt; // Getter and Setter }
  8. 8. THENAFORMANDACONTROLLER
  9. 9. namespace AlPresenterForm; final class EmployeeType extends AbstractType { public function buildForm(FormBuilderInterface $builder, array $options) { $builder->add('name'); $builder->add('position'); } public function configureOptions(OptionsResolver $resolver) { $resolver->setDefaults(array( 'data_class' => Employee::class, )); } }
  10. 10. namespace AlPresenterController; class EmployeeController extends Controller { public function indexAction(Request $request) { } public function showAction(Request $request) { } public function createAction(Request $request) { } public function updateAction(Request $request) { } public function deleteAction(Request $request) { } }
  11. 11. namespace AlPresenterController; class EmployeeController extends Controller { public function createAction(Request $request) { $form = $this->createForm(EmployeeType::class, new Employee()); $form->handleRequest($request); // First, data are mapped to the model and it is validated thereafter. if ($form->isSubmitted() && $form->isValid()) { $em = $this->get('doctrine.orm.entity_manager'); $em->persist($form->getData()); $em->flush(); return $this->redirectToRoute('employee_list'); } return $this->render('employee/hire.html.twig', [ 'form' => $form->createView() ]); } }
  12. 12. LET'SREFACTOROURANEMICMODEL
  13. 13. HOWDOESESTELLETALKABOUTHER WORK?
  14. 14. namespace AlDomain; final class Employee implements EmployeeInterface { public function hire(Uuid $identifier, string $name, string $forPosition) { } public function promote(string $toNewPosition) { } public function fire() { } public function retire() { } }
  15. 15. namespace AlDomain; final class Employee implements EmployeeInterface { /** @var Uuid */ private $id; /** @var string */ private $name; /** @var string */ private $position; /** @var DateTimeInterface */ private $hiredAt; /** @var DateTimeInterface */ private $firedAt = null; /** @var DateTimeInterface */ private $retiredAt = null; }
  16. 16. namespace AlDomain; final class Employee implements EmployeeInterface { private function __construct(Uuid $identifier, string $name, string $position) { $this->id = $identifier; $this->name = $name; $this->position = $position; $this->hireAt = new DateTime(); } public static function hire(Uuid $identifier, string $name, string $forPosition) { return new self($identifier, $name, $forPosition, $hiredAt); } public function promote(string $toNewPosition) { $this->position = $toNewPosition; } public function fire() { $this->firedAt = new DateTime(); } public function retire() { $this->retiredAt = new DateTime(); } }
  17. 17. namespace AlDomain; final class Employee implements EmployeeInterface { private function __construct(Uuid $identifier, string $name, string $position) { if ($hiredAt < new DateTime('2013-01-01')) { throw new OutOfRangeException( 'The company did not exist before 2013-01-01' ); } $this->id = $identifier; $this->name = $name; $this->position = $position; $this->hireAt = new DateTime(); } public function retire() { if (null !== $this->fired) { throw new Exception( sprint('%s employee has been fired!', $this->name) ); } $this->retiredAt = new DateTime(); } }
  18. 18. HOWCANWEUSEITINOURAPPLICATION?
  19. 19. FIRSTPROBLEM:HOWDOWEUSE DOCTRINE? We use them as query repositories interface EmployeeRepository { public function findByNameAndPositionWithoutFiredPeople( string $name, string $position ); }
  20. 20. WHATISAREPOSITORY? «A repository behaves like a collection of unique entities without taking care about the storage»
  21. 21. WHATDOESITLOOKLIKE?
  22. 22. namespace AlInfrastructure; final class EmployeeRepository implements EmployeeRepositoryInterface { public function get(Uuid $identifier) { } public function add(EmployeeInterface $employee) { } public function remove(EmployeeInterface $employee) { } }
  23. 23. namespace AlInfrastructure; final class EmployeeRepository implements EmployeeRepositoryInterface { /** @var EntityManagerInterface */ private $entityManager; public function __construct(EntityManagerInterface $entityManager) { $this->entityManager = $entityManager; } }
  24. 24. namespace AlInfrastructure; final class EmployeeRepository implements EmployeeRepositoryInterface { public function get(Uuid $identifier) { $employee = $this->entityManager->find( Employee::class, $identifier->toString() ); if (null === $employee) { throw new NonExistingEmployee($identifier->toString()); } return $employee; } public function add(EmployeeInterface $employee) { $this->entityManager->persist($employee); $this->entityManager->flush($employee); } public function remove(EmployeeInterface $employee) { $this->entityManager->remove($employee); $this->entityManager->flush($employee); } }
  25. 25. ISITMANDATORYTOUSESETTER?NO! Doctrine uses the re ection to map data Doctrine does not instantiate objects (Ocramius/Instantiator)
  26. 26. SECONDPROBLEM:FORMCOMPONENT PropertyAccessor is used to map data, it needs public properties or setter.
  27. 27. COMMANDTOTHERESCUE! « A Command is an object that represents all the information needed to call a method.»
  28. 28. LET’SCREATEACOMMAND
  29. 29. namespace AlApplication; final class HireEmployeeCommand { /** @var string */ public $name = ''; /** @var string */ public $position = ''; }
  30. 30. LET’SUPDATEOURCONTROLLER
  31. 31. namespace AlPresenterController; class EmployeeController extends Controller { public function hireAction(Request $request) { $form = $this->createForm(EmployeeType::class, new HireEmployeeCommand()); $form->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { $employeeCommand = $form->getData(); $employee = Employee::hire( Uuid::uuid4(), $employeeCommand->getName(), $employeeCommand->getPosition() ); $this->get('employee.repository')->add($employee); return $this->redirectToRoute('employee_list'); } return $this->render('employee/hire.html.twig', [ 'form' => $form->createView() ]); } }
  32. 32. NOW,ESTELLEWANTSTOIMPORT EMPLOYEES!
  33. 33. COMMANDBUSTOTHERESCUE «A Command Bus accepts a Command object and delegates it to a Command Handler.»
  34. 34. LET’SUPDATEOURCONTROLLER Here, we are going to use simple-bus/message-bus
  35. 35. namespace AlPresenterController; class EmployeeController extends Controller { public function hireAction(Request $request) { $form = $this->createForm(EmployeeType::class, new HireEmployeeCommand()); $form->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { $employeeCommand = $form->getData(); try { $this->get('command_bus')->handle($employeeCommand); } catch (Exception $e) { $this->addFlash('error', 'An error occurs!'); } return $this->redirectToRoute('employee_list'); } return $this->render('employee/hire.html.twig', [ 'form' => $form->createView() ]); } }
  36. 36. LET’SCREATEACOMMANDHANDLER
  37. 37. namespace AlApplicationHandler; final class HireEmployeeHandler { /** @var EmployeeRepositoryInterface */ private $employeeRepository; public function __construct(EmployeeRepositoryInterface $employeeRepository) { $this->employeeRepository = $employeeRepository; } public function handle(HireEmployee $command) { $employee = Employee::hire( Uuid::uuid4(), $command->getName(), $command->getPosition() ); $this->employeeRepository->add($employee); } }
  38. 38. ESTELLEWON'TUSEPHPMYADMINTOREAD DATA!
  39. 39. DOOURMODELSNEEDGETTER?NOT NECESSARILY!
  40. 40. DTO(DATATRANSFEROBJECT)TOTHE RESCUE! «A Data Transfer Object is an object that is used to encapsulate data, and to send it from one subsystem of an application to another.»
  41. 41. DOCTRINE(>=2.4):OPERATORNEW
  42. 42. namespace AlApplication; final class EmployeeDTO { /** string */ private $id; /** string */ private $name; /** string */ private $position; public function __construct(string $id, string $name, string $position) { $this->id = $id; $this->name = $name; $this->position = $position; } // Define an accessor for each property }}
  43. 43. namespace AlInfrastructure; final class EmployeeSearcher { private $entityManager; public function __construct(EntityManagerInterface $entityManager) { $this->entityManager = $entityManager; } public function findAll() { $queryBuilder = $this->entityManager->createQueryBuilder() ->from(Employee::class, 'e') ->select( sprintf('NEW %s(e.id, e.name, e.position)', EmployeeDTO::class) ); // NEW AlApplicationEmployeeDTO(e.id, e.name, e.position) return $queryBuilder->getQuery()->getResult(); } }
  44. 44. WECANSEARCHANDDISPLAYEMPLOYEE DATA!
  45. 45. MISREADINGOFDOCTRINE/DDD/CQRS Domain Driven Design Command Query Responsibility Segregation
  46. 46. WHATISTHEBESTSOLUTION? It depends on what you want!
  47. 47. THAT'STHEEND! https://joind.in/talk/6c974
  48. 48. THANKYOU!QUESTIONS? aRn0D _aRn0D https://joind.in/talk/6c974

×