Executor::downloadPackage()   A
last analyzed

Complexity

Conditions 4
Paths 2

Size

Total Lines 21
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 15
nc 2
nop 7
dl 0
loc 21
rs 9.7666
c 1
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\DependencyResolver\Operation\UninstallOperation;
12
use Composer\Installer\InstallationManager;
13
14
class Executor
15
{
16
    public function repositoryWrite($repository, $installationManager, $isDevMode)
17
    {
18
        if (version_compare(\Composer\Composer::VERSION, '2.0', '<')) {
19
            $repository->write();
20
            return;
21
        }
22
23
        $repository->write($isDevMode, $installationManager);
24
    }
25
26
    public function downloadPackage($downloader, $package, $source, $destDir, $errorHandler, &$patchData, &$errors)
27
    {
28
        if (version_compare(\Composer\Composer::VERSION, '2.0', '<')) {
29
            $downloader->download($package, $destDir, false);
30
            return;
31
        }
32
33
        $resultPromise = $downloader->download($package, $destDir, null, false);
34
        $resultPromise->then(function ($path) use (&$patchData) {
35
            $patchData[PatchDefinition::PATH] = $path;
36
        }, function (\Exception $exception) use ($source, $errorHandler, &$patchData, &$errors) {
37
            try {
38
                if (!$exception instanceof \Composer\Downloader\TransportException) {
39
                    throw $exception;
40
                }
41
                $patchData[PatchDefinition::STATUS_LABEL] = $errorHandler->handleError($source, $exception);
42
            } catch (\Exception $error) {
43
                $errors[] = $error;
44
                throw $error;
45
            }
46
            $patchData[PatchDefinition::STATUS] = PatchDefinition::STATUS_ERRORS;
47
        });
48
    }
49
50
    public function assignTmpPathForPatchData(&$patchData, $path)
51
    {
52
        if (version_compare(\Composer\Composer::VERSION, '2.0', '<')) {
53
            $patchData[PatchDefinition::PATH] = $path;
54
        }
55
    }
56
57
    public function waitDownloadCompletion(\Composer\Composer $composer)
58
    {
59
        if (version_compare(\Composer\Composer::VERSION, '2.0', '<')) {
60
            return;
61
        }
62
63
        $composer->getLoop()->getHttpDownloader()->wait();
64
    }
65
66
    public function waitForCompletion(\Composer\Composer $composer, array $processes)
67
    {
68
        if (version_compare(\Composer\Composer::VERSION, '2.0', '<')) {
69
            return;
70
        }
71
72
        $composer->getLoop()->wait($processes);
73
    }
74
75
    public function processReinstallOperation(
76
        WritableRepositoryInterface $repository,
77
        InstallationManager $installationManager,
78
        InstallOperation $installOperation,
79
        UninstallOperation $uninstallOperation
80
    ) {
81
        if (version_compare(\Composer\Composer::VERSION, '2.0', '<')) {
82
            return $installationManager->install($repository, $installOperation);
83
        }
84
85
        return $installationManager
86
            ->uninstall($repository, $uninstallOperation)
87
            ->then(function () use ($installationManager, $installOperation, $repository) {
88
                $package = $installOperation->getPackage();
89
                $installationManager->getInstaller($package->getType())
90
                    ->download($package)
91
                    ->then(function () use ($installationManager, $installOperation, $repository) {
92
                        $installationManager->install($repository, $installOperation);
93
                    });
94
            });
95
    }
96
}
97