Skip to content

Commit

Permalink
Merge pull request #1898 from cultuurnet/III-6310-use-tariffs-collection
Browse files Browse the repository at this point in the history
III-6310 Use `Tariffs` collection inside legacy `PriceInfo`
  • Loading branch information
LucWollants authored Nov 25, 2024
2 parents 718b990 + 879ab6b commit 643f624
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 53 deletions.
3 changes: 2 additions & 1 deletion src/Cdb/CdbXmlPriceInfoParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use CultureFeed_Cdb_Data_Price;
use CultuurNet\UDB3\Model\ValueObject\Price\Tariff;
use CultuurNet\UDB3\Model\ValueObject\Price\TariffName;
use CultuurNet\UDB3\Model\ValueObject\Price\Tariffs;
use CultuurNet\UDB3\Model\ValueObject\Price\TranslatedTariffName;
use CultuurNet\UDB3\Model\ValueObject\Translation\Language;
use CultuurNet\UDB3\MoneyFactory;
Expand Down Expand Up @@ -118,6 +119,6 @@ function (\CultureFeed_Cdb_Data_Detail $detail) use ($mainLanguage) {
}
}

return (new PriceInfo($basePrice))->withTariffs($tariffs);
return (new PriceInfo($basePrice))->withTariffs(new Tariffs(...$tariffs));
}
}
6 changes: 1 addition & 5 deletions src/Event/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -503,11 +503,7 @@ public function updateUiTPASPrices(Tariffs $tariffs): void
return;
}

$legacyUiTPASTariffs = [];
foreach ($tariffs as $tariff) {
$legacyUiTPASTariffs[] = $tariff;
}
$newPriceInfo = $this->priceInfo->withUiTPASTariffs($legacyUiTPASTariffs);
$newPriceInfo = $this->priceInfo->withUiTPASTariffs($tariffs);

