Slide 1

Slide 1 text

No content

Slide 2

Slide 2 text

No content

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

No content

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

No content

Slide 9

Slide 9 text

require_once __DIR__.'/../vendor/autoload.php'; $app = new Silex\Application(); $app->get('/hello/{name}', function ($name) use ($app) { return 'Hello '.$app->escape($name); }); $app->run();

Slide 10

Slide 10 text

$app['some_service'] = $app->share( function ($app) { return new SomeService($app['other_service']); } );

Slide 11

Slide 11 text

No content

Slide 12

Slide 12 text

No content

Slide 13

Slide 13 text

No content

Slide 14

Slide 14 text

Scenario: Place bid on a running auction Given there is an auction for some "Glasses" And I am a registered user And I am on "/" When I follow "Login" And I fill in "email" with "[email protected]" And I fill in "password" with "password" And I press "Login" And I follow "Glasses" And I fill in "amount" with "10.00" And I select "USD" from "currency" And I press "Place Bid" Then I should see "Bid Accepted"

Slide 15

Slide 15 text

No content

Slide 16

Slide 16 text

Scenario: Place bid on a running auction Given there is a running auction And I am viewing the auction When I place a bid on the auction Then I should see my bid is accepted

Slide 17

Slide 17 text

No content

Slide 18

Slide 18 text

No content

Slide 19

Slide 19 text

No content

Slide 20

Slide 20 text

No content

Slide 21

Slide 21 text

class BidRequest { public $auctionId; public $userId; public $amount; public function __construct($auctionId, $userId, Money $amount) { $this->auctionId = $auctionId; $this->userId = $userId; $this->amount = $amount; } }

Slide 22

Slide 22 text

class BidResponse { public $bid; public function __construct(BidValue $bid) { $this->bid = $bid; } }

Slide 23

Slide 23 text

$interactor = new BidInteractor(); $request = new BidRequest( $auctionId, $userId, new Money($amount) ); $response = $interactor($request);

Slide 24

Slide 24 text

No content

Slide 25

Slide 25 text

No content

Slide 26

Slide 26 text

namespace Douche\Interactor; class Bid { public function __invoke(BidRequest $request) { $auction = $this->auctionRepo->find($request->auctionId); $user = $this->userRepo->find($request->userId); $converted = $this->converter->convert( $request->amount, $auction->getCurrency() ); $bid = new BidValue($converted, $request->amount); $auction->bid($user, $bid); return new BidResponse($bid); } }

Slide 27

Slide 27 text

No content

Slide 28

Slide 28 text

class Auction { public function getId(); public function getName(); public function getCurrency(); public function getEndingAt(); public function getHighestBid(); public function getHighestBidder(); public function isRunning(DateTime $now = null); public function bid(User $bidder, Bid $bid, DateTime $now = null); }

Slide 29

Slide 29 text

public function bid(User $bidder, Bid $bid, DateTime $now = null) { if (!$this->isRunning($now)) { throw new AuctionClosedException(); } $highestBid = $this->getHighestBid(); if ($highestBid && $bid->getAmount() <= $highestBid->getAmount()) { throw new BidTooLowException(); } $this->bids[] = [$bidder, $bid]; }

Slide 30

Slide 30 text

No content

Slide 31

Slide 31 text

No content

Slide 32

Slide 32 text

No content

Slide 33

Slide 33 text

No content

Slide 34

Slide 34 text

No content

Slide 35

Slide 35 text

$app->get('/auction/{id}', ...) ->convert('request', function ($_, Request $req) { return new AuctionViewRequest( $req->attributes->get('id') ); });

Slide 36

Slide 36 text

$app->get('/auction/{id}', 'interactor.auction_view')

Slide 37

Slide 37 text

public function getController(Request $req) { $controller = $req->attributes->get('_controller'); if (!is_string($controller) || !isset($this->container[$controller])) { return $this->resolver->getController($req); } if (!is_callable($this->container[$controller])) { throw new \InvalidArgumentException("..."); } return $this->container[$controller]; }

Slide 38

Slide 38 text

$app['resolver'] = $app->share( $app->extend('resolver', function ($resolver, $app) { $resolver = new ControllerResolver( $resolver, $app ); return $resolver; }) );

