Executor   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 93
Duplicated Lines 0 %

Importance

Changes 5
Bugs 0 Features 0
Metric Value
wmc 17
eloc 44
c 5
b 0
f 0
dl 0
loc 93
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A processReinstallOperation() 0 32 5
A waitDownloadCompletion() 0 7 2
A assignTmpPathForPatchData() 0 4 2
A waitForCompletion() 0 7 2
A repositoryWrite() 0 8 2
A downloadPackage() 0 21 4
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\Exceptions\OperationFailure;
9
use Vaimo\ComposerPatches\Patch\Definition as PatchDefinition;
10
use Composer\Repository\WritableRepositoryInterface;
11
use Composer\DependencyResolver\Operation\InstallOperation;
12
use Composer\DependencyResolver\Operation\UninstallOperation;
13
use Composer\Installer\InstallationManager;
14
15
class Executor
16
{
17
    public function repositoryWrite($repository, $installationManager, $isDevMode)
18
    {
19
        if (version_compare(\Composer\Composer::getVersion(), '2.0', '<')) {
20
            $repository->write();
21
            return;
22
        }
23
24
        $repository->write($isDevMode, $installationManager);
25
    }
26
27
    public function downloadPackage($downloader, $package, $source, $destDir, $errorHandler, &$patchData, &$errors)
28
    {
29
        if (version_compare(\Composer\Composer::getVersion(), '2.0', '<')) {
30
            $downloader->download($package, $destDir, false);
31
            return;
32
        }
33
34
        $resultPromise = $downloader->download($package, $destDir, null, false);
35
        $resultPromise->then(function ($path) use (&$patchData) {
36
            $patchData[PatchDefinition::PATH] = $path;
37
        }, function (\Exception $exception) use ($source, $errorHandler, &$patchData, &$errors) {
38
            try {
39
                if (!$exception instanceof \Composer\Downloader\TransportException) {
40
                    throw $exception;
41
                }
42
                $patchData[PatchDefinition::STATUS_LABEL] = $errorHandler->handleError($source, $exception);
43
            } catch (\Exception $error) {
44
                $errors[] = $error;
45
                throw $error;
46
            }
47
            $patchData[PatchDefinition::STATUS] = PatchDefinition::STATUS_ERRORS;
48
        });
49
    }
50
51
    public function assignTmpPathForPatchData(&$patchData, $path)
52
    {
53
        if (version_compare(\Composer\Composer::getVersion(), '2.0', '<')) {
54
            $patchData[PatchDefinition::PATH] = $path;
55
        }
56
    }
57
58
    public function waitDownloadCompletion(\Composer\Composer $composer)
59
    {
60
        if (version_compare(\Composer\Composer::getVersion(), '2.0', '<')) {
61
            return;
62
        }
63
64
        $composer->getLoop()->getHttpDownloader()->wait();
65
    }
66
67
    public function waitForCompletion(\Composer\Composer $composer, array $processes)
68
    {
69
        if (version_compare(\Composer\Composer::getVersion(), '2.0', '<')) {
70
            return;
71
        }
72
73
        $composer->getLoop()->wait($processes);
74
    }
75
76
    public function processReinstallOperation(
77
        WritableRepositoryInterface $repository,
78
        InstallationManager $installationManager,
79
        InstallOperation $installOperation,
80
        UninstallOperation $uninstallOperation
81
    ) {
82
        if (version_compare(\Composer\Composer::getVersion(), '2.0', '<')) {
83
            return $installationManager->install($repository, $installOperation);
84
        }
85
86
        $package = $installOperation->getPackage();
87
        $installer = $installationManager->getInstaller($package->getType());
88
89
        $uninstallPromise = $installationManager->uninstall($repository, $uninstallOperation);
90
        if (!$uninstallPromise) {
91
            throw new OperationFailure(sprintf('Uninstallation of %s failed', $package->getName()));
92
        }
93
94
        $downloadPromise = $installer->download($package);
95
        if (!$downloadPromise) {
96
            throw new OperationFailure(sprintf('Download of %s failed', $package->getName()));
97
        }
98
99
        $promise = \React\Promise\all([$uninstallPromise, $downloadPromise]);
100
101
        return $promise->then(static function () use ($installationManager, $installOperation, $repository, $package) {
102
            $installPromise = $installationManager->install($repository, $installOperation);
103
            if (!$installPromise) {
104
                throw new OperationFailure(sprintf('Install of %s failed', $package->getName()));
105
            }
106
107
            return $installPromise->then(static function () {
108
            });
109
        });
110
    }
111
}
112