Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/Command/CalculateQuotePrice.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ class CalculateQuotePrice

public ?\DateTime $arrivalDate;

public ?int $guestCount = null;

public ?int $nightsCount = null;

public function __construct()
Expand Down
6 changes: 5 additions & 1 deletion src/CommandHandler/CalculateQuotePriceHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ public function __construct(QuotePriceCalculator $priceCalculator)

public function __invoke(CalculateQuotePrice $command): void
{
$command->amount = $this->priceCalculator->calculate($command->arrivalDate, $command->nightsCount);
$command->amount = $this->priceCalculator->calculate(
$command->arrivalDate,
$command->nightsCount,
$command->guestCount
);
}
}
1 change: 1 addition & 0 deletions src/Controller/QuotesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public function index(Request $request): Response
'amount' => $command->amount,
'nights_count' => $command->nightsCount,
'arrival_date' => $command->arrivalDate,
'guest_count' => $command->guestCount,
]
);
}
Expand Down
6 changes: 6 additions & 0 deletions src/Form/CalculateQuotePriceForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
[
'label' => 'nights_count',
]
)->add(
'guestCount',
IntegerType::class,
[
'label' => 'guest_count',
]
)->add(
'submit',
SubmitType::class,
Expand Down
4 changes: 2 additions & 2 deletions src/Service/QuotePriceCalculator.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public function __construct(PriceListFactory $priceListFactory)
$this->priceListFactory = $priceListFactory;
}

public function calculate(\DateTime $arrivalDate, int $nightsCount): float
public function calculate(\DateTime $arrivalDate, int $nightsCount, int $guestCount): float
{
if ($nightsCount <= 0) {
throw new \Exception();
Expand All @@ -23,7 +23,7 @@ public function calculate(\DateTime $arrivalDate, int $nightsCount): float
$pricePerNight = $priceList->getPricePerNight($arrivalDate);
$discountPercentage = $this->getLongStayDiscountPercentage($nightsCount);

return $pricePerNight * $nightsCount * (1 - $discountPercentage);
return $pricePerNight * $nightsCount * $guestCount * (1 - $discountPercentage);
}

private function getLongStayDiscountPercentage(int $nightsCount): float
Expand Down
3 changes: 3 additions & 0 deletions templates/calculatedAmount.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
<li class="list-group-item">
<strong>{{ 'nights_count'|trans }}</strong>: {{ nights_count }}
</li>
<li class="list-group-item">
<strong>{{ 'guest_count'|trans }}</strong>: {{ guest_count }}
</li>
</ul>
</div>
<div class="col-12 mt-3">
Expand Down
7 changes: 5 additions & 2 deletions templates/index.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@
{{ form_start(form) }}

<div class="row">
<div class="col-12 col-md-6">
<div class="col-12 col-md-4">
{{ form_row(form.arrivalDate) }}
</div>
<div class="col-12 col-md-6">
<div class="col-12 col-md-4">
{{ form_row(form.nightsCount) }}
</div>
<div class="col-12 col-md-4">
{{ form_row(form.guestCount) }}
</div>
</div>

<div class="row">
Expand Down
1 change: 1 addition & 0 deletions tests/Functional/QuoteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public function it_calculates_quote_price()
$form->setValues([
'calculate_quote_price_form[arrivalDate]' => '17/11/2021',
'calculate_quote_price_form[nightsCount]' => '2',
'calculate_quote_price_form[guestCount]' => '3',
]);
$client->submit($form);

Expand Down
7 changes: 4 additions & 3 deletions tests/Integration/Service/QuotePriceCalculatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@ public function it_calculates_reservation_price()

$arrivalDate = new \DateTime('2021-06-01'); // In the high season
$nightsCount = 2;
$guestCount = 3;

// Action
$price = $SUT->calculate($arrivalDate, $nightsCount);
$price = $SUT->calculate($arrivalDate, $nightsCount, $guestCount);

// Expectations: 80€ * 2 nights * 1 (no discount)
$this->assertEquals(80 * 2, $price);
// Expectations: 80€ * 2 nights * 3 guests * 1 (no discount)
$this->assertEquals(80 * 2 * 3, $price);
}
}
7 changes: 4 additions & 3 deletions tests/Unit/Service/QuotePriceCalculatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,15 @@ public function it_calculates_reservation_price()

$arrivalDate = new \DateTime('2021-06-01'); // In the high season
$nightsCount = 2;
$guestCount = 3;

// Expectation: price per night will be got from a PriceList object
$priceListFactory->method('create')->willReturn($priceList);

// Action
$price = $SUT->calculate($arrivalDate, $nightsCount);
$price = $SUT->calculate($arrivalDate, $nightsCount, $guestCount);

// Expectations: 80€ * 2 nights * 1 (no discount)
$this->assertEquals(80 * 2, $price);
// Expectations: 80€ * 2 nights * 3 guests * 1 (no discount)
$this->assertEquals(80 * 2 * 3, $price);
}
}
1 change: 1 addition & 0 deletions translations/messages.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ footer_message: Demo project @ 2021
back: Back
your_quote: Your quote
total: Total
guest_count: Guest count
1 change: 1 addition & 0 deletions translations/messages.it.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ back: Indietro
quote_amount: Importo del preventivo
your_quote: Il tuo preventivo
total: Totale
guest_count: Numero di ospiti