52 - Avoiding Service Classes with Method Injection

This commit is contained in:
Adam Wathan
2016-12-16 08:31:01 -05:00
parent cf2444da45
commit 990f873e0c
4 changed files with 8 additions and 36 deletions

View File

@@ -30,17 +30,9 @@ class ConcertOrdersController extends Controller
]); ]);
try { try {
// Find some tickets
$reservation = $concert->reserveTickets(request('ticket_quantity'), request('email')); $reservation = $concert->reserveTickets(request('ticket_quantity'), request('email'));
$order = $reservation->complete($this->paymentGateway, request('payment_token'));
// Charge the customer for the tickets
$this->paymentGateway->charge($reservation->totalCost(), request('payment_token'));
// Create an order for those tickets
$order = $reservation->complete();
return response()->json($order, 201); return response()->json($order, 201);
} catch (PaymentFailedException $e) { } catch (PaymentFailedException $e) {
$reservation->cancel(); $reservation->cancel();
return response()->json([], 422); return response()->json([], 422);

View File

@@ -28,8 +28,10 @@ class Reservation
return $this->email; return $this->email;
} }
public function complete() public function complete($paymentGateway, $paymentToken)
{ {
$paymentGateway->charge($this->totalCost(), $paymentToken);
return Order::forTickets($this->tickets(), $this->email(), $this->totalCost()); return Order::forTickets($this->tickets(), $this->email(), $this->totalCost());
} }

View File

@@ -1,25 +0,0 @@
<?php
namespace App;
use App\Order;
use App\Billing\PaymentGateway;
class TicketPurchasingService
{
private $paymentGateway;
public function __construct(PaymentGateway $paymentGateway)
{
$this->paymentGateway = $paymentGateway;
}
public function purchaseTickets($concert, $ticketQuantity, $email, $paymentToken)
{
$reservation = $concert->reserveTickets($ticketQuantity, $email);
$this->paymentGateway->charge($reservation->totalCost(), $paymentToken);
return Order::forTickets($reservation->tickets(), $reservation->email(), $reservation->totalCost());
}
}

View File

@@ -3,6 +3,7 @@
use App\Ticket; use App\Ticket;
use App\Concert; use App\Concert;
use App\Reservation; use App\Reservation;
use App\Billing\FakePaymentGateway;
use Illuminate\Foundation\Testing\WithoutMiddleware; use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations; use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions; use Illuminate\Foundation\Testing\DatabaseTransactions;
@@ -71,11 +72,13 @@ class ReservationTest extends TestCase
$concert = factory(Concert::class)->create(['ticket_price' => 1200]); $concert = factory(Concert::class)->create(['ticket_price' => 1200]);
$tickets = factory(Ticket::class, 3)->create(['concert_id' => $concert->id]); $tickets = factory(Ticket::class, 3)->create(['concert_id' => $concert->id]);
$reservation = new Reservation($tickets, 'john@example.com'); $reservation = new Reservation($tickets, 'john@example.com');
$paymentGateway = new FakePaymentGateway;
$order = $reservation->complete(); $order = $reservation->complete($paymentGateway, $paymentGateway->getValidTestToken());
$this->assertEquals('john@example.com', $order->email); $this->assertEquals('john@example.com', $order->email);
$this->assertEquals(3, $order->ticketQuantity()); $this->assertEquals(3, $order->ticketQuantity());
$this->assertEquals(3600, $order->amount); $this->assertEquals(3600, $order->amount);
$this->assertEquals(3600, $paymentGateway->totalCharges());
} }
} }