Skip to content

Commit bca0291

Browse files
authored
Merge pull request #52 from programmatordev/OPA-55-refactor-code
Refactor code
2 parents c1073f5 + 11b656f commit bca0291

23 files changed

+177
-208
lines changed

src/Config.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,7 @@ private function resolveOptions(array $options): array
4343
$resolver->setAllowedTypes('cache', ['null', CacheItemPoolInterface::class]);
4444
$resolver->setAllowedTypes('logger', ['null', LoggerInterface::class]);
4545

46-
$resolver->setAllowedValues('applicationKey', function($value) {
47-
return !empty($value);
48-
});
46+
$resolver->setAllowedValues('applicationKey', fn($value) => !empty($value));
4947
$resolver->setAllowedValues('unitSystem', UnitSystem::getList());
5048
$resolver->setAllowedValues('language', Language::getList());
5149

src/Endpoint/AbstractEndpoint.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,9 +123,7 @@ private function handleResponseErrors(ResponseInterface $response): void
123123
private function buildUrl(string $path, array $query): string
124124
{
125125
// Add application key to all requests
126-
$query = $query + [
127-
'appid' => $this->config->getApplicationKey()
128-
];
126+
$query['appid'] = $this->config->getApplicationKey();
129127

130128
return \sprintf('%s%s?%s', $this->config->getBaseUrl(), $path, http_build_query($query));
131129
}

src/Endpoint/AirPollutionEndpoint.php

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,24 @@
33
namespace ProgrammatorDev\OpenWeatherMap\Endpoint;
44

55
use Http\Client\Exception;
6+
use ProgrammatorDev\OpenWeatherMap\Endpoint\Util\ValidationTrait;
67
use ProgrammatorDev\OpenWeatherMap\Entity\AirPollution\AirPollutionLocationList;
78
use ProgrammatorDev\OpenWeatherMap\Entity\AirPollution\AirPollutionLocation;
89
use ProgrammatorDev\OpenWeatherMap\Exception\ApiErrorException;
910
use ProgrammatorDev\YetAnotherPhpValidator\Exception\ValidationException;
10-
use ProgrammatorDev\YetAnotherPhpValidator\Validator;
1111