if ($this->priceInfo->serialize() !== $newPriceInfo->serialize()) {
$this->apply(new PriceInfoUpdated($this->eventId, $newPriceInfo));
Expand Down
30 changes: 11 additions & 19 deletions src/PriceInfo/PriceInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use CultuurNet\UDB3\Model\Serializer\ValueObject\Price\TariffNormalizer;
use CultuurNet\UDB3\Model\ValueObject\Price\PriceInfo as Udb3ModelPriceInfo;
use CultuurNet\UDB3\Model\ValueObject\Price\Tariff;
use CultuurNet\UDB3\Model\ValueObject\Price\Tariffs;

/**
* @deprecated
Expand All @@ -18,31 +19,25 @@ class PriceInfo implements Serializable
{
private Tariff $basePrice;

/**
* @var Tariff[]
*/
private array $tariffs;
private Tariffs $tariffs;

/**
* @var Tariff[]
*/
private array $uitpasTariffs;
private Tariffs $uitpasTariffs;

public function __construct(Tariff $basePrice)
{
$this->basePrice = $basePrice;
$this->tariffs = [];
$this->uitpasTariffs = [];
$this->tariffs = new Tariffs();
$this->uitpasTariffs = new Tariffs();
}

public function withExtraTariff(Tariff $tariff): PriceInfo
{
$c = clone $this;
$c->tariffs[] = $tariff;
$c->tariffs = $this->tariffs->with($tariff);
return $c;
}

public function withTariffs(array $tariffs): PriceInfo
public function withTariffs(Tariffs $tariffs): PriceInfo
{
$c = clone $this;
$c->tariffs = $tariffs;
Expand All @@ -52,11 +47,11 @@ public function withTariffs(array $tariffs): PriceInfo
public function withExtraUiTPASTariff(Tariff $tariff): PriceInfo
{
$c = clone $this;
$c->uitpasTariffs[] = $tariff;
$c->uitpasTariffs = $this->uitpasTariffs->with($tariff);
return $c;
}

public function withUiTPASTariffs(array $tariffs): PriceInfo
public function withUiTPASTariffs(Tariffs $tariffs): PriceInfo
{
$c = clone $this;
$c->uitpasTariffs = $tariffs;
Expand All @@ -68,15 +63,12 @@ public function getBasePrice(): Tariff
return $this->basePrice;
}

/**
* @return Tariff[]
*/
public function getTariffs(): array
public function getTariffs(): Tariffs
{
return $this->tariffs;
}

public function getUiTPASTariffs(): array
public function getUiTPASTariffs(): Tariffs
{
return $this->uitpasTariffs;
}
Expand Down
52 changes: 26 additions & 26 deletions tests/Event/EventTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -655,7 +655,7 @@ public function it_keeps_existing_uitpas_prices_on_price_info_update(): void
new PriceInfoUpdated(
'd2b41f1d-598c-46af-a3a5-10e373faa6fe',
(new PriceInfo(Tariff::createBasePrice(new Money(100, new Currency('EUR')))))
->withUiTPASTariffs([
->withUiTPASTariffs(new Tariffs(
new Tariff(
new TranslatedTariffName(
new Language('nl'),
Expand All @@ -666,7 +666,7 @@ public function it_keeps_existing_uitpas_prices_on_price_info_update(): void
new Currency('EUR')
)
),
])
))
),
])
->when(
Expand All @@ -685,7 +685,7 @@ public function it_keeps_existing_uitpas_prices_on_price_info_update(): void
new PriceInfoUpdated(
'd2b41f1d-598c-46af-a3a5-10e373faa6fe',
(new PriceInfo(Tariff::createBasePrice(new Money(90, new Currency('EUR')))))
->withUiTPASTariffs([
->withUiTPASTariffs(new Tariffs(
new Tariff(
new TranslatedTariffName(
new Language('nl'),
Expand All @@ -696,7 +696,7 @@ public function it_keeps_existing_uitpas_prices_on_price_info_update(): void
new Currency('EUR')
)
),
])
))
),
]);
}
Expand All @@ -723,7 +723,7 @@ public function it_ignores_an_update_of_uitpas_prices(): void
new PriceInfoUpdated(
'd2b41f1d-598c-46af-a3a5-10e373faa6fe',
(new PriceInfo(Tariff::createBasePrice(new Money(100, new Currency('EUR')))))
->withUiTPASTariffs([
->withUiTPASTariffs(new Tariffs(
new Tariff(
new TranslatedTariffName(
new Language('nl'),
Expand All @@ -734,7 +734,7 @@ public function it_ignores_an_update_of_uitpas_prices(): void
new Currency('EUR')
)
),
])
))
),
])
->when(
Expand All @@ -746,7 +746,7 @@ public function it_ignores_an_update_of_uitpas_prices(): void
new Currency('EUR')
)
)
))->withUiTPASTariffs([
))->withUiTPASTariffs(new Tariffs(
new Tariff(
new TranslatedTariffName(
new Language('nl'),
Expand All @@ -757,14 +757,14 @@ public function it_ignores_an_update_of_uitpas_prices(): void
new Currency('EUR')
)
),
])
))
)
)
->then([
new PriceInfoUpdated(
'd2b41f1d-598c-46af-a3a5-10e373faa6fe',
(new PriceInfo(Tariff::createBasePrice(new Money(90, new Currency('EUR')))))
->withUiTPASTariffs([
->withUiTPASTariffs(new Tariffs(
new Tariff(
new TranslatedTariffName(
new Language('nl'),
Expand All @@ -775,7 +775,7 @@ public function it_ignores_an_update_of_uitpas_prices(): void
new Currency('EUR')
)
),
])
))
),
]);
}
Expand All @@ -802,7 +802,7 @@ public function it_ignores_an_update_with_equal_prices_without_uitpas(): void
new PriceInfoUpdated(
'd2b41f1d-598c-46af-a3a5-10e373faa6fe',
(new PriceInfo(Tariff::createBasePrice(new Money(100, new Currency('EUR')))))
->withUiTPASTariffs([
->withUiTPASTariffs(new Tariffs(
new Tariff(
new TranslatedTariffName(
new Language('nl'),
Expand All @@ -813,7 +813,7 @@ public function it_ignores_an_update_with_equal_prices_without_uitpas(): void
new Currency('EUR')
)
),
])
))
),
])
->when(
Expand Down Expand Up @@ -853,7 +853,7 @@ public function it_ignores_an_update_with_only_different_uitpas_prices(): void
new PriceInfoUpdated(
'd2b41f1d-598c-46af-a3a5-10e373faa6fe',
(new PriceInfo(Tariff::createBasePrice(new Money(100, new Currency('EUR')))))
->withUiTPASTariffs([
->withUiTPASTariffs(new Tariffs(
new Tariff(
new TranslatedTariffName(
new Language('nl'),
Expand All @@ -864,7 +864,7 @@ public function it_ignores_an_update_with_only_different_uitpas_prices(): void
new Currency('EUR')
)
),
])
))
),
])
->when(
Expand All @@ -877,7 +877,7 @@ public function it_ignores_an_update_with_only_different_uitpas_prices(): void
)
)
)
)->withUiTPASTariffs([
)->withUiTPASTariffs(new Tariffs(
new Tariff(
new TranslatedTariffName(
new Language('nl'),
Expand All @@ -888,7 +888,7 @@ public function it_ignores_an_update_with_only_different_uitpas_prices(): void
new Currency('EUR')
)
),
])
))
)
)
->then([]);
Expand Down Expand Up @@ -945,44 +945,44 @@ public function it_handles_update_price_info_from_udb2_update(): void
->when(
fn (Event $event) => $event->updatePriceInfo(
(new PriceInfo((Tariff::createBasePrice(new Money(1250, new Currency('EUR'))))))
->withTariffs([
->withTariffs(new Tariffs(
new Tariff(
new TranslatedTariffName(
new Language('nl'),
new TariffName('Met kinderen')
),
new Money(2000, new Currency('EUR'))
),
])
))
)
)
->when(
fn (Event $event) => $event->updatePriceInfo(
(new PriceInfo((Tariff::createBasePrice(new Money(1250, new Currency('EUR'))))))
->withTariffs([
->withTariffs(new Tariffs(
new Tariff(
new TranslatedTariffName(
new Language('nl'),
new TariffName('Met kinderen')
),
new Money(1499, new Currency('EUR'))
),
])
))
)
)
->then([
new PriceInfoUpdated(
$eventId,
(new PriceInfo(Tariff::createBasePrice(new Money(1250, new Currency('EUR')))))
->withTariffs([
->withTariffs(new Tariffs(
new Tariff(
new TranslatedTariffName(
new Language('nl'),
new TariffName('Met kinderen')
),
new Money(1499, new Currency('EUR'))
),
])
))
),
]);
}
Expand Down Expand Up @@ -1920,7 +1920,7 @@ public function it_ignores_equal_uitpas_prices(): void
new PriceInfoUpdated(
'd2b41f1d-598c-46af-a3a5-10e373faa6fe',
(new PriceInfo(Tariff::createBasePrice(new Money(100, new Currency('EUR')))))
->withUiTPASTariffs([
->withUiTPASTariffs(new Tariffs(
new Tariff(
new TranslatedTariffName(
new Language('nl'),
Expand All @@ -1941,7 +1941,7 @@ public function it_ignores_equal_uitpas_prices(): void
new Currency('EUR')
)
),
])
))
),
])
->when(
Expand Down Expand Up @@ -2021,7 +2021,7 @@ function (Event $event): void {
new PriceInfoUpdated(
'd2b41f1d-598c-46af-a3a5-10e373faa6fe',
(new PriceInfo(Tariff::createBasePrice(new Money(100, new Currency('EUR')))))
->withUiTPASTariffs([
->withUiTPASTariffs(new Tariffs(
new Tariff(
new TranslatedTariffName(
new Language('nl'),
Expand All @@ -2042,7 +2042,7 @@ function (Event $event): void {
new Currency('EUR')
)
),
])
)),
),
]);
}
Expand Down
4 changes: 2 additions & 2 deletions tests/PriceInfo/PriceInfoTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,15 @@ public function it_returns_the_base_price(): void
*/
public function it_returns_any_extra_tariffs(): void
{
$this->assertEquals($this->tariffs, $this->priceInfo->getTariffs());
$this->assertEquals(new Tariffs(...$this->tariffs), $this->priceInfo->getTariffs());
}

/**
* @test
*/
public function it_returns_any_extra_uitpas_tariffs(): void
{
$this->assertEquals($this->uitpasTariffs, $this->priceInfo->getUiTPASTariffs());
$this->assertEquals(new Tariffs(...$this->uitpasTariffs), $this->priceInfo->getUiTPASTariffs());
}

/**
Expand Down

0 comments on commit 643f624

Please sign in to comment.