JobFactory::createFromSchedule()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 2
dl 0
loc 10
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Setono\SyliusSchedulerPlugin\Factory;
6
7
use Setono\SyliusSchedulerPlugin\Model\JobInterface;
8
use Setono\SyliusSchedulerPlugin\Model\ScheduleInterface;
9
use Sylius\Component\Resource\Factory\FactoryInterface;
10
11
class JobFactory implements JobFactoryInterface
12
{
13
    /**
14
     * @var FactoryInterface
15
     */
16
    private $factory;
17
18
    /**
19
     * @param FactoryInterface $factory
20
     */
21
    public function __construct(
22
        FactoryInterface $factory
23
    ) {
24
        $this->factory = $factory;
25
    }
26
27
    /**
28
     * {@inheritdoc}
29
     */
30
    public function createNew()
31
    {
32
        return $this->factory->createNew();
33
    }
34
35
    /**
36
     * {@inheritdoc}
37
     */
38
    public function createRetryJob(JobInterface $job): JobInterface
39
    {
40
        /** @var JobInterface $retryJob */
41
        $retryJob = $this->createNew();
42
        $retryJob->setState(JobInterface::STATE_PENDING);
43
        $retryJob->setOriginalJob($job);
44
        $retryJob->setSchedule($job->getSchedule());
45
        $retryJob->setCommand($job->getCommand());
0 ignored issues
show
Bug introduced by
It seems like $job->getCommand() can also be of type null; however, parameter $command of Setono\SyliusSchedulerPl...Interface::setCommand() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

45
        $retryJob->setCommand(/** @scrutinizer ignore-type */ $job->getCommand());
Loading history...
46
        $retryJob->setArgs($job->getArgs());
47
48
        $retryJob->setQueue($job->getQueue());
49
        $retryJob->setPriority($job->getPriority());
50
51
        $retryJob->setMaxRuntime($job->getMaxRuntime());
52
53
        return $retryJob;
54
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59
    public function createFromSchedule(ScheduleInterface $schedule, $currentTime = 'now'): JobInterface
60
    {
61
        /** @var JobInterface $job */
62
        $job = $this->createNew();
63
        $job->setSchedule($schedule);
64
        $job->setCommand($schedule->getCommand());
0 ignored issues
show
Bug introduced by
It seems like $schedule->getCommand() can also be of type null; however, parameter $command of Setono\SyliusSchedulerPl...Interface::setCommand() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

64
        $job->setCommand(/** @scrutinizer ignore-type */ $schedule->getCommand());
Loading history...
65
        $job->setArgs($schedule->getArgs());
66
        $job->setExecuteAfter($schedule->getNextRunDate($currentTime));
67
68
        return $job;
69
    }
70
}
71