AsyncChildCommand::configure()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 6
rs 10
1
<?php
2
declare(strict_types=1);
3
4
namespace Async;
5
6
use Exception;
7
use SuperClosure\Serializer;
8
use Symfony\Component\Console\Command\Command;
9
use Symfony\Component\Console\Input\InputArgument;
10
use Symfony\Component\Console\Input\InputInterface;
11
use Symfony\Component\Console\Output\OutputInterface;
12
13
class AsyncChildCommand extends Command
14
{
15
    public const COMMAND_NAME = 'app:run-child-process';
16
    private const PARAM_NAME_JOB = 'job';
17
    private $serializer;
18
19
    public function __construct(?string $name = null)
20
    {
21
        parent::__construct($name);
22
23
        $this->serializer = new Serializer();
24
    }
25
26
    public function execute(InputInterface $input, OutputInterface $output): int
27
    {
28
        try {
29
            $job = $this->serializer->unserialize(base64_decode($input->getArgument(self::PARAM_NAME_JOB)));
0 ignored issues
show
Bug introduced by
It seems like $input->getArgument(self::PARAM_NAME_JOB) can also be of type string[]; however, parameter $data of base64_decode() 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

29
            $job = $this->serializer->unserialize(base64_decode(/** @scrutinizer ignore-type */ $input->getArgument(self::PARAM_NAME_JOB)));
Loading history...
30
31
            ob_start();
32
            $jobResults = $job();
33
            $ob = ob_get_clean();
34
            $error = null;
35
        } catch (Exception $exception) {
36
            $jobResults = null;
37
            $ob = null;
38
            $error = $exception;
39
        }
40
41
        $output->writeln(base64_encode(serialize(new AsyncChildResponse($jobResults, $ob, $error))));
42
43
        return 0;
44
    }
45
46
    protected function configure(): void
47
    {
48
        $this
49
            ->setName(self::COMMAND_NAME)
50
            ->setDescription('Runs a child process.')
51
            ->addArgument(self::PARAM_NAME_JOB, InputArgument::REQUIRED, 'Serialized callback job param.');
52
    }
53
}