Completed
Pull Request — master (#44)
by Hiraku
08:54 queued 06:28
created

Factory   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 107
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 6
Bugs 1 Features 1
Metric Value
wmc 17
c 6
b 1
f 1
lcom 1
cbo 5
dl 0
loc 107
ccs 21
cts 21
cp 1
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getConnection() 0 16 4
D getHttpGetRequest() 0 26 9
A getPreEvent() 0 8 1
A getPostEvent() 0 6 1
A getAspectAuth() 0 5 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
     * 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 6
     * @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 6
     */
41 1
    public static function getConnection($origin, $auth = false)
42 1
    {
43
        if ($auth) {
44
            if (isset(self::$authConnections[$origin])) {
45 1
                return self::$authConnections[$origin];
46
            }
47 6
48 5
            return self::$authConnections[$origin] = curl_init();
49
        } else {
50
            if (isset(self::$connections[$origin])) {
51 2
                return self::$connections[$origin];
52
            }
53
54
            return self::$connections[$origin] = curl_init();
55
        }
56
    }
57
58 6
    /**
59
     * @param string $origin domain text
60 6
     * @param string $url
61 6
     * @param IO\IOInterface $io
62 6
     * @param CConfig $config
63 6
     * @param array $pluginConfig
64 6
     * @return Aspects\HttpGetRequest
65
     */
66
    public static function getHttpGetRequest($origin, $url, IO\IOInterface $io, CConfig $config, array $pluginConfig)
67
    {
68
        if (substr($origin, -10) === 'github.com') {
69
            $origin = 'github.com';
70 6
            $requestClass = 'GitHub';
71
        } elseif (in_array($origin, $config->get('github-domains') ?: array())) {
72 6
            $requestClass = 'GitHub';
73 6
        } elseif (in_array($origin, $config->get('gitlab-domains') ?: array())) {
74 6
            $requestClass = 'GitLab';
75
        } else {
76
            $requestClass = 'HttpGet';
77
        }
78
        $requestClass = __NAMESPACE__ . '\Aspects\\' . $requestClass . 'Request';
79
        $request = new $requestClass($origin, $url, $io);
80 7
        $request->verbose = $pluginConfig['verbose'];
81
        if ($pluginConfig['insecure']) {
82 7
            $request->curlOpts[CURLOPT_SSL_VERIFYPEER] = false;
83 7
        }
84
        if (!empty($pluginConfig['capath'])) {
85
            $request->curlOpts[CURLOPT_CAPATH] = $pluginConfig['capath'];
86
        }
87
        if (!empty($pluginConfig['userAgent'])) {
88
            $request->curlOpts[CURLOPT_USERAGENT] = $pluginConfig['userAgent'];
89
        }
90
        return $request;
91
    }
92
93
    /**
94
     * @return Aspects\JoinPoint
95
     */
96
    public static function getPreEvent(Aspects\HttpGetRequest $req)
97
    {
98
        $pre = new Aspects\JoinPoint('pre-download', $req);
99
        $pre->attach(static::getAspectAuth());
100
        $pre->attach(new Aspects\AspectRedirect);
101
        $pre->attach(new Aspects\AspectProxy);
102
        return $pre;
103
    }
104
105
    /**
106
     * @return Aspects\JoinPoint
107
     */
108
    public static function getPostEvent(Aspects\HttpGetRequest $req)
109
    {
110
        $post = new Aspects\JoinPoint('post-download', $req);
111
        $post->attach(static::getAspectAuth());
112
        return $post;
113
    }
114
115
    /**
116
     * @return Aspects\AspectAuth (same instance)
117
     */
118
    public static function getAspectAuth()
119
    {
120
        static $auth;
121
        return $auth ?: $auth = new Aspects\AspectAuth;
122
    }
123
}
124