LinkedPackageFactory   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
eloc 30
c 1
b 0
f 0
dl 0
loc 59
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A fromPath() 0 18 3
A loadFromJsonFile() 0 25 3
A __construct() 0 6 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the composer-link plugin.
7
 *
8
 * Copyright (c) 2021-2022 Sander Visser <[email protected]>.
9
 *
10
 * For the full copyright and license information, please view the LICENSE.md
11
 * file that was distributed with this source code.
12
 *
13
 * @link https://github.com/SanderSander/composer-link
14
 */
15
16
namespace ComposerLink;
17
18
use Composer\Installer\InstallationManager;
19
use Composer\Json\JsonFile;
20
use Composer\Package\CompletePackage;
21
use Composer\Package\Loader\ArrayLoader;
22
use Composer\Repository\InstalledRepositoryInterface;
23
use RuntimeException;
24
25
class LinkedPackageFactory
26
{
27
    protected InstallationManager $installationManager;
28
29
    protected InstalledRepositoryInterface $installedRepository;
30
31
    public function __construct(
32
        InstallationManager $installationManager,
33
        InstalledRepositoryInterface $installedRepository
34
    ) {
35
        $this->installationManager = $installationManager;
36
        $this->installedRepository = $installedRepository;
37
    }
38
39
    private function loadFromJsonFile(string $path): CompletePackage
40
    {
41
        if (!file_exists($path . DIRECTORY_SEPARATOR . 'composer.json')) {
42
            throw new RuntimeException(sprintf('No composer.json file found in "%s".', $path));
43
        }
44
45
        $json = (new JsonFile($path . DIRECTORY_SEPARATOR . 'composer.json'))->read();
46
47
        if (!is_array($json)) {
48
            throw new RuntimeException(sprintf('Unable to read composer.json in "%s"', $path));
49
        }
50
51
        $json['version'] = 'dev-master';
52
53
        // branch alias won't work, otherwise the ArrayLoader::load won't return an instance of CompletePackage
54
        unset($json['extra']['branch-alias']);
55
56
        $loader = new ArrayLoader();
57
        /** @var CompletePackage $package */
58
        $package = $loader->load($json);
59
        $package->setDistUrl($path);
60
        $package->setInstallationSource('dist');
61
        $package->setDistType('path');
62
63
        return $package;
64
    }
65
66
    public function fromPath(string $path): LinkedPackage
67
    {
68
        $originalPackage = null;
69
        $newPackage = $this->loadFromJsonFile($path);
70
        $packages = $this->installedRepository->getCanonicalPackages();
71
        foreach ($packages as $package) {
72
            if ($package->getName() === $newPackage->getName()) {
73
                $originalPackage = $package;
74
            }
75
        }
76
77
        $destination = $this->installationManager->getInstallPath($newPackage);
78
79
        return new LinkedPackage(
80
            $path,
81
            $newPackage,
82
            $originalPackage,
83
            $destination
84
        );
85
    }
86
}
87