-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: reorganize offline inst-calculation
- implement unit-tests - add DateTime wrapper - throw proper exceptions ORCA-1973 Squashed commit of the following: commit d9038a95afac8c972e593daa7e6234da0f382647 Author: Eduardo Iriarte-Mendez <eduardo.iriarte-mendez@ratepay.com> Date: Fri Jan 28 18:11:50 2022 +0100 feat: reorganize offline inst-calculation - implement unit-tests - add DateTime wrapper - throw proper exceptions
- Loading branch information
1 parent
d25eac1
commit 624cc8e
Showing
10 changed files
with
572 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
/* | ||
* Ratepay PHP-Library | ||
* | ||
* This document contains trade secret data which are the property of | ||
* Ratepay GmbH, Berlin, Germany. Information contained herein must not be used, | ||
* copied or disclosed in whole or part unless permitted in writing by Ratepay GmbH. | ||
* All rights reserved by Ratepay GmbH. | ||
* | ||
* Copyright (c) 2019 Ratepay GmbH / Berlin / Germany | ||
*/ | ||
|
||
namespace RatePAY\Exception; | ||
|
||
class OfflineInstalmentCalculationException extends \Exception | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
|
||
/* | ||
* Ratepay PHP-Library | ||
* | ||
* This document contains trade secret data which are the property of | ||
* Ratepay GmbH, Berlin, Germany. Information contained herein must not be used, | ||
* copied or disclosed in whole or part unless permitted in writing by Ratepay GmbH. | ||
* All rights reserved by Ratepay GmbH. | ||
* | ||
* Copyright (c) 2019 Ratepay GmbH / Berlin / Germany | ||
*/ | ||
|
||
namespace RatePAY\Service\DateTime; | ||
|
||
use DateTime as InternalDateTime; | ||
|
||
/** | ||
* DateTime wrapper class inspired by Carbon. | ||
* It provides handy accessors and allow isolation of internal classes during testing. | ||
*/ | ||
class DateTime extends InternalDateTime | ||
{ | ||
use DateTimeTesting; | ||
use DateTimeModifiers; | ||
use DateTimeCalculators; | ||
|
||
/** | ||
* @return DateTime | ||
*/ | ||
public static function now() | ||
{ | ||
return self::hasTestingNow() ? self::getTestingNow() : new DateTime(); | ||
} | ||
|
||
/** | ||
* @return DateTime | ||
*/ | ||
public static function today() | ||
{ | ||
return self::hasTestingNow() ? self::getTestingNow()->setTime(0, 0) : new DateTime('today'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?php | ||
|
||
/* | ||
* Ratepay PHP-Library | ||
* | ||
* This document contains trade secret data which are the property of | ||
* Ratepay GmbH, Berlin, Germany. Information contained herein must not be used, | ||
* copied or disclosed in whole or part unless permitted in writing by Ratepay GmbH. | ||
* All rights reserved by Ratepay GmbH. | ||
* | ||
* Copyright (c) 2019 Ratepay GmbH / Berlin / Germany | ||
*/ | ||
|
||
namespace RatePAY\Service\DateTime; | ||
|
||
trait DateTimeCalculators | ||
{ | ||
/** | ||
* @param DateTime $dateTime related date time to perform calculation | ||
* | ||
* @return int|float difference between dates in seconds | ||
*/ | ||
public function diffInSeconds($dateTime) | ||
{ | ||
return $this->getTimestamp() - $dateTime->getTimestamp(); | ||
} | ||
|
||
/** | ||
* @param DateTime $dateTime related date time to perform calculation | ||
* | ||
* @return int|float difference between dates in minutes | ||
*/ | ||
public function diffInMinutes($dateTime) | ||
{ | ||
return $this->diffInSeconds($dateTime) / 60; | ||
} | ||
|
||
/** | ||
* @param DateTime $dateTime related date time to perform calculation | ||
* | ||
* @return int|float difference between dates in minutes | ||
*/ | ||
public function diffInHours($dateTime) | ||
{ | ||
return $this->diffInMinutes($dateTime) / 60; | ||
} | ||
|
||
/** | ||
* @param DateTime $dateTime related date time to perform calculation | ||
* | ||
* @return int|float difference between dates in minutes | ||
*/ | ||
public function diffInDays($dateTime) | ||
{ | ||
return $this->diffInHours($dateTime) / 24; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
<?php | ||
|
||
/* | ||
* Ratepay PHP-Library | ||
* | ||
* This document contains trade secret data which are the property of | ||
* Ratepay GmbH, Berlin, Germany. Information contained herein must not be used, | ||
* copied or disclosed in whole or part unless permitted in writing by Ratepay GmbH. | ||
* All rights reserved by Ratepay GmbH. | ||
* | ||
* Copyright (c) 2019 Ratepay GmbH / Berlin / Germany | ||
*/ | ||
|
||
namespace RatePAY\Service\DateTime; | ||
|
||
trait DateTimeModifiers | ||
{ | ||
/** | ||
* @param int $day days of month to set | ||
* | ||
* @return DateTime | ||
*/ | ||
public function setDay($day) | ||
{ | ||
$date = $this->getDateFields(); | ||
|
||
return $this->setDate($date->year, $date->month, $day); | ||
} | ||
|
||
/** | ||
* @param int $daysOffset number of days to be added | ||
* | ||
* @return DateTime | ||
*/ | ||
public function addDays($daysOffset) | ||
{ | ||
$date = $this->getDateFields(); | ||
|
||
return $this->setDate($date->year, $date->month, $date->day + $daysOffset); | ||
} | ||
|
||
/** | ||
* @param int $month month of year to set | ||
* | ||
* @return DateTime | ||
*/ | ||
public function setMonth($month) | ||
{ | ||
$date = $this->getDateFields(); | ||
|
||
return $this->setDate($date->year, $month, $date->day); | ||
} | ||
|
||
/** | ||
* @param int $monthsOffset number of months to be added | ||
* | ||
* @return DateTime | ||
*/ | ||
public function addMonths($monthsOffset) | ||
{ | ||
$date = $this->getDateFields(); | ||
|
||
return $this->setDate($date->year, $date->month + $monthsOffset, $date->day); | ||
} | ||
|
||
/** | ||
* @param int $year year to set | ||
* | ||
* @return DateTime | ||
*/ | ||
public function setYear($year) | ||
{ | ||
$date = $this->getDateFields(); | ||
|
||
return $this->setDate($year, $date->month, $date->day); | ||
} | ||
|
||
/** | ||
* @param int $yearsOffset number of years to be added | ||
* | ||
* @return DateTime | ||
*/ | ||
public function addYears($yearsOffset) | ||
{ | ||
$date = $this->getDateFields(); | ||
|
||
return $this->setDate($date->year + $yearsOffset, $date->month, $date->day); | ||
} | ||
|
||
/** | ||
* @return object | ||
*/ | ||
private function getDateFields() | ||
{ | ||
return (object) [ | ||
'year' => (int) $this->format('Y'), | ||
'month' => (int) $this->format('m'), | ||
'day' => (int) $this->format('d'), | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?php | ||
|
||
/* | ||
* Ratepay PHP-Library | ||
* | ||
* This document contains trade secret data which are the property of | ||
* Ratepay GmbH, Berlin, Germany. Information contained herein must not be used, | ||
* copied or disclosed in whole or part unless permitted in writing by Ratepay GmbH. | ||
* All rights reserved by Ratepay GmbH. | ||
* | ||
* Copyright (c) 2019 Ratepay GmbH / Berlin / Germany | ||
*/ | ||
|
||
namespace RatePAY\Service\DateTime; | ||
|
||
trait DateTimeTesting | ||
{ | ||
/** | ||
* @var DateTime | ||
*/ | ||
private static $_testingNow; | ||
|
||
/** | ||
* It sets a custom DateTime instance as the "now" value. | ||
* This handy hook allows performing tests without side effects. | ||
* | ||
* @return DateTime | ||
*/ | ||
public static function withTestingNow(DateTime $dateTime) | ||
{ | ||
self::$_testingNow = $dateTime; | ||
|
||
return self::now(); | ||
} | ||
|
||
/** | ||
* It restores testing now to empty value and allows normal DateTime functionality. | ||
* | ||
* @return void | ||
*/ | ||
public static function resetTestingNow() | ||
{ | ||
self::$_testingNow = null; | ||
} | ||
|
||
/** | ||
* @return bool | ||
*/ | ||
private static function hasTestingNow() | ||
{ | ||
return isset(self::$_testingNow); | ||
} | ||
|
||
/** | ||
* @return DateTime | ||
*/ | ||
private static function getTestingNow() | ||
{ | ||
return clone self::$_testingNow; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.