Passed
Push — master ( 99b058...aba4a6 )
by Allan
07:10 queued 11s
created

TransportErrorHandler::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
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\ErrorHandlers;
7
8
class TransportErrorHandler
9
{
10
    /**
11
     * @var bool
12
     */
13
    private $gracefulMode;
14
15
    public function __construct($gracefulMode)
16
    {
17
        $this->gracefulMode = $gracefulMode;
18
    }
19
20
    public function handleError($source, \Composer\Downloader\TransportException $exception)
21
    {
22
        $statusLabel = sprintf('ERROR %s', $exception->getCode());
23
24
        if (strpos($exception->getMessage(), 'configuration does not allow connections') !== false) {
25
            $docsUrl = 'https://github.com/vaimo/composer-patches/blob/master/docs/CONFIGURATION.md#%s';
26
            $subjectReference = 'allow-downloads-from-unsecure-locations';
27
28
            $message = sprintf(
29
                'Your configuration does not allow connections to %s. Override the \'secure-http\' to allow: %s',
30
                $source,
31
                sprintf($docsUrl, $subjectReference)
32
            );
33
34
            $exception = new \Composer\Downloader\TransportException(
35
                $message,
36
                $exception->getCode()
37
            );
38
39
            $statusLabel = 'UNSECURE';
40
        }
41
42
        if ($this->gracefulMode) {
43
            return $statusLabel;
44
        }
45
46
        throw $exception;
47
    }
48
}
49