mirror of
https://github.com/nothingworksinc/ticketbeast.git
synced 2026-03-04 11:00:04 +00:00
52 - Avoiding Service Classes with Method Injection
This commit is contained in:
@@ -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);
|
||||||
|
|||||||
@@ -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());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -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());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -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());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user