Passed
Push — master ( 578d4f...a5a9ff )
by Allan
02:40 queued 12s
created

Executor::waitForCompletion()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 2
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Copyright © Vaimo Group. All rights reserved.
4
 * See LICENSE_VAIMO.txt for license details.
5
 */
6
namespace Vaimo\ComposerPatches\Compatibility;
7
8
use Vaimo\ComposerPatches\Patch\Definition as PatchDefinition;
9
use Composer\Repository\WritableRepositoryInterface;
10
use Composer\DependencyResolver\Operation\InstallOperation;
11
use Composer\Installer\InstallationManager;
12
13
class Executor
14
{
15
    public function repositoryWrite($repository, $installationManager, $isDevMode)
16
    {
17
        if (version_compare(\Composer\Composer::VERSION, '2.0', '<')) {
18
            $repository->write();
19
            return;
20
        }
21
22
        $repository->write($isDevMode, $installationManager);
23
    }
24
25
    public function downloadPackage($downloader, $package, $source, $destDir, $errorHandler, &$patchData, &$errors)
26
    {
27
        if (version_compare(\Composer\Composer::VERSION, '2.0', '<')) {
28
            $downloader->download($package, $destDir, false);
29
            return;
30
        }
31
32
        $resultPromise = $downloader->download($package, $destDir, null, false);
33
        $resultPromise->then(function ($path) use (&$patchData) {
34
            $patchData[PatchDefinition::PATH] = $path;
35
        }, function (\Exception $exception) use ($source, $errorHandler, &$patchData, &$errors) {
36
            try {
37
                if (!$exception instanceof \Composer\Downloader\TransportException) {
38
                    throw $exception;
39
                }
40
                $patchData[PatchDefinition::STATUS_LABEL] = $errorHandler->handleError($source, $exception);
41
            } catch (\Exception $error) {
42
                $errors[] = $error;
43
                throw $error;
44
            }
45
            $patchData[PatchDefinition::STATUS] = PatchDefinition::STATUS_ERRORS;
46
        });
47
    }
48
49
    public function assignTmpPathForPatchData(&$patchData, $path)
50
    {
51
        if (version_compare(\Composer\Composer::VERSION, '2.0', '<')) {
52
            $patchData[PatchDefinition::PATH] = $path;
53
        }
54
    }
55
56
    public function waitDownloadCompletion(\Composer\Composer $composer)
57
    {
58
        if (version_compare(\Composer\Composer::VERSION, '2.0', '<')) {
59
            return;
60
        }
61
62
        $composer->getLoop()->getHttpDownloader()->wait();
63
    }
64
65
    public function waitForCompletion(\Composer\Composer $composer, array $processes)
66
    {
67
        if (version_compare(\Composer\Composer::VERSION, '2.0', '<')) {
68
            return;
69
        }
70
71
        $composer->getLoop()->wait($processes);
72
    }
73
74
    public function processReinstallOperation(
75
        WritableRepositoryInterface $repository,
76
        InstallationManager $installationManager,
77
        InstallOperation $operation
78
    ) {
79
        if (version_compare(\Composer\Composer::VERSION, '2.0', '<')) {
80
            return $installationManager->install($repository, $operation);
81
        }
82
83
        $package = $operation->getPackage();
84
        $installer = $installationManager->getInstaller($package->getType());
85
86
        return $installer
87
            ->download($package)
88
            ->then(function () use ($installationManager, $operation, $repository) {
89
                $installationManager->install($repository, $operation);
90
            });
91
    }
92
}
93