-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScheduleOption.php
More file actions
42 lines (34 loc) · 1.22 KB
/
Copy pathScheduleOption.php
File metadata and controls
42 lines (34 loc) · 1.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<?php
namespace RonasIT\Larabuilder\ValueOptions;
use Illuminate\Console\Scheduling\Event;
use Illuminate\Console\Scheduling\ManagesAttributes;
use Illuminate\Console\Scheduling\ManagesFrequencies;
use InvalidArgumentException;
use ReflectionClass;
use ReflectionMethod;
readonly class ScheduleOption
{
public function __construct(
public string $method,
public array $arguments = [],
) {
$this->validateMethod($this->method);
}
private function validateMethod(string $method): void
{
$methods = array_merge(
$this->getMethods(ManagesAttributes::class),
$this->getMethods(ManagesFrequencies::class),
$this->getMethods(Event::class),
);
if (!in_array($method, $methods, true)) {
$methods = implode("\n", $methods);
throw new InvalidArgumentException("Unknown schedule method `{$method}`.\nAllowed methods:\n{$methods}");
}
}
private function getMethods(string $class): array
{
$schedulePublicMethods = new ReflectionClass($class)->getMethods(ReflectionMethod::IS_PUBLIC);
return array_map(fn (ReflectionMethod $method) => $method->getName(), $schedulePublicMethods);
}
}