Completed
Push — master ( 4dd9bb...1d5a37 )
by Allan
03:20 queued 12s
created

DownloaderComponent   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 143
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 58
c 0
b 0
f 0
dl 0
loc 143
rs 10
wmc 12

4 Methods

Rating   Name   Duplication   Size   Complexity  
A createPackage() 0 16 2
A process() 0 42 5
A __construct() 0 12 1
A handleTransportError() 0 25 4
1
<?php
2
/**
3
 * Copyright © Vaimo Group. All rights reserved.
4
 * See LICENSE_VAIMO.txt for license details.
5
 */
6
namespace Vaimo\ComposerPatches\Patch\DefinitionList\LoaderComponents;
7
8
use Vaimo\ComposerPatches\Patch\Definition as PatchDefinition;
9
use Vaimo\ComposerPatches\Utils\PathUtils;
10
11
class DownloaderComponent implements \Vaimo\ComposerPatches\Interfaces\DefinitionListLoaderComponentInterface
12
{
13
    /**
14
     * @var bool
15
     */
16
    private $gracefulMode;
17
18
    /**
19
     * @var \Composer\Downloader\FileDownloader
20
     */
21
    private $fileDownloader;
22
23
    /**
24
     * @var \Vaimo\ComposerPatches\Console\Silencer
25
     */
26
    private $consoleSilencer;
27
28
    /**
29
     * @var \Composer\Package\PackageInterface
30
     */
31
    private $ownerPackage;
32
33
    /**
34
     * @var string
35
     */
36
    private $vendorDir;
37
38
    /**
39
     * @SuppressWarnings(PHPMD.BooleanArgumentFlag)
40
     *
41
     * @param \Composer\Package\PackageInterface $ownerPackage
42
     * @param \Composer\Downloader\FileDownloader $downloadManager
43
     * @param \Vaimo\ComposerPatches\Console\Silencer $consoleSilencer
44
     * @param string $vendorDir
45
     * @param bool $gracefulMode
46
     */
47
    public function __construct(
48
        \Composer\Package\PackageInterface $ownerPackage,
49
        \Composer\Downloader\FileDownloader $downloadManager,
50
        \Vaimo\ComposerPatches\Console\Silencer $consoleSilencer,
51
        $vendorDir,
52
        $gracefulMode = false
53
    ) {
54
        $this->ownerPackage = $ownerPackage;
55
        $this->fileDownloader = $downloadManager;
56
        $this->consoleSilencer = $consoleSilencer;
57
        $this->vendorDir = $vendorDir;
58
        $this->gracefulMode = $gracefulMode;
59
    }
60
61
    /**
62
     * @param array $patches
63
     * @param \Composer\Package\PackageInterface[] $packagesByName
64
     * @return array
65
     * @throws \Exception
66
     */
67
    public function process(array $patches, array $packagesByName)
68
    {
69
        $ownerName = $this->ownerPackage->getName();
70
71
        $relativePath = PathUtils::composePath($ownerName, 'downloads');
72
        $absolutePath = PathUtils::composePath($this->vendorDir, $relativePath);
73
        
74
        foreach ($patches as &$packagePatches) {
75
            foreach ($packagePatches as &$patchData) {
76
                if (!$patchData[PatchDefinition::URL]) {
77
                    continue;
78
                }
79
80
                $source = $patchData[PatchDefinition::URL];
81
                $checksum = $patchData[PatchDefinition::CHECKSUM];
82
83
                $hashComponents = array($source, $checksum);
84
                $sourceHash = md5(implode('|', $hashComponents));
85
86
                $package = $this->createPackage($source, $sourceHash, $relativePath, $checksum);
87
                
88
                $destinationFolder = PathUtils::composePath($absolutePath, $sourceHash);
89
                $destinationFile = PathUtils::composePath($destinationFolder, basename($source));
90
                
91
                try {
92
                    $downloader = $this->fileDownloader;
93
94
                    $this->consoleSilencer->applyToCallback(
95
                        function () use ($downloader, $package, $destinationFolder) {
96
                            $downloader->download($package, $destinationFolder, false);
97
                        }
98
                    );
99
                } catch (\Composer\Downloader\TransportException $exception) {
100
                    $this->handleTransportError($source, $exception);
101
                }
102
103
                $patchData[PatchDefinition::PATH] = $destinationFile;
104
                $patchData[PatchDefinition::TMP] = true;
105
            }
106
        }
107
108
        return $patches;
109
    }
110
    
111
    private function createPackage($remoteFile, $name, $targetDir, $checksum = null)
112
    {
113
        $version = '0.0.0';
114
        
115
        $package = new \Composer\Package\Package($name, $version, $version);
116
117
        $package->setInstallationSource('dist');
118
        $package->setDistType('file');
119
        $package->setTargetDir($targetDir);
120
        $package->setDistUrl($remoteFile);
121
        
122
        if ($checksum) {
123
            $package->setDistSha1Checksum($checksum);
124
        }
125
        
126
        return $package;
127
    }
128
    
129
    private function handleTransportError($source, \Composer\Downloader\TransportException $exception)
130
    {
131
        if (strpos($exception->getMessage(), 'configuration does not allow connections') !== false) {
132
            $exception = new \Composer\Downloader\TransportException(
133
                sprintf(
134
                    'Your configuration does not allow connections to %s. ' .
135
                    'Override the \'secure-http\' to allow: ' .
136
                    'https://github.com/vaimo/composer-patches#patcher-configuration',
137
                    $source
138
                ),
139
                $exception->getCode()
140
            );
141
142
            $patchData[PatchDefinition::STATUS_LABEL] = 'UNSECURE';
0 ignored issues
show
Comprehensibility Best Practice introduced by
$patchData was never initialized. Although not strictly required by PHP, it is generally a good practice to add $patchData = array(); before regardless.
Loading history...
143
        }
144
145
        if ($exception->getCode() === 404) {
146
            $patchData[PatchDefinition::STATUS_LABEL] = 'ERROR 404';
147
        }
148
149
        if ($this->gracefulMode) {
150
            return;
151
        }
152
153
        throw $exception;
154
    }
155
}
156