JobExampleFactory::create()   F
last analyzed

Complexity

Conditions 16
Paths 8192

Size

Total Lines 77
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 16
eloc 32
nc 8192
nop 1
dl 0
loc 77
rs 1.4
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\Doctrine\ORM\ScheduleRepository;
9
use Setono\SyliusSchedulerPlugin\Factory\JobFactory;
10
use Setono\SyliusSchedulerPlugin\Model\JobInterface;
11
use Sylius\Bundle\CoreBundle\Fixture\Factory\AbstractExampleFactory;
12
use Sylius\Bundle\CoreBundle\Fixture\OptionsResolver\LazyOption;
13
use Symfony\Component\OptionsResolver\Options;
14
use Symfony\Component\OptionsResolver\OptionsResolver;
15
16
class JobExampleFactory extends AbstractExampleFactory
17
{
18
    /**
19
     * @var ScheduleRepository
20
     */
21
    private $scheduleRepository;
22
23
    /**
24
     * @var JobFactory
25
     */
26
    private $jobFactory;
27
28
    /**
29
     * @var JobRepository
30
     */
31
    private $jobRepository;
32
33
    /**
34
     * @var \Faker\Generator
35
     */
36
    private $faker;
37
38
    /**
39
     * @var OptionsResolver
40
     */
41
    private $optionsResolver;
42
43
    /**
44
     * @param ScheduleRepository $scheduleRepository
45
     * @param JobFactory $jobFactory
46
     * @param JobRepository $jobRepository
47
     */
48
    public function __construct(
49
        ScheduleRepository $scheduleRepository,
50
        JobFactory $jobFactory,
51
        JobRepository $jobRepository
52
    ) {
53
        $this->scheduleRepository = $scheduleRepository;
54
        $this->jobFactory = $jobFactory;
55
        $this->jobRepository = $jobRepository;
56
57
        $this->faker = \Faker\Factory::create();
58
        $this->optionsResolver = new OptionsResolver();
59
60
        $this->configureOptions($this->optionsResolver);
61
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66
    public function create(array $options = []): JobInterface
67
    {
68
        $options = $this->optionsResolver->resolve($options);
69
70
        /** @var JobInterface $job */
71
        $job = $this->jobFactory->createNew();
72
73
        if (isset($options['schedule'])) {
74
            $job->setSchedule($options['schedule']);
75
        }
76
        $job->setCommand($options['command']);
77
78
        if (isset($options['args'])) {
79
            $job->setArgs($options['args']);
80
        }
81
        if (isset($options['state'])) {
82
            $job->setState($options['state']);
83
        }
84
        if (isset($options['queue'])) {
85
            $job->setQueue($options['queue']);
86
        }
87
        if (isset($options['priority'])) {
88
            $job->setPriority((int) $options['priority']);
89
        }
90
//        if (isset($options['created_at'])) {
91
//            $job->setCreatedAt(new \DateTime($options['created_at']));
92
//        }
93
//        if (isset($options['started_at'])) {
94
//            $job->setStartedAt(new \DateTime($options['started_at']));
95
//        }
96
//        if (isset($options['checked_at'])) {
97
//            $job->setCheckedAt(new \DateTime($options['checked_at']));
98
//        }
99
//        if (isset($options['closed_at'])) {
100
//            $job->setClosedAt(new \DateTime($options['closed_at']));
101
//        }
102
        if (isset($options['execute_after'])) {
103
            $job->setExecuteAfter(new \DateTime($options['execute_after']));
104
        }
105
106
//        /** @var JobInterface $dependencyJob */
107
//        foreach ($options['dependencies'] as $dependencyJob) {
108
//            $job->addDependency($dependencyJob);
109
//        }
110
111
        if (isset($options['worker_name'])) {
112
            $job->setWorkerName($options['worker_name']);
113
        }
114
        if (isset($options['output'])) {
115
            $job->setOutput($options['output']);
116
        }
117
        if (isset($options['error_output'])) {
118
            $job->setErrorOutput($options['error_output']);
119
        }
120
        if (isset($options['exit_code'])) {
121
            $job->setExitCode($options['exit_code']);
122
        }
123
        if (isset($options['max_runtime'])) {
124
            $job->setMaxRuntime($options['max_runtime']);
125
        }
126
        if (isset($options['max_retries'])) {
127
            $job->setMaxRetries($options['max_retries']);
128
        }
129
130
//        if (isset($options['original_job'])) {
131
//            $job->setOriginalJob($options['original_job']);
132
//        }
133
134
        if (isset($options['retry_jobs']) && is_int($options['retry_jobs'])) {
135
            /** @var JobInterface $retryJob */
136
            for ($i = 0; $i < $options['retry_jobs']; ++$i) {
137
                $retryJob = $this->jobFactory->createRetryJob($job);
138
                $job->addRetryJob($retryJob);
139
            }
140
        }
141
142
        return $job;
143
    }
144
145
    /**
146
     * {@inheritdoc}
147
     */
148
    protected function configureOptions(OptionsResolver $resolver): void
149
    {
150
        $resolver
151
            ->setDefined('schedule')
152
            ->setNormalizer('schedule', LazyOption::findOneBy($this->scheduleRepository, 'code'))
153
            ->setAllowedTypes('schedule', ['null', 'string'])
154
155
            ->setDefined('command')
156
            ->setAllowedTypes('command', 'string')
157
158
            ->setDefined('args')
159
            ->setDefault('args', [])
160
            ->setAllowedTypes('args', 'array')
161
162
            ->setDefined('state')
163
            ->setAllowedTypes('state', ['null', 'string'])
164
165
            ->setDefined('queue')
166
            ->setAllowedTypes('queue', ['null', 'string'])
167
168
            ->setDefault('priority', function (Options $options): int {
0 ignored issues
show
Unused Code introduced by
The parameter $options is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

168
            ->setDefault('priority', function (/** @scrutinizer ignore-unused */ Options $options): int {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
169
                return $this->faker->randomElement([
170
                    JobInterface::PRIORITY_LOW,
171
                    JobInterface::PRIORITY_DEFAULT,
172
                    JobInterface::PRIORITY_HIGH,
173
                ]);
174
            })
175
            ->setAllowedTypes('priority', ['int', 'null'])
176
177
//            ->setDefined('created_at')
178
//            ->setAllowedTypes('created_at', ['null', 'string'])
179
//
180
//            ->setDefined('started_at')
181
//            ->setAllowedTypes('started_at', ['null', 'string'])
182
//
183
//            ->setDefined('checked_at')
184
//            ->setAllowedTypes('checked_at', ['null', 'string'])
185
//
186
//            ->setDefined('closed_at')
187
//            ->setAllowedTypes('closed_at', ['null', 'string'])
188
189
            ->setDefined('execute_after')
190
            ->setAllowedTypes('execute_after', ['null', 'string'])
191
192
//            // @todo Decide how to identify
193
//            ->setNormalizer('dependencies', LazyOption::findBy($this->jobRepository, '...'))
194
//            ->setAllowedTypes('dependencies', 'array')
195
196
            ->setDefined('worker_name')
197
            ->setAllowedTypes('worker_name', ['null', 'string'])
198
199
            ->setDefined('output')
200
            ->setAllowedTypes('output', ['null', 'string'])
201
202
            ->setDefined('error_output')
203
            ->setAllowedTypes('error_output', ['null', 'string'])
204
205
            ->setDefined('exit_code')
206
            ->setAllowedTypes('exit_code', ['null', 'int'])
207
208
            ->setDefined('max_runtime')
209
            ->setAllowedTypes('max_runtime', ['null', 'int'])
210
211
            ->setDefined('max_retries')
212
            ->setAllowedTypes('max_retries', ['null', 'int'])
213
214
//            // @todo Decide how to identify
215
//            ->setNormalizer('retry_jobs', LazyOption::findOneBy($this->jobRepository, '...'))
216
//            ->setAllowedTypes('original_job', ['null', 'object'])
217
218
            ->setDefined('retry_jobs')
219
            ->setNormalizer('retry_jobs', function ($options) {
0 ignored issues
show
Unused Code introduced by
The parameter $options is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

219
            ->setNormalizer('retry_jobs', function (/** @scrutinizer ignore-unused */ $options) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
220
                return $this->faker->numberBetween(0, 4);
221
            })
222
            ->setAllowedTypes('retry_jobs', 'int')
223
        ;
224
    }
225
}
226