Completed
Pull Request — master (#66)
by Jan
05:29
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 1
Bugs 0 Features 0
Metric Value
c 1
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
     * not need Authorization
21
     * @var array {
22
     *  'origin.example.com' => x
23
     * }
24
     */
25
    private static $connections = array();
26
27
    /**
28
     * need Authorization header
29
     * @var array {
30
     *  'origin.example.com' => x
31
     * }
32
     */
33
    private static $authConnections = array();
34
35
    /**
36
     * get cached curl handler
37
     * @param string $origin
38
     * @param bool $auth
39
     * @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...
40
     */
41 7
    public static function getConnection($origin, $auth = false)
42
    {
43 7
        if ($auth) {
44 1
            if (isset(self::$authConnections[$origin])) {
45 1
                return self::$authConnections[$origin];
46
            }
47
48 1
            return self::$authConnections[$origin] = curl_init();
49
        } else {
50 7
            if (isset(self::$connections[$origin])) {
51 6
                return self::$connections[$origin];
52
            }
53
54 2
            return self::$connections[$origin] = curl_init();
55
        }
56
    }
57
58
    /**
59
     * @param string $origin domain text
60
     * @param string $url
61
     * @param IO\IOInterface $io
62
     * @param CConfig $config
63
     * @param array $pluginConfig
64
     * @return Aspects\HttpGetRequest
65
     */
66 9
    public static function getHttpGetRequest($origin, $url, IO\IOInterface $io, CConfig $config, array $pluginConfig)
67
    {
68 9
        if (substr($origin, -10) === 'github.com') {
69 1
            $origin = 'github.com';
70 1
        }
71 9
        $requestClass = __NAMESPACE__ . '\Aspects\\' . self::getRequestClass($origin, $config) . 'Request';
72 9
        $request = new $requestClass($origin, $url, $io);
73 9
        $request->verbose = $pluginConfig['verbose'];
74 9
        if ($pluginConfig['insecure']) {
75 1
            $request->curlOpts[CURLOPT_SSL_VERIFYPEER] = false;
76 1
        }
77 9
        if (!empty($pluginConfig['cainfo'])) {
78 1
            $request->curlOpts[CURLOPT_CAINFO] = $pluginConfig['cainfo'];
79 1
        }
80 9
        if (!empty($pluginConfig['userAgent'])) {
81 1
            $request->curlOpts[CURLOPT_USERAGENT] = $pluginConfig['userAgent'];
82 1
        }
83 9
        return $request;
84
    }
85
86 9
    private static function getRequestClass($origin, $config)
87
    {
88 9
        if (in_array($origin, $config->get('github-domains') ?: array())) {
89 1
            return 'GitHub';
90
        }
91 9
        if (in_array($origin, $config->get('gitlab-domains') ?: array())) {
92 1
            return 'GitLab';
93
        }
94 8
        return 'HttpGet';
95
    }
96
97
    /**
98
     * @return Aspects\JoinPoint
99
     */
100 9
    public static function getPreEvent(Aspects\HttpGetRequest $req)
101
    {
102 9
        $pre = new Aspects\JoinPoint('pre-download', $req);
103 9
        $pre->attach(static::getAspectAuth());
104 9
        $pre->attach(new Aspects\AspectRedirect);
105 9
        $pre->attach(new Aspects\AspectProxy);
106 9
        return $pre;
107
    }
108
109
    /**
110
     * @return Aspects\JoinPoint
111
     */
112 7
    public static function getPostEvent(Aspects\HttpGetRequest $req)
113
    {
114 7
        $post = new Aspects\JoinPoint('post-download', $req);
115 7
        $post->attach(static::getAspectAuth());
116 7
        return $post;
117
    }
118
119
    /**
120
     * @return Aspects\AspectAuth (same instance)
121
     */
122 10
    public static function getAspectAuth()
123
    {
124 10
        static $auth;
125 10
        return $auth ?: $auth = new Aspects\AspectAuth;
126
    }
127
}
128