Slide 39

Slide 39 text

$dispatcher->addListener(KernelEvents::VIEW, function ($event) use ($app) { $view = $event->getControllerResult(); $request = $event->getRequest(); $controller = $request->attributes->get('controller'); $template = "$controller.html"; $body = $app['mustache']->render($template, $view); $response = new Response($body); $event->setResponse($response); });

Slide 40

Slide 40 text

No content

Slide 41

Slide 41 text

{{> _header.html }}

{{ auction.name }}

Ends: {{ auction.endingAt | format_date }}

{{# auction.highestBid }}

Highest bid: {{ getAmount | format_money }}

{{/ auction.highestBid }} {{# auction.highestBidder }}

Highest bidder: {{ . }}

{{/ auction.highestBidder }}

Slide 42

Slide 42 text

$app->get('/auction/{id}', 'interactor.auction_view') ->value('success_handler', function ($view, $req) { return new RedirectResponse( "/auction/" . $request->attributes->get("id") ); });

Slide 43

Slide 43 text

if ($request->attributes->has('success_handler')) { $fn = $request->attributes->get('success_handler'); $view = $fn($view, $request); if ($view instanceof Response) { $event->setResponse($view); return; } }

Slide 44

Slide 44 text

$app->post('/login', 'interactor.user_login') ->value('error_handlers', [ "IncorrectPasswordException" => function () { return ['errors' => [ 'Invalid Credentials', ]]; }, ]);

Slide 45

Slide 45 text

$app->error(function (DoucheException $e, $code) use ($app) { $handlers = $app['request'] ->attributes ->get('error_handlers', []); foreach ($handlers as $type => $handler) { if ($e instanceof $type) { return $handler( $e, $code, $app['request'] ); } } });

Slide 46

Slide 46 text

namespace DoucheWeb; use Douche\Interactor\AuctionListResponse; use Douche\Interactor\AuctionViewRequest; use Douche\Interactor\UserLoginRequest; use Douche\Interactor\UserLoginResponse; use Douche\Interactor\AuctionViewResponse; use Douche\Interactor\BidRequest; use Douche\Exception\Exception as DoucheException; use Mustache\Silex\Provider\MustacheServiceProvider; use Silex\Application; use Silex\Provider\DoctrineServiceProvider; use Silex\Provider\MonologServiceProvider; use Silex\Provider\ServiceControllerServiceProvider; use Silex\Provider\SessionServiceProvider; use Silex\ExceptionListenerWrapper; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\RedirectResponse; use Symfony\Component\HttpKernel\KernelEvents; use Money\Money; use Money\Currency; $app = new Application(); $app->register(new MonologServiceProvider()); $app->register(new DoctrineServiceProvider()); $app->register(new MustacheServiceProvider(), [ 'mustache.options' => [ 'helpers' => [ 'format_money' => function ($money) { return $money->getCurrency().' '.($money->getAmount() / 100); }, 'format_date' => function (\DateTime $date) { return $date->format("Y-m-d H:i:s"); }, ], ], ]); $app->register(new ServiceControllerServiceProvider()); $app->register(new SessionServiceProvider()); $app->register(new ServiceProvider()); $app->get('/', 'interactor.auction_list') ->value('controller', 'auction_list'); $app->get('/auction/{id}', 'interactor.auction_view') ->value('controller', 'auction_view') ->convert('request', function ($_, Request $request) { return new AuctionViewRequest($request->attributes->get('id')); }); $app->post('/auction/{id}/bids', 'interactor.bid') ->before(function (Request $request, Application $app) { if (!$request->getSession()->has('current_user')) { return $app->abort(401, 'Authenitcation Required'); } }) ->value('controller', 'bid') ->value('success_handler', function ($view, $request) { return new RedirectResponse("/auction/" . $request->attributes->get('id')); }) ->value('error_handlers', [ "Douche\Exception\BidTooLowException" => function ($e, $code, $request) { $request->getSession()->getFlashBag()->set('errors', [ 'The provided bid was too low.', ]); return new RedirectResponse("/auction/" . $request->attributes->get('id')); }, ]) ->convert('request', function ($_, Request $request) { return new BidRequest( $request->attributes->get('id'), $request->getSession()->get('current_user')->id, new Money((int) $request->request->get('amount') * 100, new Currency($request->request->get('currency'))) ); }); $app->post('/login', 'interactor.user_login') ->value('controller', 'login') ->value('success_handler', function ($view, $request) { $request->getSession()->set('current_user', $view->user); return new RedirectResponse("/"); }) ->value('error_handlers', [ "Douche\Exception\UserNotFoundException" => function ($e) { return [ 'errors' => ['Incorrect email provided.'], 'email' => $e->email, ]; }, "Douche\Exception\IncorrectPasswordException" => function ($e) { return [ 'errors' => ['Invalid credentials provided.'], 'email' => $e->email, ]; }, ]) ->convert('request', function ($_, Request $request) { return new UserLoginRequest($request->request->all()); }); $app->get('/login', function(Request $request, Application $app) { $view = [ 'errors' => [], ]; return $app['mustache']->render('login.html.mustache', $view); }); $app->get('/logout', function(Request $request, Application $app) { $request->getSession()->start(); $request->getSession()->invalidate(); return $app->redirect("/"); }); $app['resolver'] = $app->share($app->extend('resolver', function ($resolver, $app) { $resolver = new ControllerResolver($resolver, $app); return $resolver; })); // TODO change to ->error once fabpot/silex#705 is merged $app['dispatcher'] = $app->share($app->extend('dispatcher', function ($dispatcher, $app) { $dispatcher->addListener(KernelEvents::EXCEPTION, new ExceptionListenerWrapper($app, function (DoucheException $e, $code) use ($app) { $app['request']->attributes->set('failed', true); $errorHandlers = $app['request']->attributes->get('error_handlers', []); foreach ($errorHandlers as $type => $handler) { if ($e instanceof $type) { return $handler($e, $code, $app['request']); } } }), -8); return $dispatcher; })); // TODO change to ->on once fabpot/silex#705 is merged $app['dispatcher'] = $app->share($app->extend('dispatcher', function ($dispatcher, $app) { $dispatcher->addListener(KernelEvents::VIEW, function ($event) use ($app) { $view = $event->getControllerResult(); if (is_null($view) || is_string($view)) { return; } $request = $event->getRequest(); if (!$request->attributes->get('failed') && $request->attributes->has('success_handler')) { $handler = $request->attributes->get('success_handler'); $view = $handler($view, $request); if ($view instanceof Response) { $event->setResponse($view); return; } } $controller = $request->attributes->get('controller'); $template = "$controller.html"; $view = (object) $view; $view->current_user = $request->getSession()->get('current_user'); $view->form_errors = $request->getSession()->getFlashBag()->get('errors'); $body = $app['mustache']->render($template, $view); $response = new Response($body); $event->setResponse($response); }); return $dispatcher; })); // TODO change to ->after once fabpot/silex#705 is merged $app['dispatcher'] = $app->share($app->extend('dispatcher', function ($dispatcher, $app) { $dispatcher->addListener(KernelEvents::RESPONSE, function () use ($app) { $app['douche.auction_repo']->save(); }); return $dispatcher; })); return $app; service providers routes listeners

Slide 47

Slide 47 text

No content

Slide 48

Slide 48 text

No content

Slide 49

Slide 49 text

/** * @When /^I place a bid on the auction$/ */ public function iPlaceABidOnTheAuction() { $this->auctionHelper->placeBid(1.0); }

Slide 50

Slide 50 text

public function placeBid($amount) { $interactor = new BidInteractor( $this->getAuctionRepository() ); $request = new BidRequest( $this->auction->getId(), $this->getCurrentUserId(), $amount ); $this->response = $interactor($request); }

Slide 51

Slide 51 text

public function placeBid($amount) { $page = $this->mink->getSession()->getPage(); $page->fillField('amount', $amount); $page->pressButton("Place Bid"); }

Slide 52

Slide 52 text

No content

Slide 53

Slide 53 text

No content

Slide 54

Slide 54 text

No content

Slide 55

Slide 55 text

No content

Slide 56

Slide 56 text

No content