Completed
Push — master ( cdbec6...220d3b )
by Anton
01:25
created

Controllers/Api/Requirements/Delete/Action.php (1 issue)

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
/*
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\Requirements\Delete;
15
16
use Cog\Contracts\Paket\Job\Repositories\Job as JobRepositoryContract;
17
use Cog\Laravel\Paket\Job\Entities\Job;
18
use Cog\Laravel\Paket\Process\Entities\Process;
19
use Cog\Laravel\Paket\Requirement\Entities\Requirement;
20
use Cog\Laravel\Paket\Requirement\Events\RequirementUninstalling;
21
use Illuminate\Contracts\Support\Responsable as ResponsableContract;
22
use Illuminate\Support\Arr;
23
use Illuminate\Support\Carbon;
24
use MCStreetguy\ComposerParser\Factory as ComposerParser;
25
use Ramsey\Uuid\Uuid;
26
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
27
28
final class Action
29
{
30
    public function __invoke(string $uid, JobRepositoryContract $jobs): ResponsableContract
31
    {
32
        $installedRequirements = $this->getInstalledRequirements();
33
        $requirementName = base64_decode($uid);
34
35
        $installedRequirement = Arr::first($installedRequirements, function (array $value) use ($requirementName) {
36
            return $value['name'] === $requirementName;
37
        });
38
39
        if (is_null($installedRequirement)) {
40
            throw new NotFoundHttpException();
41
        }
42
43
        $requirement = Requirement::fromArray($installedRequirement);
44
45
        $job = new Job(
46
            'ComposerUninstall',
47
            Uuid::uuid4()->toString(),
48
            'Waiting',
49
            Carbon::now(),
50
            new Process(),
51
            $requirement
52
        );
53
54
        $jobs->store($job);
55
56
        event(new RequirementUninstalling($requirement, $job));
57
58
        return new Response($requirement, $job);
59
    }
60
61 View Code Duplication
    private function getInstalledRequirements(): array
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
62
    {
63
        $lockFile = ComposerParser::parseLockfile(base_path('composer.lock'));
64
65
        $packages = $lockFile->getPackages()->getData();
66
        $devPackages = $lockFile->getPackagesDev()->getData();
67
        $platform = $lockFile->getPlatform()->getData();
68
        $devPlatform = $lockFile->getPlatformDev()->getData();
69
        foreach ($packages as &$package) {
70
            $package['isDevelopment'] = false;
71
        }
72
        foreach ($devPackages as &$package) {
73
            $package['isDevelopment'] = true;
74
        }
75
        $platforms = [];
76
        foreach ($platform as $name => $version) {
77
            $platforms[] = [
78
                'name' => $name,
79
                'version' => $version,
80
                'isDevelopment' => false,
81
            ];
82
        }
83
        foreach ($devPlatform as $name => $version) {
84
            $platforms[] = [
85
                'name' => $name,
86
                'version' => $version,
87
                'isDevelopment' => true,
88
            ];
89
        }
90
91
        return array_merge($packages, $devPackages, $platforms);
92
    }
93
}
94