|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Setono\SyliusSchedulerPlugin\Fixture\Factory; |
|
6
|
|
|
|
|
7
|
|
|
use Setono\SyliusSchedulerPlugin\Doctrine\ORM\JobRepository; |
|
8
|
|
|
use Setono\SyliusSchedulerPlugin\Factory\ScheduleFactoryInterface; |
|
9
|
|
|
use Setono\SyliusSchedulerPlugin\Model\JobInterface; |
|
10
|
|
|
use Setono\SyliusSchedulerPlugin\Model\ScheduleInterface; |
|
11
|
|
|
use Sylius\Bundle\CoreBundle\Fixture\Factory\AbstractExampleFactory; |
|
12
|
|
|
use Sylius\Component\Core\Formatter\StringInflector; |
|
13
|
|
|
use Symfony\Component\OptionsResolver\Options; |
|
14
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
|
15
|
|
|
|
|
16
|
|
|
class ScheduleExampleFactory extends AbstractExampleFactory |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* @var ScheduleFactoryInterface |
|
20
|
|
|
*/ |
|
21
|
|
|
private $scheduleFactory; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var JobRepository |
|
25
|
|
|
*/ |
|
26
|
|
|
private $scheduleRepository; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var \Faker\Generator |
|
30
|
|
|
*/ |
|
31
|
|
|
private $faker; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var OptionsResolver |
|
35
|
|
|
*/ |
|
36
|
|
|
private $optionsResolver; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @param ScheduleFactoryInterface $scheduleFactory |
|
40
|
|
|
* @param JobRepository $scheduleRepository |
|
41
|
|
|
*/ |
|
42
|
|
|
public function __construct( |
|
43
|
|
|
ScheduleFactoryInterface $scheduleFactory, |
|
44
|
|
|
JobRepository $scheduleRepository |
|
45
|
|
|
) { |
|
46
|
|
|
$this->scheduleFactory = $scheduleFactory; |
|
47
|
|
|
$this->scheduleRepository = $scheduleRepository; |
|
48
|
|
|
|
|
49
|
|
|
$this->faker = \Faker\Factory::create(); |
|
50
|
|
|
$this->optionsResolver = new OptionsResolver(); |
|
51
|
|
|
|
|
52
|
|
|
$this->configureOptions($this->optionsResolver); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* {@inheritdoc} |
|
57
|
|
|
*/ |
|
58
|
|
|
public function create(array $options = []): ScheduleInterface |
|
59
|
|
|
{ |
|
60
|
|
|
$options = $this->optionsResolver->resolve($options); |
|
61
|
|
|
|
|
62
|
|
|
/** @var ScheduleInterface $schedule */ |
|
63
|
|
|
$schedule = $this->scheduleFactory->createNew(); |
|
64
|
|
|
$schedule->setCode($options['code']); |
|
65
|
|
|
$schedule->setName($options['name']); |
|
66
|
|
|
$schedule->setCommand($options['command']); |
|
67
|
|
|
|
|
68
|
|
|
if (isset($options['args'])) { |
|
69
|
|
|
$schedule->setArgs($options['args']); |
|
70
|
|
|
} |
|
71
|
|
|
if (isset($options['queue'])) { |
|
72
|
|
|
$schedule->setQueue($options['queue']); |
|
73
|
|
|
} |
|
74
|
|
|
if (isset($options['priority'])) { |
|
75
|
|
|
$schedule->setPriority((int) $options['priority']); |
|
76
|
|
|
} |
|
77
|
|
|
if (isset($options['created_at'])) { |
|
78
|
|
|
$schedule->setCreatedAt(new \DateTime($options['created_at'])); |
|
79
|
|
|
} |
|
80
|
|
|
if (isset($options['cron_expression'])) { |
|
81
|
|
|
$schedule->setCronExpression($options['cron_expression']); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
return $schedule; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* {@inheritdoc} |
|
89
|
|
|
*/ |
|
90
|
|
|
protected function configureOptions(OptionsResolver $resolver): void |
|
91
|
|
|
{ |
|
92
|
|
|
$resolver |
|
93
|
|
|
->setDefault('name', function (Options $options): string { |
|
|
|
|
|
|
94
|
|
|
return $this->faker->words(3, true); |
|
|
|
|
|
|
95
|
|
|
}) |
|
96
|
|
|
->setDefault('code', function (Options $options): string { |
|
97
|
|
|
return StringInflector::nameToCode($options['name']); |
|
98
|
|
|
}) |
|
99
|
|
|
|
|
100
|
|
|
->setDefined('command') |
|
101
|
|
|
->setAllowedTypes('command', 'string') |
|
102
|
|
|
|
|
103
|
|
|
->setDefined('args') |
|
104
|
|
|
->setDefault('args', []) |
|
105
|
|
|
->setAllowedTypes('args', 'array') |
|
106
|
|
|
|
|
107
|
|
|
->setDefined('queue') |
|
108
|
|
|
->setAllowedTypes('queue', ['null', 'string']) |
|
109
|
|
|
|
|
110
|
|
|
->setDefault('priority', function (Options $options): int { |
|
|
|
|
|
|
111
|
|
|
return $this->faker->randomElement([ |
|
112
|
|
|
JobInterface::PRIORITY_LOW, |
|
113
|
|
|
JobInterface::PRIORITY_DEFAULT, |
|
114
|
|
|
JobInterface::PRIORITY_HIGH, |
|
115
|
|
|
]); |
|
116
|
|
|
}) |
|
117
|
|
|
->setAllowedTypes('priority', ['int', 'null']) |
|
118
|
|
|
|
|
119
|
|
|
->setDefined('created_at') |
|
120
|
|
|
->setAllowedTypes('created_at', ['null', 'string']) |
|
121
|
|
|
|
|
122
|
|
|
->setDefined('cron_expression') |
|
123
|
|
|
->setAllowedTypes('cron_expression', ['null', 'string']) |
|
124
|
|
|
; |
|
125
|
|
|
} |
|
126
|
|
|
} |
|
127
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.