Completed
Push — master ( f735c4...3825d6 )
by Hiraku
7s
created

Factory::getAspectAuth()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 9.4285
cc 2
eloc 3
nc 2
nop 0
crap 2
1
<?php
2
/*
3
 * hirak/prestissimo
4
 * @author Hiraku NAKANO
5
 * @license MIT https://github.com/hirak/prestissimo
6
 */
7
namespace Hirak\Prestissimo;
8
9
use Composer\IO;
10
use Composer\Config as CConfig;
11
12
/**
13
 * cache manager for curl handler
14
 *
15
 * Singleton
16
 */
17
final class Factory
18
{
19
    /**
20
     * @var array {
21
     *  'origin.example.com' => x
22
     * }
23
     */
24
    private static $connections = array();
25
26
    /**
27
     * get cached curl handler
28
     * @param string $origin
29
     * @return resource<curl>
0 ignored issues
show
Documentation introduced by
The doc-type resource<curl> could not be parsed: Expected "|" or "end of type", but got "<" at position 8. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
30
     */
31 7
    public static function getConnection($origin)
32
    {
33 7
        if (isset(self::$connections[$origin])) {
34 6
            return self::$connections[$origin];
35
        }
36
37 2
        return self::$connections[$origin] = curl_init();
38
    }
39
40
    /**
41
     * @param string $origin domain text
42
     * @param string $url
43
     * @param IO\IOInterface $io
44
     * @param CConfig $config
45
     * @param array $pluginConfig
46
     * @return Aspects\HttpGetRequest
47
     */
48 9
    public static function getHttpGetRequest($origin, $url, IO\IOInterface $io, CConfig $config, array $pluginConfig)
49
    {
50 9
        if (substr($origin, -10) === 'github.com') {
51 1
            $origin = 'github.com';
52 1
        }
53 9
        $requestClass = __NAMESPACE__ . '\Aspects\\' . self::getRequestClass($origin, $config) . 'Request';
54 9
        $request = new $requestClass($origin, $url, $io);
55 9
        $request->verbose = $pluginConfig['verbose'];
56 9
        if ($pluginConfig['insecure']) {
57 1
            $request->curlOpts[CURLOPT_SSL_VERIFYPEER] = false;
58 1
        }
59 9
        if (!empty($pluginConfig['cainfo'])) {
60 1
            $request->curlOpts[CURLOPT_CAINFO] = $pluginConfig['cainfo'];
61 1
        }
62 9
        if (!empty($pluginConfig['userAgent'])) {
63 1
            $request->curlOpts[CURLOPT_USERAGENT] = $pluginConfig['userAgent'];
64 1
        }
65 9
        return $request;
66
    }
67
68
    /**
69
     * @param string $origin
70
     * @param Composer\Config $config
71
     * @return string
72
     */
73 9
    private static function getRequestClass($origin, CConfig $config)
74
    {
75 9
        if (in_array($origin, $config->get('github-domains') ?: array())) {
76 1
            return 'GitHub';
77
        }
78 9
        if (in_array($origin, $config->get('gitlab-domains') ?: array())) {
79 1
            return 'GitLab';
80
        }
81 8
        return 'HttpGet';
82
    }
83
84
    /**
85
     * @param Aspects\HttpGetRequest $req
86
     * @return Aspects\JoinPoint
87
     */
88 9
    public static function getPreEvent(Aspects\HttpGetRequest $req)
89
    {
90 9
        $pre = new Aspects\JoinPoint('pre-download', $req);
91 9
        $pre->attach(new Aspects\AspectProxy);
92 9
        return $pre;
93
    }
94
95
    /**
96
     * @param Aspects\HttpGetRequest $req
97
     * @return Aspects\JoinPoint
98
     */
99 7
    public static function getPostEvent(Aspects\HttpGetRequest $req)
100
    {
101 7
        $post = new Aspects\JoinPoint('post-download', $req);
102 7
        $post->attach(new Aspects\AspectAuth);
103 7
        return $post;
104
    }
105
}
106