Issues (62)

Command/GenerateCommand.php (4 issues)

Labels
1
<?php
2
3
namespace Kaliop\eZWorkflowEngineBundle\Command;
4
5
use Symfony\Component\Console\Input\InputArgument;
6
use Symfony\Component\Console\Input\InputInterface;
7
use Symfony\Component\Console\Input\InputOption;
8
use Symfony\Component\Console\Output\OutputInterface;
9
use Symfony\Component\HttpFoundation\File\Exception\FileException;
10
11
class GenerateCommand extends AbstractCommand
12
{
13
    const DIR_CREATE_PERMISSIONS = 0755;
14
15
    private $availableWorkflowFormats = array('yml', 'json');
16
    private $thisBundle = 'EzWorkflowEngineBundle';
17
18
    /**
19
     * Configure the console command
20
     */
21
    protected function configure()
22
    {
23
        $this->setName('kaliop:workflows:generate')
24
            ->setDescription('Generate a blank workflow definition file.')
25
            ->addOption('format', null, InputOption::VALUE_REQUIRED, 'The format of workflow file to generate (' . implode(', ', $this->availableWorkflowFormats) . ')', 'yml')
26
            ->addArgument('bundle', InputArgument::REQUIRED, 'The bundle to generate the workflow definition file in. eg.: AcmeWorkflowsBundle')
27
            ->addArgument('name', InputArgument::OPTIONAL, 'The workflow name (will be prefixed with current date)', null)
28
            ->setHelp(<<<EOT
29
The <info>kaliop:workflows:generate</info> command generates a skeleton workflows definition file:
30
31
    <info>php bin/console kaliop:workflows:generate bundlename</info>
32
EOT
33
            );
34
    }
35
36
    /**
37
     * Run the command and display the results.
38
     *
39
     * @param InputInterface $input
40
     * @param OutputInterface $output
41
     * @return null|int null or 0 if everything went fine, or an error code
42
     * @throws \InvalidArgumentException When an unsupported file type is selected
43
     */
44
    public function execute(InputInterface $input, OutputInterface $output)
45
    {
46
        $bundleName = $input->getArgument('bundle');
47
        $name = $input->getArgument('name');
48
        $fileType = $input->getOption('format');
49
50
        if ($bundleName == $this->thisBundle) {
51
            throw new \InvalidArgumentException("It is not allowed to create workflows in bundle '$bundleName'");
52
        }
53
54
        if (!in_array($fileType, $this->availableWorkflowFormats)) {
55
            throw new \InvalidArgumentException('Unsupported workflow file format ' . $fileType);
56
        }
57
58
        $workflowDirectory = $this->getWorkflowDirectory($bundleName);
0 ignored issues
show
It seems like $bundleName can also be of type string[]; however, parameter $bundleName of Kaliop\eZWorkflowEngineB...:getWorkflowDirectory() 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

58
        $workflowDirectory = $this->getWorkflowDirectory(/** @scrutinizer ignore-type */ $bundleName);
Loading history...
59
60
        if (!is_dir($workflowDirectory)) {
61
            $output->writeln(sprintf(
62
                "Workflows directory <info>%s</info> does not exist. I will create it now....",
63
                $workflowDirectory
64
            ));
65
66
            if (mkdir($workflowDirectory, self::DIR_CREATE_PERMISSIONS, true)) {
67
                $output->writeln(sprintf(
68
                    "Workflows directory <info>%s</info> has been created",
69
                    $workflowDirectory
70
                ));
71
            } else {
72
                throw new FileException(sprintf(
73
                    "Failed to create workflows directory %s.",
74
                    $workflowDirectory
75
                ));
76
            }
77
        }
78
79
        $date = date('YmdHis');
80
81
        if ($name == '') {
82
            $name = 'placeholder';
83
        }
84
        $fileName = $date . '_' . $name . '.' . $fileType;
0 ignored issues
show
Are you sure $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

84
        $fileName = $date . '_' . /** @scrutinizer ignore-type */ $name . '.' . $fileType;
Loading history...
85
86
        $path = $workflowDirectory . '/' . $fileName;
87
88
        $warning = $this->generateWorkflowFile($path, $fileType);
0 ignored issues
show
It seems like $fileType can also be of type string[]; however, parameter $fileType of Kaliop\eZWorkflowEngineB...:generateWorkflowFile() 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

88
        $warning = $this->generateWorkflowFile($path, /** @scrutinizer ignore-type */ $fileType);
Loading history...
89
90
        $output->writeln(sprintf("Generated new workflow file: <info>%s</info>", $path));
91
92
        if ($warning != '') {
93
            $output->writeln("<comment>$warning</comment>");
94
        }
95
96
        return 0;
97
    }
98
99
    /**
100
     * Generates a workflow definition file.
101
     *
102
     * @param string $path filename to file to generate (full path)
103
     * @param string $fileType The type of workflow file to generate
104
     * @param array $parameters
105
     * @return string A warning message in case file generation was OK but there was something weird
106
     * @throws \Exception
107
     */
108
    protected function generateWorkflowFile($path, $fileType, $parameters = array())
109
    {
110
        $warning = '';
111
112
        // Generate workflow file by template
113
        $template = 'Workflow.' . $fileType . '.twig';
114
        $templatePath = $this->getApplication()->getKernel()->getBundle($this->thisBundle)->getPath() . '/Resources/views/WorkflowTemplate/';
0 ignored issues
show
The method getKernel() does not exist on Symfony\Component\Console\Application. It seems like you code against a sub-type of Symfony\Component\Console\Application such as Symfony\Bundle\FrameworkBundle\Console\Application. ( Ignorable by Annotation )

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

114
        $templatePath = $this->getApplication()->/** @scrutinizer ignore-call */ getKernel()->getBundle($this->thisBundle)->getPath() . '/Resources/views/WorkflowTemplate/';
Loading history...
115
        if (!is_file($templatePath . $template)) {
116
            throw new \Exception("Generation of a workflow example is not supported with format '$fileType'");
117
        }
118
119
        $code = $this->getContainer()->get('twig')->render($this->thisBundle . ':WorkflowTemplate:' . $template, $parameters);
120
121
        file_put_contents($path, $code);
122
123
        return $warning;
124
    }
125
126
    /**
127
     * @param string $bundleName a bundle name or filesystem path to a directory
128
     * @return string
129
     */
130
    protected function getWorkflowDirectory($bundleName)
131
    {
132
        // Allow direct usage of a directory path instead of a bundle name
133
        if (strpos($bundleName, '/') !== false && is_dir($bundleName)) {
134
            return rtrim($bundleName, '/');
135
        }
136
137
        $activeBundles = array();
138
        foreach ($this->getApplication()->getKernel()->getBundles() as $bundle) {
139
            $activeBundles[] = $bundle->getName();
140
        }
141
        asort($activeBundles);
142
        if (!in_array($bundleName, $activeBundles)) {
143
            throw new \InvalidArgumentException("Bundle '$bundleName' does not exist or it is not enabled. Try with one of:\n" . implode(', ', $activeBundles));
144
        }
145
146
        $bundle = $this->getApplication()->getKernel()->getBundle($bundleName);
147
        $workflowDirectory = $bundle->getPath() . '/' . $this->getContainer()->get('ez_migration_bundle.helper.config.resolver')->getParameter('ez_workflowengine_bundle.workflow_directory');
148
149
        return $workflowDirectory;
150
    }
151
}
152