ImportModuleCommand::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 4
rs 10
1
<?php
2
3
namespace PiouPiou\RibsAdminBundle\Command;
4
5
use Doctrine\ORM\EntityManagerInterface;
6
use PiouPiou\RibsAdminBundle\Entity\Module;
7
use Symfony\Component\Console\Command\Command;
8
use Symfony\Component\Console\Input\InputArgument;
9
use Symfony\Component\Console\Input\InputInterface;
10
use Symfony\Component\Console\Output\OutputInterface;
11
use Symfony\Component\Process\Exception\ProcessFailedException;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Proces...\ProcessFailedException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
use Symfony\Component\Process\Process;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Process\Process was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
14
class ImportModuleCommand extends Command
15
{
16
    private $em;
17
18
    /**
19
     * ImportModuleCommand constructor.
20
     * @param EntityManagerInterface $em
21
     * @param string|null $name
22
     */
23
    public function __construct(EntityManagerInterface $em, string $name = null)
24
    {
25
        parent::__construct($name);
26
        $this->em = $em;
27
    }
28
29
    protected function configure()
30
    {
31
        $this
32
            ->setName('ribsadmin:import-module')
33
            ->setDescription('Import a module in ribs admin')
34
            ->addArgument(
35
                'package-name',
36
                InputArgument::REQUIRED,
37
                'Name of composer package to import'
38
            )
39
            ->addArgument(
40
                'module-name',
41
                InputArgument::REQUIRED,
42
                'Name of package to display in app'
43
            )
44
        ;
45
    }
46
47
    protected function execute(InputInterface $input, OutputInterface $output)
48
    {
49
        $package_name = $input->getArgument('package-name');
50
        $output->writeln("Start composer require " . $package_name);
0 ignored issues
show
Bug introduced by
Are you sure $package_name of type null|string|string[] can be used in concatenation? ( Ignorable by Annotation )

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

50
        $output->writeln("Start composer require " . /** @scrutinizer ignore-type */ $package_name);
Loading history...
51
52
        $process = Process::fromShellCommandline("composer require " . $package_name);
53
        $process->run(function ($type, $buffer) {
54
            echo $buffer;
55
        });
56
57
        if (!$process->isSuccessful()) {
58
            throw new ProcessFailedException($process);
59
        }
60
61
        $output->writeln("End composer require " . $package_name);
62
        $output->writeln("Start insertion of module in database " . $package_name);
63
64
        $module = new Module();
65
        $module->setPackageName($package_name);
0 ignored issues
show
Bug introduced by
It seems like $package_name can also be of type null and string[]; however, parameter $package_name of PiouPiou\RibsAdminBundle...odule::setPackageName() 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

65
        $module->setPackageName(/** @scrutinizer ignore-type */ $package_name);
Loading history...
66
        $module->setTitle($input->getArgument('module-name'));
0 ignored issues
show
Bug introduced by
It seems like $input->getArgument('module-name') can also be of type null and string[]; however, parameter $title of PiouPiou\RibsAdminBundle\Entity\Module::setTitle() 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

66
        $module->setTitle(/** @scrutinizer ignore-type */ $input->getArgument('module-name'));
Loading history...
67
        $module->setActive(false);
68
        $module->setDisplayed(true);
69
        $this->em->persist($module);
70
        $this->em->flush();
71
72
        $output->writeln("Installation of " . $package_name . " is finished. You have now to configure this module in your administration interface");
73
74
        return 0;
75
    }
76
}
77