Failed Conditions
Pull Request — master (#19)
by Billie
02:57 queued 19s
created

src/SwaggerApiInstaller.php (12 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace PurpleBooth\JaneOpenApiAutogenerate;
4
5
use Composer\Composer;
6
use Composer\Installer\BinaryInstaller;
7
use Composer\Installer\LibraryInstaller;
8
use Composer\IO\IOInterface;
9
use Composer\Package\PackageInterface;
10
use Composer\Util\Filesystem;
11
use Joli\Jane\OpenApi\JaneOpenApi;
12
use RecursiveDirectoryIterator;
13
use RecursiveIteratorIterator;
14
use SplFileInfo;
15
16
class SwaggerApiInstaller extends LibraryInstaller
17
{
18
    const PACKAGE_TYPE = 'swagger-api';
0 ignored issues
show
Equals sign not aligned with surrounding assignments; expected 20 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
19
    const EXTRA_KEY_NAMESPACE = 'namespace';
0 ignored issues
show
Equals sign not aligned with surrounding assignments; expected 13 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
20
    const EXTRA_KEY_SCHEMA_FILE = 'schema-file';
0 ignored issues
show
Equals sign not aligned with surrounding assignments; expected 11 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
21
    const EXTRA_KEY_ENVIRONMENT_VARIABLE = 'environment-variable';
0 ignored issues
show
Equals sign not aligned with surrounding assignments; expected 2 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
22
    const GENERATED_DIRECTORY = 'generated';
0 ignored issues
show
Equals sign not aligned with surrounding assignments; expected 13 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
23
    const SCHEMA_PATH_IS_DOWNLOAD_PATTERN = '/^https?:/';
24
25
    /**
26
     * {@inheritdoc}
27
     */
28
    public function __construct(
29
        IOInterface $inputOutput,
30
        Composer $composer,
31
        $type = self::PACKAGE_TYPE,
32
        Filesystem $filesystem = null,
33
        BinaryInstaller $binaryInstaller = null
34
    ) {
35
        parent::__construct($inputOutput, $composer, $type, $filesystem, $binaryInstaller);
36
    }
37
38
    /**
39
     * {@inheritdoc}.
40
     */
41
    protected function updateCode(PackageInterface $initial, PackageInterface $target)
42
    {
43
        $this->removeCode($initial);
44
        $this->installCode($target);
45
    }
46
47
    /**
48
     * {@inheritdoc}.
49
     */
50
    protected function removeCode(PackageInterface $package)
51
    {
52
        // Is this a schema we download from the internet or checkout locally
53
        if (!$this->isSchemaToDownload($package)) {
54
            parent::removeCode($package);
55
        } else {
56
            $downloadPath = $this->getInstallPath($package);
57
            $this->removeDirectory($downloadPath);
58
        }
59
    }
60
61
    /**
62
     * Do we download or checkout this schema.
63
     *
64
     * @param PackageInterface $package
65
     *
66
     * @return bool
67
     */
68
    private function isSchemaToDownload(PackageInterface $package)
69
    {
70
        return preg_match(self::SCHEMA_PATH_IS_DOWNLOAD_PATTERN, $this->getInstallPath($package)) === 1;
71
    }
72
73
    /**
74
     * RM -rf in PHP.
75
     *
76
     * @param string $downloadPath
77
     */
78
    private function removeDirectory($downloadPath)
79
    {
80
        if (!(new SplFileInfo($downloadPath))->isDir()) {
81
            return;
82
        }
83
84
85
        $directory = new RecursiveDirectoryIterator($downloadPath);
86
        $iterator = new RecursiveIteratorIterator(
0 ignored issues
show
Equals sign not aligned with surrounding assignments; expected 2 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
87
            -$directory,
88
            RecursiveIteratorIterator::CHILD_FIRST
89
        );
90
91
        /** @var SplFileInfo $item */
92
        foreach ($iterator as $item) {
93
            if ($item->isFile()) {
94
                unlink($item->getPathname());
95
            }
96
97
            if ($item->isDir()) {
98
                rmdir($item->getPathname());
99
            }
100
        }
101
102
        rmdir($downloadPath);
103
    }
104
105
    /**
106
     * {@inheritdoc}.
107
     */
108
    protected function installCode(PackageInterface $package)
109
    {
110
        // Is this a schema we download from the internet or checkout locally
111
        if (!$this->isSchemaToDownload($package)) {
112
            parent::installCode($package);
113
        }
114
115
        $this->generateSwaggerClient(
116
            $package,
117
            $this->getInstallPath($package)
118
        );
119
    }
120
121
    /**
122
     * Generate the client for a package.
123
     *
124
     * @param PackageInterface $package
125
     * @param string           $downloadPath
126
     */
127
    protected function generateSwaggerClient(PackageInterface $package, $downloadPath)
128
    {
129
        $janeOpenApi = JaneOpenApi::build();
130
131
        $extra = $package->getExtra();
0 ignored issues
show
Equals sign not aligned with surrounding assignments; expected 13 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
132
        $namespace = $extra[self::EXTRA_KEY_NAMESPACE];
0 ignored issues
show
Equals sign not aligned with surrounding assignments; expected 9 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
133
        $openApiSchemaFile = $this->getSchemaFile($package);
134
135
        $this->io->write(
136
            "Generating <info>$namespace</info> from <info>$openApiSchemaFile</info>",
137
            true,
138
            IOInterface::VERBOSE
139
        );
140
141
        $this->io->write(
142
            "Writing to <info>$downloadPath</info>",
143
            true,
144
            IOInterface::VERY_VERBOSE
145
        );
146
147
        $generatePath = implode(DIRECTORY_SEPARATOR, [$downloadPath, self::GENERATED_DIRECTORY]);
148
        $files = $janeOpenApi->generate($openApiSchemaFile, $namespace, $generatePath);
0 ignored issues
show
Equals sign not aligned with surrounding assignments; expected 8 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
149
        $janeOpenApi->printFiles($files, $downloadPath);
150
151
        foreach ($files as $file) {
152
            $this->io->write("Generated <info>{$file->getFilename()}</info>", true, IOInterface::DEBUG);
153
        }
154
155
        $this->io->write(
156
            "Generated <info>$namespace</info> from <info>$openApiSchemaFile</info>",
157
            true,
158
            IOInterface::VERBOSE
159
        );
160
    }
161
162
    /**
163
     * Get the schema file.
164
     *
165
     * @param PackageInterface $package
166
     *
167
     * @return string
168
     */
169
    private function getSchemaFile(PackageInterface $package)
170
    {
171
        $downloadPath = $this->getInstallPath($package);
0 ignored issues
show
Equals sign not aligned with surrounding assignments; expected 6 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
172
        $extra = $package->getExtra();
0 ignored issues
show
Equals sign not aligned with surrounding assignments; expected 13 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
173
        $openApiSchemaFile = $extra[self::EXTRA_KEY_SCHEMA_FILE];
174
175
        if (isset($extra[self::EXTRA_KEY_ENVIRONMENT_VARIABLE])) {
176
            $envVariableName = $extra[self::EXTRA_KEY_ENVIRONMENT_VARIABLE];
177
            $envVariable = getenv($envVariableName);
0 ignored issues
show
Equals sign not aligned with surrounding assignments; expected 5 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
178
179
            if ($envVariable) {
180
                $openApiSchemaFile = $envVariable;
181
            }
182
        }
183
184
        $vendorSchemaPath = implode(DIRECTORY_SEPARATOR, [$downloadPath, $openApiSchemaFile]);
185
186
        if (file_exists($vendorSchemaPath)) {
187
            $openApiSchemaFile = $vendorSchemaPath;
188
189
            return $openApiSchemaFile;
190
        }
191
192
        return $openApiSchemaFile;
193
    }
194
}
195