Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
sifex committed Jul 26, 2022
1 parent 67e1bef commit 6b22cbe
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 deletions.
15 changes: 10 additions & 5 deletions src/SLA.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,15 @@ class SLA
private array $pause_periods = [];

/**
* @param SLASchedule $schedule
* @param SLASchedule|array $schedules
*
* @throws ReflectionException
*/
public function __construct(SLASchedule $schedule)
public function __construct(SLASchedule|array $schedules)
{
CarbonPeriod::mixin(EnhancedPeriod::class);

$this->addSchedule($schedule);
collect([$schedules])->flatten()->each(fn ($b) => $this->addSchedule($b));
}

public function addBreach(SLABreach $breach): self
Expand All @@ -54,7 +54,7 @@ public function addBreach(SLABreach $breach): self

public function addBreaches(...$breaches): self
{
collect($breaches)->flatten()->each(fn ($b) => $this->addBreach($b));
collect([$breaches])->flatten()->each(fn ($b) => $this->addBreach($b));

return $this;
}
Expand Down Expand Up @@ -82,7 +82,7 @@ public function clearPausePeriods(): self

public function addHolidays($dates): self
{
collect($dates)->flatten()->each(fn ($d) => $this->addHoliday($d));
collect([$dates])->flatten()->each(fn ($d) => $this->addHoliday($d));

return $this;
}
Expand All @@ -99,6 +99,11 @@ public static function fromSchedule(SLASchedule $definition): self
return new self($definition);
}

public static function fromSchedules(SLASchedule|array $definition): self
{
return new self($definition);
}

public function status(string $started_at, string $stopped_at = null): SLAStatus
{
return $this->calculate($started_at, $stopped_at);
Expand Down
20 changes: 20 additions & 0 deletions tests/SLATest.php
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,26 @@
expect($sla->duration('2022-07-25 08:59:00')->totalSeconds)->toEqual(230);
});

it('tests adding multiple schedules through the constructor', function () {
SLA::fromSchedules([
SLASchedule::create()->from('09:00:00')->to('09:01:00') // 60 seconds
->everyDay(),
SLASchedule::create()->effectiveFrom('27-07-2022')
->from('09:00:00')->to('09:00:30')->onWeekdays()->and() // 30 seconds
->from('09:00:00')->to('09:00:10')->onWeekends(), // 10 seconds
]);

(new SLA([
SLASchedule::create()->from('09:00:00')->to('09:01:00') // 60 seconds
->everyDay(),
SLASchedule::create()->effectiveFrom('27-07-2022')
->from('09:00:00')->to('09:00:30')->onWeekdays()->and() // 30 seconds
->from('09:00:00')->to('09:00:10')->onWeekends(), // 10 seconds
]));

$this->expectNotToPerformAssertions();
});

//it('tests 0 length SLAs', function () {
// $subject_start_time = '2022-07-21 08:59:00';
// $time_now = '2022-07-21 09:00:30';
Expand Down

0 comments on commit 6b22cbe

Please sign in to comment.