Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Performance improvment #2 #7

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
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
26 changes: 12 additions & 14 deletions src/Factories/UkBankHolidayFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
abstract class UkBankHolidayFactory
{
private static $dataRetriver;
private static $dates = [];

private static function setUpDataRetriever()
{
Expand All @@ -15,35 +16,32 @@ private static function setUpDataRetriever()

public static function getAll($location = 'england-and-wales')
{
if (empty(self::$dates[$location]) === false) {
return self::$dates[$location];
}
self::setUpDataRetriever();
$dates = self::$dataRetriver->retrieve($location);
self::$dates[$location] = self::$dataRetriver->retrieve($location);

return $dates;
return self::$dates[$location];
}

public static function getByMonth($year, $month, $location = 'england-and-wales')
{
$dates = self::getAll($location);
$dateRange = [];
foreach ($dates as $date) {
if (date('Y', strtotime($date->date)) == $year && date('m', strtotime($date->date)) == $month) {
$dateRange[] = $date;
}
if (!isset($dates[$year][$month])) {
return [];
}

return $dateRange;
return $dates[$year][$month];
}

public static function getByDate($year, $month, $day, $location = 'england-and-wales')
{
$dates = self::getByMonth($year, $month, $location);
$dateRange = [];
foreach ($dates as $date) {
if (date('d', strtotime($date->date)) == $day) {
$dateRange[] = $date;
}
if (!isset($dates[$day])) {
return [];
}

return $dateRange;
return $dates[$day];
}
}
3 changes: 2 additions & 1 deletion src/Objects/DataRetrievers/GovUkDataRetriever.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,9 @@ public function retrieve($location)
$bankHolidayDates = [];

foreach ($data[$location]['events'] as $holidayDate) {
list($year, $month, $day) = explode('-', $holidayDate['date']);
$bankHolidayDate = new UkBankHoliday($holidayDate['title'], $holidayDate['date'], $holidayDate['notes']);
$bankHolidayDates[] = $bankHolidayDate;
$bankHolidayDates[$year][$month][$day][] = $bankHolidayDate;
}

return $bankHolidayDates;
Expand Down
2 changes: 1 addition & 1 deletion tests/Functional/BasicUsageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ final class BasicUsageTest extends TestCase
{
public function testGetNewYearsDay()
{
$holidays = UkBankHolidayFactory::getByDate(2017, 01, 2);
$holidays = UkBankHolidayFactory::getByDate('2017', '01', '02');

$this->assertTrue(is_array($holidays), 'Holidays should be an array.');
$this->assertEquals(1, count($holidays), 'Holidays array should have 1 value.');
Expand Down
13 changes: 9 additions & 4 deletions tests/Unit/HolidayRetrievalByLocationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,15 @@ private function checkHolidaysArrayFormat($holidays)
{
$this->assertTrue(is_array($holidays), 'Holidays should be an array.');
$this->assertTrue(count($holidays) > 0, 'Holidays array should have at least 1 value.');

$holiday = $holidays[0];

$this->assertEquals('RapidWeb\UkBankHolidays\Objects\UkBankHoliday', get_class($holiday));
$currYear = date('Y');
$this->assertTrue(is_array($holidays[$currYear]));
$months = end($holidays[$currYear]);
$this->assertTrue(is_array($months));
$days = end($months);
$this->assertTrue(is_array($days));
$day = end($days);

$this->assertEquals('RapidWeb\UkBankHolidays\Objects\UkBankHoliday', get_class($day));
}

public function testEnglandWales()
Expand Down
11 changes: 8 additions & 3 deletions tests/Unit/LaravelCacheDriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,15 @@ private function checkHolidaysArrayFormat($holidays)
{
$this->assertTrue(is_array($holidays), 'Holidays should be an array.');
$this->assertTrue(count($holidays) > 0, 'Holidays array should have at least 1 value.');
$currYear = date('Y');
$this->assertTrue(is_array($holidays[$currYear]));
$months = end($holidays[$currYear]);
$this->assertTrue(is_array($months));
$days = end($months);
$this->assertTrue(is_array($days));
$day = end($days);

$holiday = $holidays[0];

$this->assertEquals('RapidWeb\UkBankHolidays\Objects\UkBankHoliday', get_class($holiday));
$this->assertEquals('RapidWeb\UkBankHolidays\Objects\UkBankHoliday', get_class($day));
}

public function testEnglandWales()
Expand Down