Slide 1

Slide 1 text

Building an Open-Source Campaign Platform for the President of France . SymfonyCon 2017 / Nov. 17th / Cluj / Romania Hugo Hamon

Slide 2

Slide 2 text

Hugo Hamon Senior Software Developer 15 years of PHP experience 10 years of Symfony experience Conferences speaker @hhamon on social networks Books (co) author

Slide 3

Slide 3 text

Titouan Galopin Enrolled in the En-Marche movement Initiated the Symfony migration Full-Stack Developer Setup the new technical stack @titouangalopin on Twitter

Slide 4

Slide 4 text

Innovative e-learning solution targeting Symfony teams. https://university.sensiolabs.com

Slide 5

Slide 5 text

The Context

Slide 6

Slide 6 text

Why is the Presidential election such a challenging environment?

Slide 7

Slide 7 text

Why building a Citizen Collaborative Platform?

Slide 8

Slide 8 text

Accept donations Register adherents Facilitate communication Structure movement

Slide 9

Slide 9 text

April 2016

Slide 10

Slide 10 text

No content

Slide 11

Slide 11 text

Summer 2016

Slide 12

Slide 12 text

+

Slide 13

Slide 13 text

No content

Slide 14

Slide 14 text

October 2016

Slide 15

Slide 15 text

No content

Slide 16

Slide 16 text

https://github.com/enmarche/en-marche.fr

Slide 17

Slide 17 text

https://en-marche.fr

Slide 18

Slide 18 text

Response Web Design Full featured Admin area Users management Fined grained permissions Http caching Images lazy loading Content Management Unit & functional tests Mass emails management Maps & address geocoding Social networks integration Procurations management Media management Field actions reports Legislatives elections support Logging Search engine Etc.

Slide 19

Slide 19 text

End of Year 2016

Slide 20

Slide 20 text

Emmanuel Macron formalizes his candidacy for the presidential election.

Slide 21

Slide 21 text

Symfony 3.2 also comes out!

Slide 22

Slide 22 text

Coincidence? J

Slide 23

Slide 23 text

No content

Slide 24

Slide 24 text

No content

Slide 25

Slide 25 text

# app/config/parameters.yml parameters: env(DATABASE_HOST): db env(DATABASE_PORT): 3306 env(DATABASE_SOCKET): null env(DATABASE_NAME): enmarche env(DATABASE_USER): root env(DATABASE_PASSWORD): root

Slide 26

Slide 26 text

No content

Slide 27

Slide 27 text

