-
-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Store schedule monitoring configurations in its own singleton
Instead of declaring runtime properties in the macros, we delegate storage of configuration properties to an extra class.
- Loading branch information
Showing
6 changed files
with
140 additions
and
16 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
13 changes: 13 additions & 0 deletions
13
src/Support/Concerns/UsesScheduleMonitoringConfigurationsRepository.php
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,13 @@ | ||
<?php | ||
|
||
namespace Spatie\ScheduleMonitor\Support\Concerns; | ||
|
||
use Spatie\ScheduleMonitor\Support\ScheduledTasks\ScheduleMonitoringConfigurationsRepository; | ||
|
||
trait UsesScheduleMonitoringConfigurationsRepository | ||
{ | ||
public function getScheduleMonitoringConfigurationsRepository(): ScheduleMonitoringConfigurationsRepository | ||
{ | ||
return app(ScheduleMonitoringConfigurationsRepository::class); | ||
} | ||
} |
80 changes: 80 additions & 0 deletions
80
src/Support/ScheduledTasks/ScheduleMonitoringConfigurationsRepository.php
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,80 @@ | ||
<?php | ||
|
||
namespace Spatie\ScheduleMonitor\Support\ScheduledTasks; | ||
|
||
class ScheduleMonitoringConfigurationsRepository | ||
{ | ||
private array $store = []; | ||
|
||
public function setMonitorName(object $target, string $monitorName): void | ||
{ | ||
$this->setProperty($target, 'monitorName', $monitorName); | ||
} | ||
|
||
public function getMonitorName(object $target): ?string | ||
{ | ||
return $this->getProperty($target, 'monitorName'); | ||
} | ||
|
||
public function setGraceTimeInMinutes(object $target, int $graceTimeInMinutes): void | ||
{ | ||
$this->setProperty($target, 'graceTimeInMinutes', $graceTimeInMinutes); | ||
} | ||
|
||
public function getGraceTimeInMinutes(object $target): ?int | ||
{ | ||
return $this->getProperty($target, 'graceTimeInMinutes'); | ||
} | ||
|
||
public function setDoNotMonitor(object $target, bool $doNotMonitor = true): void | ||
{ | ||
$this->setProperty($target, 'doNotMonitor', $doNotMonitor); | ||
} | ||
|
||
public function getDoNotMonitor(object $target): ?bool | ||
{ | ||
return $this->getProperty($target, 'doNotMonitor'); | ||
} | ||
|
||
public function setDoNotMonitorAtOhDear(object $target, bool $doNotMonitorAtOhDear = true): void | ||
{ | ||
$this->setProperty($target, 'doNotMonitorAtOhDear', $doNotMonitorAtOhDear); | ||
} | ||
|
||
public function getDoNotMonitorAtOhDear(object $target): ?bool | ||
{ | ||
return $this->getProperty($target, 'doNotMonitorAtOhDear'); | ||
} | ||
|
||
public function setStoreOutputInDb(object $target, bool $storeOutputInDb = true): void | ||
{ | ||
$this->setProperty($target, 'storeOutputInDb', $storeOutputInDb); | ||
} | ||
|
||
public function getStoreOutputInDb(object $target): ?bool | ||
{ | ||
return $this->getProperty($target, 'storeOutputInDb'); | ||
} | ||
|
||
|
||
private function setProperty(object $target, string $key, mixed $value): void | ||
{ | ||
data_set($this->store, $this->makeKey($target, $key), $value); | ||
} | ||
|
||
private function getProperty(object $target, string $key): mixed | ||
{ | ||
dump(__METHOD__, $this->store); | ||
|
||
return data_get($this->store, $this->makeKey($target, $key)); | ||
} | ||
|
||
private function makeKey(object $target, string $key): array | ||
{ | ||
return [ | ||
$target::class, | ||
spl_object_hash($target), | ||
$key, | ||
]; | ||
} | ||
} |
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
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
14 changes: 14 additions & 0 deletions
14
tests/Traits/UsesScheduleMonitoringConfigurationsRepositoryTest.php
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,14 @@ | ||
<?php | ||
|
||
use Spatie\ScheduleMonitor\Support\Concerns\UsesScheduleMonitoringConfigurationsRepository; | ||
use Spatie\ScheduleMonitor\Support\ScheduledTasks\ScheduleMonitoringConfigurationsRepository; | ||
|
||
it('can resolve schedule monitoring configurations repository', function () { | ||
$concern = new class() { | ||
use UsesScheduleMonitoringConfigurationsRepository; | ||
}; | ||
|
||
$repository = $concern->getScheduleMonitoringConfigurationsRepository(); | ||
|
||
expect($repository)->toBeInstanceOf(ScheduleMonitoringConfigurationsRepository::class); | ||
}); |