These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | /* |
||
4 | * This file is part of Laravel Paket. |
||
5 | * |
||
6 | * (c) Anton Komarev <[email protected]> |
||
7 | * |
||
8 | * For the full copyright and license information, please view the LICENSE |
||
9 | * file that was distributed with this source code. |
||
10 | */ |
||
11 | |||
12 | declare(strict_types=1); |
||
13 | |||
14 | namespace Cog\Laravel\Paket\Http\Controllers\Api\Jobs; |
||
15 | |||
16 | use Cog\Contracts\Paket\Job\Repositories\Job as JobRepositoryContract; |
||
17 | use Cog\Contracts\Paket\Requirement\Entities\Requirement as RequirementContract; |
||
18 | use Cog\Laravel\Paket\Job\Entities\Job; |
||
19 | use Cog\Laravel\Paket\Job\Events\JobHasBeenCreated; |
||
20 | use Cog\Laravel\Paket\Process\Entities\Process; |
||
21 | use Cog\Laravel\Paket\Requirement\Entities\Requirement; |
||
22 | use Illuminate\Contracts\Support\Responsable as ResponsableContract; |
||
23 | use Illuminate\Support\Arr; |
||
24 | use Illuminate\Support\Carbon; |
||
25 | use Illuminate\Validation\ValidationException; |
||
26 | use MCStreetguy\ComposerParser\Factory as ComposerParser; |
||
27 | use Ramsey\Uuid\Uuid; |
||
28 | use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
||
29 | |||
30 | final class PostAction |
||
31 | { |
||
32 | private $jobs; |
||
33 | |||
34 | public function __construct(JobRepositoryContract $jobs) |
||
35 | { |
||
36 | $this->jobs = $jobs; |
||
37 | } |
||
38 | |||
39 | public function __invoke(PostRequest $request): ResponsableContract |
||
40 | { |
||
41 | $type = $request->input('type'); |
||
42 | $requirement = Requirement::fromArray($request->input('requirement')); |
||
43 | |||
44 | switch ($type) { |
||
45 | case 'RequirementInstall': |
||
46 | if ($this->isRequirementInstalled($requirement)) { |
||
47 | throw ValidationException::withMessages([ |
||
48 | 'name' => [ |
||
49 | "Package `{$requirement}` already installed", |
||
50 | ], |
||
51 | ]); |
||
52 | } |
||
53 | break; |
||
54 | case 'RequirementUninstall': |
||
55 | $requirement = $this->getInstalledRequirement($requirement); |
||
56 | break; |
||
57 | } |
||
58 | |||
59 | $job = new Job( |
||
60 | $type, |
||
61 | strval(Uuid::uuid4()), |
||
62 | 'Pending', |
||
63 | Carbon::now(), |
||
64 | new Process(), |
||
65 | $requirement |
||
66 | ); |
||
67 | |||
68 | $this->jobs->store($job); |
||
69 | |||
70 | event(new JobHasBeenCreated($job)); |
||
71 | |||
72 | return new PostResponse($job); |
||
73 | } |
||
74 | |||
75 | private function getInstalledRequirements(): array |
||
76 | { |
||
77 | $lockFile = ComposerParser::parseLockfile(base_path('composer.lock')); |
||
78 | |||
79 | $packages = $lockFile->getPackages()->getData(); |
||
80 | $devPackages = $lockFile->getPackagesDev()->getData(); |
||
81 | $platform = $lockFile->getPlatform()->getData(); |
||
82 | $devPlatform = $lockFile->getPlatformDev()->getData(); |
||
83 | foreach ($packages as &$package) { |
||
84 | $package['isDevelopment'] = false; |
||
85 | } |
||
86 | foreach ($devPackages as &$package) { |
||
87 | $package['isDevelopment'] = true; |
||
88 | } |
||
89 | $platforms = []; |
||
90 | foreach ($platform as $name => $version) { |
||
91 | $platforms[] = [ |
||
92 | 'name' => $name, |
||
93 | 'version' => $version, |
||
94 | 'isDevelopment' => false, |
||
95 | ]; |
||
96 | } |
||
97 | foreach ($devPlatform as $name => $version) { |
||
98 | $platforms[] = [ |
||
99 | 'name' => $name, |
||
100 | 'version' => $version, |
||
101 | 'isDevelopment' => true, |
||
102 | ]; |
||
103 | } |
||
104 | |||
105 | return array_merge($packages, $devPackages, $platforms); |
||
106 | } |
||
107 | |||
108 | private function getInstalledRequirement(RequirementContract $requirement): RequirementContract |
||
109 | { |
||
110 | $installedRequirements = $this->getInstalledRequirements(); |
||
111 | |||
112 | $installedRequirement = Arr::first($installedRequirements, function (array $value) use ($requirement) { |
||
0 ignored issues
–
show
|
|||
113 | return $value['name'] === $requirement->getName(); |
||
114 | }); |
||
115 | |||
116 | if (is_null($installedRequirement)) { |
||
117 | throw new NotFoundHttpException(); |
||
118 | } |
||
119 | |||
120 | $requirement = Requirement::fromArray($installedRequirement); |
||
121 | |||
122 | return $requirement; |
||
123 | } |
||
124 | |||
125 | private function isRequirementInstalled(RequirementContract $requirement): bool |
||
126 | { |
||
127 | $installedRequirements = $this->getInstalledRequirements(); |
||
128 | |||
129 | $installedRequirement = Arr::first($installedRequirements, function (array $value) use ($requirement) { |
||
0 ignored issues
–
show
$installedRequirements is of type array , but the function expects a object<Illuminate\Support\iterable> .
It seems like the type of the argument is not accepted by the function/method which you are calling. In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug. We suggest to add an explicit type cast like in the following example: function acceptsInteger($int) { }
$x = '123'; // string "123"
// Instead of
acceptsInteger($x);
// we recommend to use
acceptsInteger((integer) $x);
Loading history...
|
|||
130 | return $value['name'] === $requirement->getName(); |
||
131 | }); |
||
132 | |||
133 | return !is_null($installedRequirement) |
||
134 | && $installedRequirement['version'] === $requirement->getVersion() |
||
135 | && $installedRequirement['isDevelopment'] === $requirement->isDevelopment(); |
||
136 | } |
||
137 | } |
||
138 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: