Completed
Push — master ( d244a1...8fd770 )
by Anton
01:17
created

CollectAction::__invoke()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
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;
15
16
use Illuminate\Contracts\Support\Responsable as ResponsableContract;
17
use MCStreetguy\ComposerParser\Factory as ComposerParser;
18
19
final class CollectAction
20
{
21
    public function __invoke(): ResponsableContract
22
    {
23
        $requirements = $this->getInstalledRequirements();
24
25
        return new CollectResponse($requirements);
26
    }
27
28
    private function getInstalledRequirements(): array
29
    {
30
        $lockFile = ComposerParser::parseLockfile(base_path('composer.lock'));
31
        $jsonFile = ComposerParser::parseComposerJson(base_path('composer.json'));
32
33
        $packages = $lockFile->getPackages()->getData();
34
        $devPackages = $lockFile->getPackagesDev()->getData();
35
        $platform = $lockFile->getPlatform()->getData();
36
        $devPlatform = $lockFile->getPlatformDev()->getData();
37
38
        foreach ($packages as &$package) {
39
            $package['isDevelopment'] = false;
40
        }
41
        foreach ($devPackages as &$package) {
42
            $package['isDevelopment'] = true;
43
        }
44
45
        $requires = $jsonFile->getRequire()->getData();
46
        $devRequires = $jsonFile->getRequireDev()->getData();
47
48
        $roots = [
49
            'essential' => [],
50
            'dev' => [],
51
        ];
52
        foreach ($platform as $name => $version) {
53
            $roots['essential'][] = [
54
                'name' => $name,
55
                'version' => $version,
56
            ];
57
        }
58
        foreach ($devPlatform as $name => $version) {
59
            $roots['dev'][] = [
60
                'name' => $name,
61
                'version' => $version,
62
            ];
63
        }
64 View Code Duplication
        foreach ($packages as $key => $package) {
0 ignored issues
show
Duplication introduced by antonkomarev
This code seems to be duplicated across 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...
65
            if (isset($requires[$package['name']]) || isset($devRequires[$package['name']])) {
66
                $roots['essential'][] = $package;
67
                unset($packages[$key]);
68
            }
69
        }
70 View Code Duplication
        foreach ($devPackages as $key => $package) {
0 ignored issues
show
Duplication introduced by antonkomarev
This code seems to be duplicated across 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...
71
            if (isset($requires[$package['name']]) || isset($devRequires[$package['name']])) {
72
                $roots['dev'][] = $package;
73
                unset($devPackages[$key]);
74
            }
75
        }
76
        $dependencies = [
77
            'essential' => [],
78
            'dev' => [],
79
        ];
80
        foreach ($packages as $key => $package) {
81
            $dependencies['essential'][] = $package;
82
        }
83
        foreach ($devPackages as $key => $package) {
84
            $dependencies['dev'][] = $package;
85
        }
86
87
        return [
88
            'roots' => $roots,
89
            'dependencies' => $dependencies,
90
        ];
91
    }
92
}
93