Passed
Pull Request — master (#29)
by Sander
02:17
created

LinkPackages   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 18
c 1
b 0
f 0
dl 0
loc 37
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A execute() 0 19 4
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\Actions;
17
18
use Composer\Repository\RepositoryManager;
19
use ComposerLink\LinkManager;
20
use ComposerLink\Repository\Repository;
21
22
class LinkPackages
23
{
24
    protected RepositoryManager $repositoryManager;
25
26
    protected Repository $repository;
27
28
    protected LinkManager $linkManager;
29
30
    public function __construct(
31
        LinkManager $linkManager,
32
        Repository $repository,
33
        RepositoryManager $repositoryManager
34
    ) {
35
        $this->linkManager = $linkManager;
36
        $this->repository = $repository;
37
        $this->repositoryManager = $repositoryManager;
38
    }
39
40
    public function execute(): void
41
    {
42
        foreach ($this->repository->all() as $linkedPackage) {
43
            if (!$this->linkManager->isLinked($linkedPackage)) {
44
                // Package is updated, so we need to link the newer original package
45
                $oldOriginalPackage = $linkedPackage->getOriginalPackage();
46
                if (!is_null($oldOriginalPackage)) {
47
                    $newOriginalPackage = $this->repositoryManager
48
                        ->getLocalRepository()
49
                        ->findPackage($oldOriginalPackage->getName(), '*');
50
                    $linkedPackage->setOriginalPackage($newOriginalPackage);
51
                    $this->repository->store($linkedPackage);
52
                }
53
54
                $this->linkManager->linkPackage($linkedPackage);
55
            }
56
        }
57
58
        $this->repository->persist();
59
    }
60
}
61