Completed
Pull Request — master (#80)
by Hiraku
02:32
created

Factory::getRequestClass()   B

Complexity

Conditions 5
Paths 3

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 5

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 10
ccs 6
cts 6
cp 1
rs 8.8571
cc 5
eloc 6
nc 3
nop 2
crap 5
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
     * @param string $origin domain text
21
     * @param string $url
22
     * @param IO\IOInterface $io
23
     * @param CConfig $config
24
     * @param array $pluginConfig
25
     * @return Aspects\HttpGetRequest
26
     */
27 3
    public static function getHttpGetRequest($origin, $url, IO\IOInterface $io, CConfig $config, array $pluginConfig)
28
    {
29 3
        if (substr($origin, -10) === 'github.com') {
30 1
            $origin = 'github.com';
31 1
        }
32 3
        $requestClass = __NAMESPACE__ . '\Aspects\\' . self::getRequestClass($origin, $config) . 'Request';
33 3
        $request = new $requestClass($origin, $url, $io);
34 3
        $request->verbose = $pluginConfig['verbose'];
35 3
        if ($pluginConfig['insecure']) {
36 1
            $request->curlOpts[CURLOPT_SSL_VERIFYPEER] = false;
37 1
        }
38 3
        if (!empty($pluginConfig['cainfo'])) {
39 1
            $request->curlOpts[CURLOPT_CAINFO] = $pluginConfig['cainfo'];
40 1
        }
41 3
        if (!empty($pluginConfig['userAgent'])) {
42 1
            $request->curlOpts[CURLOPT_USERAGENT] = $pluginConfig['userAgent'];
43 1
        }
44 3
        return $request;
45
    }
46
47
    /**
48
     * @param string $origin
49
     * @param Composer\Config $config
50
     * @return string
51
     */
52 3
    private static function getRequestClass($origin, CConfig $config)
53
    {
54 3
        if (in_array($origin, $config->get('github-domains') ?: array())) {
55 1
            return 'GitHub';
56
        }
57 3
        if (in_array($origin, $config->get('gitlab-domains') ?: array())) {
58 1
            return 'GitLab';
59
        }
60 2
        return 'HttpGet';
61
    }
62
63
    /**
64
     * @param Aspects\HttpGetRequest $req
65
     * @return Aspects\JoinPoint
66
     */
67 3
    public static function getPreEvent(Aspects\HttpGetRequest $req)
68
    {
69 3
        $pre = new Aspects\JoinPoint('pre-download', $req);
70 3
        $pre->attach(new Aspects\AspectProxy);
71 3
        return $pre;
72
    }
73
}
74