Skip to content

Commit

Permalink
Merge pull request #23 from Roomify/improvement
Browse files Browse the repository at this point in the history
Improvements
  • Loading branch information
istos authored Oct 22, 2017
2 parents 919b182 + 471401b commit e8522d0
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 27 deletions.
4 changes: 2 additions & 2 deletions src/Constraint/ConstraintManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ public function __construct($constraints = array()) {
*/
public function getConstraints($constraint_class = NULL) {
if ($constraint_class == NULL) {
return $this->constraint;
return $this->constraints;
} else {
return $this->constraint[$constraint_class];
return $this->constraints[$constraint_class];
}
}

Expand Down
28 changes: 13 additions & 15 deletions src/Event/EventItemizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ private function createDayGranural($itemized = array()) {

if ($this->event->isSameDay()) {
if (!($this->event->getStartDate()->format('H:i') == '00:00' && $this->event->getEndDate()->format('H:i') == '23:59')) {
$itemized_same_day = $this->createHourlyGranular($start_date, $end_date->add(new \DateInterval('PT1M')), $interval, $start_date);
$itemized_same_day = $this->createHourlyGranular($start_date, $end_date->add(new \DateInterval('PT1M')), $interval);
$itemized[EventItemizer::BAT_DAY][$sy][$sm]['d' . $sd] = -1;
$itemized[EventItemizer::BAT_HOUR][$sy][$sm]['d' . $sd] = $itemized_same_day[EventItemizer::BAT_HOUR][$sy][$sm]['d' . $sd];
$itemized[EventItemizer::BAT_MINUTE][$sy][$sm]['d' . $sd] = $itemized_same_day[EventItemizer::BAT_MINUTE][$sy][$sm]['d' . $sd];
Expand All @@ -225,7 +225,7 @@ private function createDayGranural($itemized = array()) {
else {
// Deal with the start day unless it starts on midnight precisely at which point the whole day is booked
if (!($this->event->getStartDate()->format('H:i') == '00:00')) {
$itemized_start = $this->createHourlyGranular($start_date, new \DateTime($start_date->format("Y-n-j 23:59:59")), $interval, $start_date);
$itemized_start = $this->createHourlyGranular($start_date, new \DateTime($start_date->format("Y-n-j 23:59:59")), $interval);
$itemized[EventItemizer::BAT_DAY][$sy][$sm]['d' . $sd] = -1;
$itemized[EventItemizer::BAT_HOUR][$sy][$sm]['d' . $sd] = $itemized_start[EventItemizer::BAT_HOUR][$sy][$sm]['d' . $sd];
$itemized[EventItemizer::BAT_MINUTE][$sy][$sm]['d' . $sd] = $itemized_start[EventItemizer::BAT_MINUTE][$sy][$sm]['d' . $sd];
Expand All @@ -242,7 +242,7 @@ private function createDayGranural($itemized = array()) {
$itemized[EventItemizer::BAT_MINUTE][$ey][$em]['d' . $ed] = array();
}
else {
$itemized_end = $this->createHourlyGranular(new \DateTime($end_date->format("Y-n-j 00:00:00")), $end_date->add(new \DateInterval('PT1M')), $interval, new \DateTime($end_date->format("Y-n-j 00:00:00")));
$itemized_end = $this->createHourlyGranular(new \DateTime($end_date->format("Y-n-j 00:00:00")), $end_date->add(new \DateInterval('PT1M')), $interval);
$itemized[EventItemizer::BAT_DAY][$ey][$em]['d' . $ed] = -1;
$itemized[EventItemizer::BAT_HOUR][$ey][$em]['d' . $ed] = $itemized_end[EventItemizer::BAT_HOUR][$ey][$em]['d' . $ed];
$itemized[EventItemizer::BAT_MINUTE][$ey][$em]['d' . $ed] = $itemized_end[EventItemizer::BAT_MINUTE][$ey][$em]['d' . $ed];
Expand All @@ -259,28 +259,31 @@ private function createDayGranural($itemized = array()) {
* @param \DateTime $start_date
* @param \DateTime $end_date
* @param \DateInterval $interval
* @param \DateTime $period_start
* @return array
*/
public function createHourlyGranular(\DateTime $start_date, \DateTime $end_date, \DateInterval $interval, \DateTime $period_start) {
public function createHourlyGranular(\DateTime $start_date, \DateTime $end_date, \DateInterval $interval) {
$period = new \DatePeriod($start_date, $interval, $end_date);

$itemized = array();

$start_minute = (int)$period_start->format('i');
$init = TRUE;
$start_minute = (int)$start_date->format('i');

$event_value = $this->event->getValue();

$year = $start_date->format('Y');
$month = $start_date->format('n');
$day = $start_date->format('j');
$hour = $start_date->format('G');
$min = $start_date->format('i');

foreach ($period as $minute) {
if ($init) {
// Re-calculate if we're at a day boundary.
if ($hour == 24) {
$year = $minute->format('Y');
$month = $minute->format('n');
$day = $minute->format('j');
$hour = $minute->format('G');
$min = $minute->format('i');

$init = FALSE;
}

// Doing minutes so set the values in the minute array
Expand All @@ -304,11 +307,6 @@ public function createHourlyGranular(\DateTime $start_date, \DateTime $end_date,
$start_minute = 0;
}

if ($hour == 24) {
// Re-calculate if we're at a day boundary.
$init = TRUE;
}

$min = str_pad($min, 2, 0, STR_PAD_LEFT);
}

Expand Down
7 changes: 4 additions & 3 deletions src/Store/DrupalDBStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace Roomify\Bat\Store;

use Roomify\Bat\Event\EventInterface;
use Roomify\Bat\Event\Event;
use Roomify\Bat\Event\EventItemizer;
use Roomify\Bat\Store\SqlDBStore;
Expand Down Expand Up @@ -70,12 +71,12 @@ public function getEventData(\DateTime $start_date, \DateTime $end_date, $unit_i
}

/**
* @param \Roomify\Bat\Event\Event $event
* @param \Roomify\Bat\Event\EventInterface $event
* @param $granularity
*
* @return bool
*/
public function storeEvent(Event $event, $granularity = Event::BAT_HOURLY) {
public function storeEvent(EventInterface $event, $granularity = Event::BAT_HOURLY) {
$stored = TRUE;
$transaction = db_transaction();

Expand Down Expand Up @@ -118,7 +119,7 @@ public function storeEvent(Event $event, $granularity = Event::BAT_HOURLY) {
}
}

//If we have minutes write minutes
// If we have minutes write minutes
foreach ($itemized[Event::BAT_MINUTE] as $year => $months) {
foreach ($months as $month => $days) {
foreach ($days as $day => $hours) {
Expand Down
2 changes: 1 addition & 1 deletion src/Store/SqlDBStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public function buildQueries(\DateTime $start_date, \DateTime $end_date, $unit_i
$queries[Event::BAT_MINUTE] = 'SELECT * FROM ' . $this->minute_table . ' WHERE ';

// Create a mock event which we will use to determine how to query the database
$mock_event = new Event($start_date, $end_date, new Unit(0, 0, NULL), -10);
$mock_event = new Event($start_date, $end_date, new Unit(0, 0, array()), -10);
// We don't need a granular event even if we are retrieving granular data - since we don't
// know what the event break-down is going to be we need to get the full range of data from
// days, hours and minutes.
Expand Down
5 changes: 3 additions & 2 deletions src/Store/SqlLiteDBStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace Roomify\Bat\Store;

use Roomify\Bat\Event\EventInterface;
use Roomify\Bat\Event\Event;
use Roomify\Bat\Event\EventItemizer;
use Roomify\Bat\Store\SqlDBStore;
Expand Down Expand Up @@ -78,12 +79,12 @@ public function getEventData(\DateTime $start_date, \DateTime $end_date, $unit_i
}

/**
* @param \Roomify\Bat\Event\Event $event
* @param \Roomify\Bat\Event\EventInterface $event
* @param $granularity
*
* @return bool
*/
public function storeEvent(Event $event, $granularity = Event::BAT_HOURLY) {
public function storeEvent(EventInterface $event, $granularity = Event::BAT_HOURLY) {
$stored = TRUE;

// Get existing event data from db
Expand Down
5 changes: 3 additions & 2 deletions src/Store/StoreInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

namespace Roomify\Bat\Store;

use Roomify\Bat\Event\Event;
use Roomify\Bat\Event\EventInterface;

/**
* A store is a place where event data is held. The purpose of separating these
Expand Down Expand Up @@ -35,5 +35,6 @@ public function getEventData(\DateTime $start_date, \DateTime $end_date, $unit_i
*
* @return boolean
*/
public function storeEvent(Event $event, $granularity);
public function storeEvent(EventInterface $event, $granularity);

}
3 changes: 1 addition & 2 deletions src/Valuator/AbstractValuator.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,9 @@ public function setEndDate(\DateTime $end_date) {
}

/**
* @param \DateTime $end_date
* @return \DateTime
*/
public function getEndDate(\DateTime $end_date) {
public function getEndDate() {
return $this->end_date;
}

Expand Down
20 changes: 20 additions & 0 deletions tests/EventTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -554,4 +554,24 @@ public function testEventItemizeEventOneDay() {
$this->assertEquals($itemized[Event::BAT_DAY]['2016']['3']['d1'], '5');
}

public function testCreateHourlyGranular() {
$event_state = 5;
$start_date = new \DateTime('2016-01-30 12:12');
$end_date = new \DateTime('2016-02-02 12:12');
$unit = new Unit(1, 2, array());

$event = new Event($start_date, $end_date, $unit, $event_state);

$itemizer = new EventItemizer($event);
$itemized = $itemizer->createHourlyGranular($start_date, $end_date, new \DateInterval('PT1M'));

$this->assertEquals($itemized[Event::BAT_HOUR]['2016']['1']['d30']['h12'], '-1');

$this->assertEquals($itemized[Event::BAT_MINUTE]['2016']['1']['d30']['h12']['m12'], '5');

$this->assertEquals($itemized[Event::BAT_HOUR]['2016']['2']['d2']['h12'], '-1');

$this->assertEquals($itemized[Event::BAT_MINUTE]['2016']['2']['d2']['h12']['m11'], '5');
}

}

0 comments on commit e8522d0

Please sign in to comment.