Skip to content

Commit

Permalink
Add NotUniqueCantonException (#50)
Browse files Browse the repository at this point in the history
  • Loading branch information
kira0269 authored May 27, 2024
1 parent 517d02f commit a83b2a5
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 16 deletions.
18 changes: 10 additions & 8 deletions src/CantonManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Wnx\SwissCantons;

use Wnx\SwissCantons\Exceptions\CantonNotFoundException;
use Wnx\SwissCantons\Exceptions\NotUniqueCantonException;

class CantonManager
{
Expand Down Expand Up @@ -81,29 +82,30 @@ public function getByZipcode(int $zipcode): array
* @param int $zipcode
* @param ?string $cityName
* @return Canton
* @throws CantonNotFoundException
* @throws CantonNotFoundException|NotUniqueCantonException
*/
public function getByZipcodeAndCity(int $zipcode, ?string $cityName = null): Canton
{
$cities = $this->citySearch->findByZipcode($zipcode);
$cantons = $this->getByZipcode($zipcode);

if (1 === count($cities)) {
return $this->search->findByAbbreviation($cities[0]['canton'])
?? throw CantonNotFoundException::notFoundForZipcode($zipcode);
if (1 === count($cantons)) {
return $cantons[0];
}

if (null !== $cityName) {
$cities = $this->citySearch->findByZipcode($zipcode);

foreach ($cities as $city) {
if ($city['city'] === $cityName) {
return $this->search->findByAbbreviation($city['canton'])
?? throw CantonNotFoundException::notFoundForZipcodeAndCity($zipcode, $cityName);
?? throw CantonNotFoundException::notFoundForAbbreviation($city['canton']);
}
}

throw CantonNotFoundException::notFoundForZipcodeAndCity($zipcode, $cityName);
throw NotUniqueCantonException::notUniqueForZipcodeAndCity($zipcode, $cityName);
}

throw CantonNotFoundException::notFoundForZipcode($zipcode);
throw NotUniqueCantonException::notUniqueForZipcode($zipcode);
}

}
6 changes: 0 additions & 6 deletions src/Exceptions/CantonNotFoundException.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,4 @@ public static function notFoundForZipcode(int $zipcode): self
/** @phpstan-ignore-next-line */
return new static("Couldn't find Canton for given zipcode: {$zipcode}");
}

public static function notFoundForZipcodeAndCity(int $zipcode, string $city): self
{
/** @phpstan-ignore-next-line */
return new static("Couldn't find Canton for given zipcode and city name: {$zipcode}, {$city}");
}
}
20 changes: 20 additions & 0 deletions src/Exceptions/NotUniqueCantonException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php declare(strict_types=1);

namespace Wnx\SwissCantons\Exceptions;

use Exception;

class NotUniqueCantonException extends Exception
{
public static function notUniqueForZipcode(int $zipcode): self
{
/** @phpstan-ignore-next-line */
return new static("Couldn't find an unique Canton for given zipcode: {$zipcode}");
}

public static function notUniqueForZipcodeAndCity(int $zipcode, string $city): self
{
/** @phpstan-ignore-next-line */
return new static("Couldn't find an unique Canton for given zipcode and city name: {$zipcode}, {$city}");
}
}
25 changes: 23 additions & 2 deletions tests/CantonManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Wnx\SwissCantons\Canton;
use Wnx\SwissCantons\CantonManager;
use Wnx\SwissCantons\Exceptions\CantonNotFoundException;
use Wnx\SwissCantons\Exceptions\NotUniqueCantonException;

class CantonManagerTest extends TestCase
{
Expand Down Expand Up @@ -132,7 +133,7 @@ public function it_finds_single_canton_with_zipcode_and_city(): void
}

#[Test]
public function it_finds_single_canton_with_zipcode(): void
public function it_finds_single_canton_with_zipcode_for_one_city(): void
{
$cantonManager = new CantonManager();

Expand All @@ -141,6 +142,16 @@ public function it_finds_single_canton_with_zipcode(): void
$this->assertEquals('VD', $canton->getAbbreviation());
}

#[Test]
public function it_finds_single_canton_with_zipcode_for_several_cities_in_same_canton(): void
{
$cantonManager = new CantonManager();

$canton = $cantonManager->getByZipcodeAndCity(8914);

$this->assertEquals('ZH', $canton->getAbbreviation());
}

#[Test]
public function it_throws_exception_if_no_single_canton_for_zipcode_could_be_found(): void
{
Expand All @@ -154,10 +165,20 @@ public function it_throws_exception_if_no_single_canton_for_zipcode_could_be_fou
#[Test]
public function it_throws_exception_if_no_canton_for_zipcode_and_city_could_be_found(): void
{
$this->expectException(CantonNotFoundException::class);
$this->expectException(NotUniqueCantonException::class);

$canton = new CantonManager();

$canton->getByZipcodeAndCity(1290, 'Lausanne');
}

#[Test]
public function it_throws_exception_if_no_unique_canton_for_zipcode(): void
{
$this->expectException(NotUniqueCantonException::class);

$canton = new CantonManager();

$canton->getByZipcodeAndCity(1290);
}
}

0 comments on commit a83b2a5

Please sign in to comment.