class AssetsController extends Controller { /** * @Route("/assets/{path}", name="asset_url") * @Cache(maxage=900, smaxage=900) */ public function assetAction(Request $request, string $path): Response { // ... $glide = $this->get('app.glide'); $glide->setResponseFactory(new SymfonyResponseFactory($request)); try { return $glide->getImageResponse($path, $request->query->all()); } catch (FileNotFoundException $e) { throw $this->createNotFoundException('', $e); } } }

Slide 28

Slide 28 text

January 2017

Slide 29

Slide 29 text

No content

Slide 30

Slide 30 text

Week n People at En-Marche write the User Stories and introduce them at SensioLabs on Thursday. Week n+1 Poker planning session on Tuesday with the SensioLabs team. Week n+2 New coding sprint starts on Monday. Demo session is on Friday afternoon.

Slide 31

Slide 31 text

trait EntityIdentityTrait { /** * @ORM\Id * @ORM\Column(type="integer") * @ORM\GeneratedValue */ private $id; /** * @ORM\Column(type="uuid") * @var \Ramsey\Uuid\UuidInterface */ private $uuid; // ... + getters } Custom UUIDs for Doctrine entities.

Slide 32

Slide 32 text

class MembershipController extends Controller { // ... public function registerAction(Request $request): Response { $membership = MembershipRequest::createWithCaptcha(...); $form = $this ->createForm(MembershipRequestType::class, $membership) ->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { $this->get('app.membership')->handle($membership); return $this->redirectToRoute('app_membership_donate'); } return $this->render('...', [ 'form' => $form->createView(), // ... ]); } } Thin & dumb controllers.

Slide 33

Slide 33 text

src/Mailjet/ ├── ApiClient.php ├── ClientInterface.php ├── EmailTemplate.php ├── EmailTemplateFactory.php ├── Event/ │ ├── MailjetEvent.php │ └── MailjetEvents.php ├── EventSubscriber/ │ └── EmailPersisterEventSubscriber.php ├── Exception/ │ └── MailjetException.php ├── MailjetService.php ├── MailjetUtils.php ├── Message/ │ ├── AdherentAccountActivationMessage.php │ ├── AdherentAccountConfirmationMessage.php │ ├── ... │ └── TonMacronFriendMessage.php └── Transport/ ├── ApiTransport.php ├── RabbitMQTransport.php └── TransportInterface.php

Slide 34

Slide 34 text

class MailjetService { // ... public function sendMessage(MailjetMessage $message): void { $email = $this->factory->createFromMailjetMessage($message); $event = new MailjetEvent($message, $email); try { $this->dispatch(MailjetEvents::DELIVERY_MESSAGE, $event); $this->transport->sendTemplateEmail($email); $this->dispatch(MailjetEvents::DELIVERY_SUCCESS, $event); } catch (MailjetException $e) { $this->dispatch( MailjetEvents::DELIVERY_ERROR, new MailjetEvent($message, $email, $e) ); } } }

Slide 35

Slide 35 text

class MembershipRequestHandler { // ... public function handle(MembershipRequest $membershipRequest) { $adherent = $this->factory->createFromMembershipRequest($membershipRequest); $token = AdherentActivationToken::generate($adherent); $this->manager->persist($adherent); $this->manager->persist($token); $this->manager->flush(); $message = AdherentAccountActivationMessage::createFromAdherent( $adherent, $this->generateMembershipActivationUrl($adherent, $token) ); $this->mailjet->sendMessage($message); $e = new AdherentAccountWasCreatedEvent($adherent); $this->dispatch(..., $e); } }

Slide 36

Slide 36 text

February 2017

Slide 37

Slide 37 text

No content

Slide 38

Slide 38 text

class CommitteeManagementAuthority { // ... function followCommittee(Adherent $adherent, Committee $committee): void { $this->manager->followCommittee($adherent, $committee); if (!$hosts = $this->manager->getCommitteeHosts($committee)) { return; } $this->mailjet->sendMessage(CommitteeNewFollowerMessage::create( $committee, $hosts, $adherent, $this->urlGenerator->getUrl('...', $committee) )); } } Service Layer

Slide 39

Slide 39 text

Service Layer Custom voters src/Committee/Voter ├─ AbstractCommitteeVoter.php ├─ CreateCommitteeVoter.php ├─ FollowCommitteeVoter.php ├─ HostCommitteeVoter.php ├─ ShowCommitteeVoter.php └─ SuperviseCommitteeVoter.php

Slide 40

Slide 40 text

class Geocoder implements GeocoderInterface { // ... public function geocode(string $address): Coordinates { try { $addresses = $this->geocoder->geocode($address); } catch (\Exception $exception) { throw GeocodingException::create($address, $exception); } if (!$geo = $addresses->first()) { throw GeocodingException::create($address); } return new Coordinates( $geo->getLatitude(), $geo->getLongitude() ); } } Geocoding addresses Based on Google Maps

Slide 41

Slide 41 text

class EntityAddressGeocodingSubscriber implements EventSubscriberInterface { // ... public function onCommitteeCreated(CommitteeWasCreatedEvent $event) { $this->updateGeocodableEntity($event->getCommittee()); } private function updateGeocodableEntity(GeoPointInterface $geocodable) { if ($coords = $this->geocode($geocodable->getGeocodableAddress())) { $geocodable->updateCoordinates($coords); $this->manager->flush(); } } } Doctrine listener to update coordinates of any Geocodable entity when it’s saved to the database.

Slide 42

Slide 42 text

No content

Slide 43

Slide 43 text

No content

Slide 44

Slide 44 text

trait NearbyTrait { public function getNearbyExpression(): string { return '(6371 * acos(cos(radians(:latitude)) * cos(radians(n.postAddress.latitude)) * cos(radians(n.postAddress.longitude) - radians(:longitude)) + sin(radians(:latitude)) * sin(radians(n.postAddress.latitude))))'; } }

Slide 45

Slide 45 text

trait NearbyTrait { function createNearbyQueryBuilder(Coordinates $coordinates, bool $hidden): { $hidden = $hidden ? 'hidden' : ''; return $this ->createQueryBuilder('n') ->addSelect($this->getNearbyExpression().' as '.$hidden.' dst_between') ->setParameter('latitude', $coordinates->getLatitude()) ->setParameter('longitude', $coordinates->getLongitude()) ->where('n.postAddress.latitude IS NOT NULL') ->andWhere('n.postAddress.longitude IS NOT NULL') ->orderBy('dst_between', 'asc') ; } }

Slide 46

Slide 46 text

/** * @group functional */ class CommitteeControllerTest extends MysqlWebTestCase { // ... function testAnonymousUserIsNotAllowedToFollowCommittee() { $crawler = $this->client->request('GET', ...); $this->assertResponseStatusCode(200, ...); $this->assertFalse($this->seeFollowLink($crawler)); $this->assertFalse($this->seeUnfollowLink($crawler)); $this->assertTrue($this->seeRegisterLink($crawler)); } } Functional Testing PHPUnit

Slide 47

Slide 47 text

Continuous Integration Pull-Requests SensioLabsInsight Style CI Circle CI

Slide 48

Slide 48 text

March 2017

Slide 49

Slide 49 text

Program announced! +6,000 users live +300,000 daily

Slide 50

Slide 50 text

{ "@context": "http://schema.org", "@type": "WebSite", "url": "http://en-marche.fr", "name": "En Marche !", "image": "http://en-marche.fr/images/default_sharer.jpg", "description": "Pour ceux qui ... l'Europe.", "funder": { "@type": "Person", "givenName": "Emmanuel", "familyName": "Macron", "jobTitle": "President of France" } } Schema.org JSON LD Metadata in HTML code

Slide 51

Slide 51 text

Optimizing natural indexing in search engines.

Slide 52

Slide 52 text

# app/config/parameters.yml parameters: # Staging configuration env(ENABLE_CANARY): 1 # Production configuration env(ENABLE_CANARY): 0 Feature Flags Env vars

Slide 53

Slide 53 text

class PageController extends Controller { // ... public function ellesMarchentAction() { if (!$this->getParameter('enable_canary'))) { throw $this->createNotFoundException(); } return $this->render('... '); } } Feature Flags

Slide 54

Slide 54 text

Procuration System

Slide 55

Slide 55 text

Asynchronous Tasks use AppBundle\Mailer\EmailTemplate; use OldSound\RabbitMqBundle\RabbitMq\Producer; class MailerProducer extends Producer implements MailerProducerInterface { public function scheduleEmail(EmailTemplate $email): void { $this->publish(json_encode([ 'uuid' => (string) $email->getUuid(), ])); } }

Slide 56

Slide 56 text

// ... abstract class AbstractMailerConsumer extends AbstractConsumer { // ... protected function doExecute(array $data): int { try { if (!$message = $this->repository->findOneByUuid($data['uuid'])) { $this->logger->error('Email not found', $data); return self::MSG_ACK; } if ($delivered = $this->sendEmail($message->getRequestPayloadJson())) { $this->getEmailRepository()->setDelivered($message, $delivered); } return $delivered ? self::MSG_ACK : self::MSG_REJECT_REQUEUE; } catch (...) { ... } } }

Slide 57

Slide 57 text

April 2017

Slide 58

Slide 58 text

Official regulations require candidates to stop proactive propaganda 24h before each ballot.

Slide 59

Slide 59 text

Notifications Committees & events Make the website read-only! Contents publication Emails

Slide 60

Slide 60 text

Who’s trully concerned?

Slide 61

Slide 61 text

https://commons.wikimedia.org/wiki/File%3AMapadefrancia.svg

Slide 62

Slide 62 text

UTC -3 Guyana Saint Pierre and Miquelon UTC -4 St. Martin St. Barthelemy Guadeloupe Martinique UTC -8 Clipperton UTC -9 / UTC -9,5 / UTC -10 French Polynesia https://commons.wikimedia.org/wiki/File%3AMapadefrancia.svg

Slide 63

Slide 63 text

UTC +3 Mayotte Europa Island https://commons.wikimedia.org/wiki/File%3AMapadefrancia.svg UTC +4 Crozet Islands Reunion Glorious Islands Tromelin Island Juan de Nova UTC +5 St. Paul and Amsterdam Kerguelen Islands UTC +10 Adelie Land UTC +10 New Caledonia UTC +12 Wallis and Futuna

Slide 64

Slide 64 text

How to make the platform fullfil the official regulations?

Slide 65

Slide 65 text

{% if not app.request.attributes.get('_campaign_expired') and not user_is_adherent %} Connexion Inscription {% endif %} Disabling links in Twig

Slide 66

Slide 66 text

class ArticleController extends Controller { /** * @Route( * "/articles/{category}/{page}", * requirements={"category"="\w+", "page"="\d+"}, * defaults={ "_enable_campaign_silence": true } * ) */ public function actualitesAction(...): Response { ... } } Marking a mutable action

Slide 67

Slide 67 text

class CampaignSilenceSubscriber implements EventSubscriberInterface { // ... public function onKernelController(FilterControllerEvent $event) { // ... $expired = $this->processor->isCampaignExpired($request); $request->attributes->set('_campaign_expired', $expired); if (!$expired || $request->attributes->get('_enable_campaign_silence', false)) { return; } $event->setController(function () { return new Response($this->twig->render('campaign_silent.html.twig')); }); } }

Slide 68

Slide 68 text

# app/config/config_prod.yml services: app.mailjet.transport.transactional: class: AppBundle\Mailjet\Transport\MailjetNullTransport arguments: ['@?logger'] public: false app.mailjet.transport.campaign: class: AppBundle\Mailjet\Transport\MailjetNullTransport arguments: ['@?logger'] public: false

Slide 69

Slide 69 text

April 23rd 1st Ballot

Slide 70

Slide 70 text

~500,000 connections to the website.

Slide 71

Slide 71 text

May 2017

Slide 72

Slide 72 text

# ... http { # ... server { # ... # Block WP Pingback DDoS attacks if ($http_user_agent ~* "WordPress") { return 403; } # ... } } WordPress pingback DDoS attacks

Slide 73

Slide 73 text

class AssetsController extends Controller { /** * @Route("/assets/{path}") * @Method("GET") * @Cache(maxage=900, smaxage=900) */ public function assetAction(string $path, ...) { // ... } } Http caching for static assets

Slide 74

Slide 74 text

May 7th 2nd Ballot

Slide 75

Slide 75 text

~650,000 connections to the website.

Slide 76

Slide 76 text

Wrap Up

Slide 77

Slide 77 text

Thank you! https://www.flickr.com/photos/enmarchefr/31534490113/ @titouangalopin @hhamon @sensiolabs