Skip to content

Commit

Permalink
Merge pull request #37 from msiebeneicher/enhancement/improve_epsilon…
Browse files Browse the repository at this point in the history
…_validation

Enhancement/improve epsilon validation [BC]
  • Loading branch information
msiebeneicher committed Sep 25, 2015
2 parents 5dadb18 + 58bfccc commit 4b0407c
Show file tree
Hide file tree
Showing 9 changed files with 272 additions and 48 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
# dev-master (v0.4.x)
2015-09-25 msiebeneicher <marc.siebeneicher@trivago.com>
* Improved epsilon validation [issue#36]
* added own Iso8601Entity and refactor deprecated usage of parseIso8601String()
* removed deprecated method parseIso8601String() [BC]

2015-09-20 msiebeneicher <marc.siebeneicher@trivago.com>
* [issue#17] - Added optional parameters to configure command
* [issue#13] - Added validation command
Expand Down
12 changes: 6 additions & 6 deletions src/BusinessCase/Comparison/JobComparisonBusinessCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -311,16 +311,16 @@ private function isSchedulePropertyIdentical(JobEntity $oJobEntityA, JobEntity $
*/
private function createDateTimeObj($sIso8601String, $sTimeZone = '')
{
$aMatch = $this->oDatePeriodFactory->parseIso8601String($sIso8601String);
$_oIso8601Entity = $this->oDatePeriodFactory->createIso8601Entity($sIso8601String);

if (!empty($sTimeZone))
{
$_oDateTime = new \DateTime(str_replace('Z', '', $aMatch[2]));
$_oDateTime = new \DateTime(str_replace('Z', '', $_oIso8601Entity->sStartTime));
$_oDateTime->setTimezone(new \DateTimeZone($sTimeZone));
}
else
{
$_oDateTime = new \DateTime($aMatch[2]);
$_oDateTime = new \DateTime($_oIso8601Entity->sStartTime);
}

return $_oDateTime;
Expand All @@ -333,10 +333,10 @@ private function createDateTimeObj($sIso8601String, $sTimeZone = '')
*/
private function isEqualInterval($sIso8601StringA, $sIso8601StringB)
{
$aMatchA = $this->oDatePeriodFactory->parseIso8601String($sIso8601StringA);
$aMatchB = $this->oDatePeriodFactory->parseIso8601String($sIso8601StringB);
$_oIso8601EntityA = $this->oDatePeriodFactory->createIso8601Entity($sIso8601StringA);
$_oIso8601EntityB = $this->oDatePeriodFactory->createIso8601Entity($sIso8601StringB);

return ($aMatchA[3] == $aMatchB[3]);
return ($_oIso8601EntityA->sInterval == $_oIso8601EntityB->sInterval);
}

/**
Expand Down
5 changes: 2 additions & 3 deletions src/Commands/SchedulingViewCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -333,9 +333,8 @@ private function getJobStartTimesInPeriod(\DatePeriod $oJobDatePeriod, $iJobStar
*/
private function createDatePeriodForJob(JobEntity $oJobEntity, $iEndTime)
{
$aMatch = $this->oDatePeriodFactory->parseIso8601String($oJobEntity->schedule);

return $this->createDatePeriod($aMatch[2], 0, null, $iEndTime, $aMatch[3]);
$_oIso8601Entity = $this->oDatePeriodFactory->createIso8601Entity($oJobEntity->schedule);
return $this->createDatePeriod($_oIso8601Entity->sStartTime, 0, null, $iEndTime, $_oIso8601Entity->sInterval);
}

/**
Expand Down
38 changes: 25 additions & 13 deletions src/Component/DatePeriod/DatePeriodFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,40 @@

namespace Chapi\Component\DatePeriod;

use Chapi\Entity\DatePeriod\Iso8601Entity;
use Chapi\Exception\DatePeriodException;

class DatePeriodFactory implements DatePeriodFactoryInterface
{
const REG_EX_ISO_8601_STRING = '#(R[0-9]*)/(.*)/(P.*)#';
/**
* @var Iso8601Entity[]
*/
private static $aIso8601Entity = [];

/**
* @param $sIso8601
* @return null|string[]
* @param string $sIso8601
* @return Iso8601Entity
* @throws DatePeriodException
*/
public function parseIso8601String($sIso8601)
public function createIso8601Entity($sIso8601)
{
$aMatch = [];
preg_match(self::REG_EX_ISO_8601_STRING, $sIso8601, $aMatch);
$_sKey = md5($sIso8601); // class cache key

if (count($aMatch) != 4)
// return instance
if (isset(self::$aIso8601Entity[$_sKey]))
{
throw new DatePeriodException(sprintf("Can't parse '%s' as iso 8601 string.", $sIso8601));
return self::$aIso8601Entity[$_sKey];
}

return $aMatch;
// init instance
try
{
return self::$aIso8601Entity[$_sKey] = new Iso8601Entity($sIso8601);
}
catch (\InvalidArgumentException $_oException)
{
throw new DatePeriodException(sprintf("Can't init Iso8601Entity for '%s' iso 8601 string.", $sIso8601), 1, $_oException);
}
}

/**
Expand All @@ -40,19 +52,19 @@ public function parseIso8601String($sIso8601)
*/
public function createDatePeriod($sIso8601, $sTimeZone = '')
{
$aMatch = $this->parseIso8601String($sIso8601);
$_oIso8601Entity = $this->createIso8601Entity($sIso8601);

if (!empty($sTimeZone))
{
$_oDateStart = new \DateTime(str_replace('Z', '', $aMatch[2]));
$_oDateStart = new \DateTime(str_replace('Z', '', $_oIso8601Entity->sStartTime));
$_oDateStart->setTimezone(new \DateTimeZone($sTimeZone));
}
else
{
$_oDateStart = new \DateTime($aMatch[2]);
$_oDateStart = new \DateTime($_oIso8601Entity->sStartTime);
}

$_oDateInterval = new \DateInterval($aMatch[3]);
$_oDateInterval = new \DateInterval($_oIso8601Entity->sInterval);
$_oDataEnd = new \DateTime();

$_oDateStart->sub($_oDateInterval);
Expand Down
10 changes: 7 additions & 3 deletions src/Component/DatePeriod/DatePeriodFactoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,19 @@

namespace Chapi\Component\DatePeriod;

use Chapi\Entity\DatePeriod\Iso8601Entity;
use Chapi\Exception\DatePeriodException;

interface DatePeriodFactoryInterface
{
const DIC_NAME = 'DatePeriodFactoryInterface';

/**
* @param $sIso8601
* @return mixed
* @param string $sIso8601
* @return Iso8601Entity
* @throws DatePeriodException
*/
public function parseIso8601String($sIso8601);
public function createIso8601Entity($sIso8601);

/**
* @param $sIso8601
Expand Down
60 changes: 60 additions & 0 deletions src/Entity/DatePeriod/Iso8601Entity.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php
/**
* @package: chapi
*
* @author: msiebeneicher
* @since: 2015-09-25
*
* @link: https://github.com/msiebeneicher/chapi/issues/36
*/


namespace Chapi\Entity\DatePeriod;


class Iso8601Entity
{
const REG_EX_ISO_8601_STRING = '#(R[0-9]*)/(.*)/(P.*)#';

/** @var string */
public $sIso8601 = '';

/** @var string */
public $sRepeat = '';

/** @var string */
public $sStartTime = '';

/** @var string */
public $sInterval = '';

/**
* @param string $sIso8601
*/
public function __construct($sIso8601)
{
$this->sIso8601 = $sIso8601;

$_aMatch = $this->parseIsoString();
if (count($_aMatch) != 4)
{
throw new \InvalidArgumentException(sprintf("Can't parse '%s' as iso 8601 string.", $sIso8601));
}

$this->sRepeat = $_aMatch[1];
$this->sStartTime = $_aMatch[2];
$this->sInterval = $_aMatch[3];
}

/**
* @return array
*/
private function parseIsoString()
{
$_aMatch = [];

preg_match(self::REG_EX_ISO_8601_STRING, $this->sIso8601, $_aMatch);

return $_aMatch;
}
}
42 changes: 41 additions & 1 deletion src/Service/JobRepository/JobEntityValidatorService.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,13 @@ public function validateJobEntity(JobEntity $oJobEntity)
case 'description':
case 'owner':
case 'ownerName':
case 'epsilon':
$_aValidProperties[$_sProperty] = (!empty($oJobEntity->{$_sProperty}));
break;

case 'epsilon':
$_aValidProperties[$_sProperty] = $this->isEpsilonPropertyValid($oJobEntity);
break;

case 'async':
case 'disabled':
case 'softError':
Expand Down Expand Up @@ -144,4 +147,41 @@ private function isSchedulePropertyValid(JobEntity $oJobEntity)

return false;
}

/**
* @param JobEntity $oJobEntity
* @return bool
*/
private function isEpsilonPropertyValid(JobEntity $oJobEntity)
{
if ($oJobEntity->isSchedulingJob() && !empty($oJobEntity->epsilon))
{
try
{
$_oDateIntervalEpsilon = new \DateInterval($oJobEntity->epsilon);
$_iIntervalEpsilon = (int) $_oDateIntervalEpsilon->format('%Y%M%D%H%I%S');

if ($_iIntervalEpsilon > 30) // if epsilon > "PT30S"
{
$_oIso8601Entity = $this->oDatePeriodFactory->createIso8601Entity($oJobEntity->schedule);

$_oDateIntervalScheduling = new \DateInterval($_oIso8601Entity->sInterval);
$_iIntervalScheduling = (int) $_oDateIntervalScheduling->format('%Y%M%D%H%I%S');

return ($_iIntervalScheduling > $_iIntervalEpsilon);
}

// if epsilon is less or equal than 30sec the not empty check is enough
return true;
}
catch (\Exception $_oException)
{
// can't init \DateInterval instance
return false;
}
}

// else
return (!empty($oJobEntity->epsilon));
}
}
35 changes: 14 additions & 21 deletions test/unit/Component/DatePeriod/DatePeriodFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,20 @@ public function testParseIso8601StringSuccess()
{
$_oDatePeriodFactory = new DatePeriodFactory();

$_aExpectedMatchValues = [
'R/2015-07-07T01:00:00Z/P1D',
'R',
'2015-07-07T01:00:00Z',
'P1D'
];
$this->assertEquals(
$_aExpectedMatchValues,
$_oDatePeriodFactory->parseIso8601String($_aExpectedMatchValues[0])
);
$_oIso8601Entity = $_oDatePeriodFactory->createIso8601Entity('R/2015-07-07T01:00:00Z/P1D');

$_aExpectedMatchValues = [
'R0/2015-07-07T01:00:00Z/PT1M',
'R0',
'2015-07-07T01:00:00Z',
'PT1M'
];
$this->assertEquals(
$_aExpectedMatchValues,
$_oDatePeriodFactory->parseIso8601String($_aExpectedMatchValues[0])
);
$this->assertEquals('R/2015-07-07T01:00:00Z/P1D', $_oIso8601Entity->sIso8601);
$this->assertEquals('R', $_oIso8601Entity->sRepeat);
$this->assertEquals('2015-07-07T01:00:00Z', $_oIso8601Entity->sStartTime);
$this->assertEquals('P1D', $_oIso8601Entity->sInterval);


$_oIso8601Entity = $_oDatePeriodFactory->createIso8601Entity('R0/2015-07-07T01:00:00Z/PT1M');

$this->assertEquals('R0/2015-07-07T01:00:00Z/PT1M', $_oIso8601Entity->sIso8601);
$this->assertEquals('R0', $_oIso8601Entity->sRepeat);
$this->assertEquals('2015-07-07T01:00:00Z', $_oIso8601Entity->sStartTime);
$this->assertEquals('PT1M', $_oIso8601Entity->sInterval);
}

/**
Expand All @@ -48,7 +41,7 @@ public function testParseIso8601StringFailure()
$_oDatePeriodFactory = new DatePeriodFactory();

$this->assertNull(
$_oDatePeriodFactory->parseIso8601String('2015-07-07T01:00:00Z/P1D')
$_oDatePeriodFactory->createIso8601Entity('2015-07-07T01:00:00Z/P1D')
);
}

Expand Down

0 comments on commit 4b0407c

Please sign in to comment.