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