1212
class AirPollutionEndpoint extends AbstractEndpoint
1313
{
14+
use ValidationTrait;
15+
1416
/**
1517
* @throws Exception
1618
* @throws ApiErrorException
1719
* @throws ValidationException
1820
*/
1921
public function getCurrent(float $latitude, float $longitude): AirPollutionLocation
2022
{
21-
Validator::range(-90, 90)->assert($latitude, 'latitude');
22-
Validator::range(-180, 180)->assert($longitude, 'longitude');
23+
$this->validateCoordinate($latitude, $longitude);
2324

2425
$data = $this->sendRequest(
2526
method: 'GET',
@@ -40,8 +41,7 @@ public function getCurrent(float $latitude, float $longitude): AirPollutionLocat
4041
*/
4142
public function getForecast(float $latitude, float $longitude): AirPollutionLocationList
4243
{
43-
Validator::range(-90, 90)->assert($latitude, 'latitude');
44-
Validator::range(-180, 180)->assert($longitude, 'longitude');
44+
$this->validateCoordinate($latitude, $longitude);
4545

4646
$data = $this->sendRequest(
4747
method: 'GET',
@@ -67,10 +67,8 @@ public function getHistory(
6767
\DateTimeInterface $endDate
6868
): AirPollutionLocationList
6969
{
70-
Validator::range(-90, 90)->assert($latitude, 'latitude');
71-
Validator::range(-180, 180)->assert($longitude, 'longitude');
72-
Validator::lessThan(new \DateTime('now'))->assert($endDate, 'endDate');
73-
Validator::greaterThan($startDate)->assert($endDate, 'endDate');
70+
$this->validateCoordinate($latitude, $longitude);
71+
$this->validateDateRange($startDate, $endDate);
7472

7573
$timezoneUtc = new \DateTimeZone('UTC');
7674

src/Endpoint/GeocodingEndpoint.php

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,17 @@
33
namespace ProgrammatorDev\OpenWeatherMap\Endpoint;
44

55
use Http\Client\Exception;
6+
use ProgrammatorDev\OpenWeatherMap\Endpoint\Util\ValidationTrait;
67
use ProgrammatorDev\OpenWeatherMap\Entity\Geocoding\ZipCodeLocation;
78
use ProgrammatorDev\OpenWeatherMap\Entity\Location;
89
use ProgrammatorDev\OpenWeatherMap\Exception\ApiErrorException;
9-
use ProgrammatorDev\OpenWeatherMap\Util\CreateEntityListTrait;
10+
use ProgrammatorDev\OpenWeatherMap\Util\EntityListTrait;
1011
use ProgrammatorDev\YetAnotherPhpValidator\Exception\ValidationException;
11-
use ProgrammatorDev\YetAnotherPhpValidator\Validator;
1212

1313
class GeocodingEndpoint extends AbstractEndpoint
1414
{
15-
use CreateEntityListTrait;
15+
use ValidationTrait;
16+
use EntityListTrait;
1617

1718
private const NUM_RESULTS = 5;
1819

@@ -26,8 +27,8 @@ class GeocodingEndpoint extends AbstractEndpoint
2627
*/
2728
public function getByLocationName(string $locationName, int $numResults = self::NUM_RESULTS): array
2829
{
29-
Validator::notBlank()->assert($locationName, 'locationName');
30-
Validator::greaterThan(0)->assert($numResults, 'numResults');
30+
$this->validateSearchQuery($locationName, 'locationName');
31+
$this->validateNumResults($numResults);
3132

3233
$data = $this->sendRequest(
3334
method: 'GET',
@@ -38,7 +39,7 @@ public function getByLocationName(string $locationName, int $numResults = self::
3839
]
3940
);
4041

41-
return $this->createEntityList($data, Location::class);
42+
return $this->createEntityList(Location::class, $data);
4243
}
4344

4445
/**
@@ -48,8 +49,8 @@ public function getByLocationName(string $locationName, int $numResults = self::
4849
*/
4950
public function getByZipCode(string $zipCode, string $countryCode): ZipCodeLocation
5051
{
51-
Validator::notBlank()->assert($zipCode, 'zipCode');
52-
Validator::country()->assert($countryCode, 'countryCode');
52+
$this->validateSearchQuery($zipCode, 'zipCode');
53+
$this->validateCountryCode($countryCode);
5354

5455
$data = $this->sendRequest(
5556
method: 'GET',
@@ -70,9 +71,8 @@ public function getByZipCode(string $zipCode, string $countryCode): ZipCodeLocat
7071
*/
7172
public function getByCoordinate(float $latitude, float $longitude, int $numResults = self::NUM_RESULTS): array
7273
{
73-
Validator::range(-90, 90)->assert($latitude, 'latitude');
74-
Validator::range(-180, 180)->assert($longitude, 'longitude');
75-
Validator::greaterThan(0)->assert($numResults, 'numResults');
74+
$this->validateCoordinate($latitude, $longitude);
75+
$this->validateNumResults($numResults);
7676

7777
$data = $this->sendRequest(
7878
method: 'GET',
@@ -84,6 +84,6 @@ public function getByCoordinate(float $latitude, float $longitude, int $numResul
8484
]
8585
);
8686

87-
return $this->createEntityList($data, Location::class);
87+
return $this->createEntityList(Location::class, $data);
8888
}
8989
}

src/Endpoint/OneCallEndpoint.php

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,18 @@
55
use Http\Client\Exception;
66
use ProgrammatorDev\OpenWeatherMap\Endpoint\Util\LanguageTrait;
77
use ProgrammatorDev\OpenWeatherMap\Endpoint\Util\UnitSystemTrait;
8+
use ProgrammatorDev\OpenWeatherMap\Endpoint\Util\ValidationTrait;
89
use ProgrammatorDev\OpenWeatherMap\Entity\OneCall\WeatherAggregate;
910
use ProgrammatorDev\OpenWeatherMap\Entity\OneCall\WeatherLocation;
1011
use ProgrammatorDev\OpenWeatherMap\Entity\OneCall\OneCall;
1112
use ProgrammatorDev\OpenWeatherMap\Exception\ApiErrorException;
1213
use ProgrammatorDev\YetAnotherPhpValidator\Exception\ValidationException;
13-
use ProgrammatorDev\YetAnotherPhpValidator\Validator;
1414

1515
class OneCallEndpoint extends AbstractEndpoint
1616
{
1717
use UnitSystemTrait;
1818
use LanguageTrait;
19+
use ValidationTrait;
1920

2021
/**
2122
* @throws Exception
@@ -24,8 +25,7 @@ class OneCallEndpoint extends AbstractEndpoint
2425
*/
2526
public function getWeather(float $latitude, float $longitude): OneCall
2627
{
27-
Validator::range(-90, 90)->assert($latitude, 'latitude');
28-
Validator::range(-180, 180)->assert($longitude, 'longitude');
28+
$this->validateCoordinate($latitude, $longitude);
2929

3030
$data = $this->sendRequest(
3131
method: 'GET',
@@ -48,9 +48,8 @@ public function getWeather(float $latitude, float $longitude): OneCall
4848
*/
4949
public function getHistoryMoment(float $latitude, float $longitude, \DateTimeInterface $dateTime): WeatherLocation
5050
{
51-
Validator::range(-90, 90)->assert($latitude, 'latitude');
52-
Validator::range(-180, 180)->assert($longitude, 'longitude');
53-
Validator::lessThan(new \DateTime('now'))->assert($dateTime, 'dateTime');
51+
$this->validateCoordinate($latitude, $longitude);
52+
$this->validatePastDate($dateTime, 'dateTime');
5453

5554
$data = $this->sendRequest(
5655
method: 'GET',
@@ -74,9 +73,8 @@ public function getHistoryMoment(float $latitude, float $longitude, \DateTimeInt
7473
*/
7574
public function getHistoryAggregate(float $latitude, float $longitude, \DateTimeInterface $date): WeatherAggregate
7675
{
77-
Validator::range(-90, 90)->assert($latitude, 'latitude');
78-
Validator::range(-180, 180)->assert($longitude, 'longitude');
79-
Validator::lessThan(new \DateTime('now'))->assert($date, 'date');
76+
$this->validateCoordinate($latitude, $longitude);
77+
$this->validatePastDate($date, 'date');
8078

8179
$data = $this->sendRequest(
8280
method: 'GET',
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
namespace ProgrammatorDev\OpenWeatherMap\Endpoint\Util;
4+
5+
use ProgrammatorDev\YetAnotherPhpValidator\Exception\ValidationException;
6+
use ProgrammatorDev\YetAnotherPhpValidator\Validator;
7+
8+
trait ValidationTrait
9+
{
10+
/**
11+
* @throws ValidationException
12+
*/
13+
private function validateCoordinate(float $latitude, float $longitude): void
14+
{
15+
Validator::range(-90, 90)->assert($latitude, 'latitude');
16+
Validator::range(-180, 180)->assert($longitude, 'longitude');
17+
}
18+
19+
/**
20+
* @throws ValidationException
21+
*/
22+
private function validateDateRange(\DateTimeInterface $startDate, \DateTimeInterface $endDate): void
23+
{
24+
Validator::lessThan(new \DateTime('now'))->assert($endDate, 'endDate');
25+
Validator::greaterThan($startDate)->assert($endDate, 'endDate');
26+
}
27+
28+
/**
29+
* @throws ValidationException
30+
*/
31+
private function validatePastDate(\DateTimeInterface $date, string $name): void
32+
{
33+
Validator::lessThan(new \DateTime('now'))->assert($date, $name);
34+
}
35+
36+
/**
37+
* @throws ValidationException
38+
*/
39+
private function validateSearchQuery(string $searchQuery, string $name): void
40+
{
41+
Validator::notBlank()->assert($searchQuery, $name);
42+
}
43+
44+
/**
45+
* @throws ValidationException
46+
*/
47+
private function validateNumResults(int $numResults): void
48+
{
49+
Validator::greaterThan(0)->assert($numResults, 'numResults');
50+
}
51+
52+
/**
53+
* @throws ValidationException
54+
*/
55+
private function validateCountryCode(string $countryCode): void
56+
{
57+
Validator::country()->assert($countryCode, 'countryCode');
58+
}
59+
}

src/Endpoint/WeatherEndpoint.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,17 @@
55
use Http\Client\Exception;
66
use ProgrammatorDev\OpenWeatherMap\Endpoint\Util\LanguageTrait;
77
use ProgrammatorDev\OpenWeatherMap\Endpoint\Util\UnitSystemTrait;
8+
use ProgrammatorDev\OpenWeatherMap\Endpoint\Util\ValidationTrait;
89
use ProgrammatorDev\OpenWeatherMap\Entity\Weather\WeatherLocation;
910
use ProgrammatorDev\OpenWeatherMap\Entity\Weather\WeatherLocationList;
1011
use ProgrammatorDev\OpenWeatherMap\Exception\ApiErrorException;
1112
use ProgrammatorDev\YetAnotherPhpValidator\Exception\ValidationException;
12-
use ProgrammatorDev\YetAnotherPhpValidator\Validator;
1313

1414
class WeatherEndpoint extends AbstractEndpoint
1515
{
1616
use UnitSystemTrait;
1717
use LanguageTrait;
18+
use ValidationTrait;
1819

1920
private const NUM_RESULTS = 40;
2021

@@ -25,8 +26,7 @@ class WeatherEndpoint extends AbstractEndpoint
2526
*/
2627
public function getCurrent(float $latitude, float $longitude): WeatherLocation
2728
{
28-
Validator::range(-90, 90)->assert($latitude, 'latitude');
29-
Validator::range(-180, 180)->assert($longitude, 'longitude');
29+
$this->validateCoordinate($latitude, $longitude);
3030

3131
$data = $this->sendRequest(
3232
method: 'GET',
@@ -49,9 +49,8 @@ public function getCurrent(float $latitude, float $longitude): WeatherLocation
4949
*/
5050
public function getForecast(float $latitude, float $longitude, int $numResults = self::NUM_RESULTS): WeatherLocationList
5151
{
52-
Validator::range(-90, 90)->assert($latitude, 'latitude');
53-
Validator::range(-180, 180)->assert($longitude, 'longitude');
54-
Validator::greaterThan(0)->assert($numResults, 'numResults');
52+
$this->validateCoordinate($latitude, $longitude);
53+
$this->validateNumResults($numResults);
5554

5655
$data = $this->sendRequest(
5756
method: 'GET',

src/Entity/AirPollution/AirPollutionLocationList.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,28 @@
33
namespace ProgrammatorDev\OpenWeatherMap\Entity\AirPollution;
44

55
use ProgrammatorDev\OpenWeatherMap\Entity\Coordinate;
6-
use ProgrammatorDev\OpenWeatherMap\Util\CreateEntityListTrait;
6+
use ProgrammatorDev\OpenWeatherMap\Util\EntityListTrait;
77

88
class AirPollutionLocationList
99
{
10-
use CreateEntityListTrait;
10+
use EntityListTrait;
1111

1212
private Coordinate $coordinate;
1313

14+
/** @var AirPollution[] */
1415
private array $list;
1516

1617
public function __construct(array $data)
1718
{
1819
$this->coordinate = new Coordinate($data['coord']);
19-
$this->list = $this->createEntityList($data['list'], AirPollution::class);
20+
$this->list = $this->createEntityList(AirPollution::class, $data['list']);
2021
}
2122

2223
public function getCoordinate(): Coordinate
2324
{
2425
return $this->coordinate;
2526
}
2627

27-
/**
28-
* @return AirPollution[]
29-
*/
3028
public function getList(): array
3129
{
3230
return $this->list;

src/Entity/AirPollution/AirQuality.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22

33
namespace ProgrammatorDev\OpenWeatherMap\Entity\AirPollution;
44

5-
use ProgrammatorDev\OpenWeatherMap\Entity\AirPollution\Util\GetAirQualityQualitativeNameTrait;
5+
use ProgrammatorDev\OpenWeatherMap\Entity\AirPollution\Util\AirQualityQualitativeNameTrait;
66

77
class AirQuality
88
{
9-
use GetAirQualityQualitativeNameTrait;
9+
use AirQualityQualitativeNameTrait;
1010

1111
private int $index;
1212

src/Entity/AirPollution/Util/GetAirQualityQualitativeNameTrait.php renamed to src/Entity/AirPollution/Util/AirQualityQualitativeNameTrait.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace ProgrammatorDev\OpenWeatherMap\Entity\AirPollution\Util;
44

5-
trait GetAirQualityQualitativeNameTrait
5+
trait AirQualityQualitativeNameTrait
66
{
77
// Levels based on https://openweathermap.org/api/air-pollution
88
private array $airQualityIndex = [

0 commit comments

Comments
 (0)