Completed
Push — master ( 43792a...dc88f4 )
by Anton
12s queued 11s
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\Repositories;
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
        $repositories = $this->getRepositories();
24
25
        return new CollectResponse($repositories);
26
    }
27
28
    private function getRepositories(): array
29
    {
30
        $jsonFile = ComposerParser::parseComposerJson(base_path('composer.json'));
31
32
        $repositories = $jsonFile->getRepositories();
33
        $output = [];
34
35
        foreach ($repositories as $key => $repository) {
36
            $package = $repository->getPackage();
37
38
            $output[$key] = [
39
                'type' => $repository->getType(),
40
                'url' => $repository->getUrl(),
41
                'options' => $repository->getOptions(),
42
                'package' => is_null($package) ? null : $package->toArray(),
43
            ];
44
        }
45
46
        $output = $this->addDefaultRepository($output);
47
48
        return $output;
49
    }
50
51
    // TODO: Remove after fix: https://github.com/MCStreetguy/ComposerParser/issues/4
52
    private function addDefaultRepository(array $repositories): array
53
    {
54
        $isPackagistOrgDisabled = false;
55
        foreach ($repositories as $key => $repository) {
56
            // This hacky temporary solution the only way to determine `{"packagist.org": false}` repository object
57
            // But in fact it will display that packagist ignored even with any empty object
58
            if ($repository['type'] === '' && $repository['url'] === '' && $repository['options'] === [] && is_null($repository['package'])) {
59
                $isPackagistOrgDisabled = true;
60
                unset($repositories[$key]);
61
            }
62
        }
63
64
        if ($isPackagistOrgDisabled) {
65
            return $repositories;
66
        }
67
68
        $defaultRepository = [
69
            'type' => 'composer',
70
            'url' => 'https?://repo.packagist.org',
71
            'allow_ssl_downgrade' => true,
72
        ];
73
74
        if (!isset($repositories['packagist.org'])) {
75
            $repositories['packagist.org'] = [];
76
        }
77
78
        $repositories['packagist.org'] = array_merge($defaultRepository, $repositories['packagist.org']);
79
80
        return $repositories;
81
    }
82
}
83