RunSchedulerCommand   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
A configure() 0 6 1
A execute() 0 17 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Jellyfish\Scheduler\Command;
6
7
use Jellyfish\Lock\LockFactoryInterface;
8
use Jellyfish\Lock\LockTrait;
9
use Jellyfish\Scheduler\SchedulerInterface;
10
use Psr\Log\LoggerInterface;
11
use Symfony\Component\Console\Command\Command;
12
use Symfony\Component\Console\Input\InputInterface;
13
use Symfony\Component\Console\Output\OutputInterface;
14
use Throwable;
15
16
class RunSchedulerCommand extends Command
17
{
18
    use LockTrait;
19
20
    public const NAME = 'scheduler:run';
21
    public const DESCRIPTION = 'Run scheduler.';
22
23
    /**
24
     * @var \Jellyfish\Scheduler\SchedulerInterface
25
     */
26
    protected $scheduler;
27
28
    /**
29
     * @var \Psr\Log\LoggerInterface
30
     */
31
    protected $logger;
32
33
    /**
34
     * @param \Jellyfish\Scheduler\SchedulerInterface $scheduler
35
     * @param \Jellyfish\Lock\LockFactoryInterface $lockFactory
36
     * @param \Psr\Log\LoggerInterface $logger
37
     */
38
    public function __construct(
39
        SchedulerInterface $scheduler,
40
        LockFactoryInterface $lockFactory,
41
        LoggerInterface $logger
42
    ) {
43
        parent::__construct();
44
45
        $this->scheduler = $scheduler;
46
        $this->lockFactory = $lockFactory;
47
        $this->logger = $logger;
48
    }
49
50
    /**
51
     * @return void
52
     */
53
    protected function configure(): void
54
    {
55
        parent::configure();
56
57
        $this->setName(static::NAME);
58
        $this->setDescription(static::DESCRIPTION);
59
    }
60
61
    /**
62
     * @param \Symfony\Component\Console\Input\InputInterface $input
63
     * @param \Symfony\Component\Console\Output\OutputInterface $output
64
     *
65
     * @return int|null
66
     */
67
    protected function execute(InputInterface $input, OutputInterface $output): ?int
68
    {
69
        $lockIdentifierParts = [static::NAME];
70
71
        if (!$this->acquire($lockIdentifierParts)) {
72
            return null;
73
        }
74
75
        try {
76
            $this->scheduler->run();
77
        } catch (Throwable $e) {
78
            $this->logger->error($e->getMessage());
79
        } finally {
80
            $this->release();
81
        }
82
83
        return null;
84
    }
85
}
86