MarkJobIncompleteCommand   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 23
dl 0
loc 64
rs 10
c 0
b 0
f 0
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A execute() 0 25 3
A __construct() 0 8 1
A configure() 0 5 1
1
<?php
2
3
/** @noinspection ReturnTypeCanBeDeclaredInspection */
4
5
declare(strict_types=1);
6
7
namespace Setono\SyliusSchedulerPlugin\Command;
8
9
use Setono\SyliusSchedulerPlugin\Doctrine\ORM\JobRepository;
10
use Setono\SyliusSchedulerPlugin\JobManager\JobManager;
11
use Setono\SyliusSchedulerPlugin\Model\JobInterface;
12
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
13
use Symfony\Component\Console\Input\InputArgument;
14
use Symfony\Component\Console\Input\InputInterface;
15
use Symfony\Component\Console\Output\OutputInterface;
16
17
class MarkJobIncompleteCommand extends ContainerAwareCommand
0 ignored issues
show
Deprecated Code introduced by
The class Symfony\Bundle\Framework...d\ContainerAwareCommand has been deprecated: since Symfony 4.2, use {@see Command} instead. ( Ignorable by Annotation )

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

17
class MarkJobIncompleteCommand extends /** @scrutinizer ignore-deprecated */ ContainerAwareCommand
Loading history...
18
{
19
    protected static $defaultName = 'setono:scheduler:mark-incomplete';
20
21
    /**
22
     * @var JobManager
23
     */
24
    private $jobManager;
25
26
    /**
27
     * @var JobRepository
28
     */
29
    private $jobRepository;
30
31
    /**
32
     * @param JobManager $jobManager
33
     * @param JobRepository $jobRepository
34
     */
35
    public function __construct(
36
        JobManager $jobManager,
37
        JobRepository $jobRepository
38
    ) {
39
        parent::__construct();
40
41
        $this->jobManager = $jobManager;
42
        $this->jobRepository = $jobRepository;
43
    }
44
45
    protected function configure()
46
    {
47
        $this
48
            ->setDescription('Internal command (do not use). It marks jobs as incomplete.')
49
            ->addArgument('job-id', InputArgument::REQUIRED, 'The ID of the Job.')
50
        ;
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56
    protected function execute(InputInterface $input, OutputInterface $output)
57
    {
58
        /** @var JobInterface $job */
59
        $job = $this->jobRepository->find(
60
            $input->getArgument('job-id')
61
        );
62
63
        if (!$job instanceof JobInterface) {
0 ignored issues
show
introduced by
$job is always a sub-type of Setono\SyliusSchedulerPlugin\Model\JobInterface.
Loading history...
64
            $output->writeln('<error>Job was not found.</error>');
65
66
            return 1;
67
        }
68
69
        try {
70
            $this->jobManager->closeJob($job, JobInterface::STATE_INCOMPLETE);
71
        } catch (\Exception $e) {
72
            $output->writeln(sprintf(
73
                '<error>Failed to close job: %s</error>',
74
                $e->getMessage()
75
            ));
76
77
            return 1;
78
        }
79
80
        return 0;
81
    }
82
}
83