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

TransportErrorHandler   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 18
dl 0
loc 39
rs 10
c 1
b 0
f 0
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A handleError() 0 27 3
A __construct() 0 3 1
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