Completed
Push — master ( 1fdeea...99fcff )
by Hiraku
7s
created

Factory::getPostEvent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 9.4285
cc 1
eloc 4
nc 1
nop 1
crap 1
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
            $requestClass = 'GitHub';
71 9
        } elseif (in_array($origin, $config->get('github-domains') ?: array())) {
72 1
            $requestClass = 'GitHub';
73 9
        } elseif (in_array($origin, $config->get('gitlab-domains') ?: array())) {
74 1
            $requestClass = 'GitLab';
75 1
        } else {
76 8
            $requestClass = 'HttpGet';
77
        }
78 9
        $requestClass = __NAMESPACE__ . '\Aspects\\' . $requestClass . 'Request';
79 9
        $request = new $requestClass($origin, $url, $io);
80 9
        $request->verbose = $pluginConfig['verbose'];
81 9
        if ($pluginConfig['insecure']) {
82 1
            $request->curlOpts[CURLOPT_SSL_VERIFYPEER] = false;
83 1
        }
84 9
        if (!empty($pluginConfig['cainfo'])) {
85 1
            $request->curlOpts[CURLOPT_CAINFO] = $pluginConfig['cainfo'];
86 1
        }
87 9
        if (!empty($pluginConfig['userAgent'])) {
88 1
            $request->curlOpts[CURLOPT_USERAGENT] = $pluginConfig['userAgent'];
89 1
        }
90 9
        return $request;
91
    }
92
93
    /**
94
     * @return Aspects\JoinPoint
95
     */
96 9
    public static function getPreEvent(Aspects\HttpGetRequest $req)
97
    {
98 9
        $pre = new Aspects\JoinPoint('pre-download', $req);
99 9
        $pre->attach(static::getAspectAuth());
100 9
        $pre->attach(new Aspects\AspectRedirect);
101 9
        $pre->attach(new Aspects\AspectProxy);
102 9
        return $pre;
103
    }
104
105
    /**
106
     * @return Aspects\JoinPoint
107
     */
108 7
    public static function getPostEvent(Aspects\HttpGetRequest $req)
109
    {
110 7
        $post = new Aspects\JoinPoint('post-download', $req);
111 7
        $post->attach(static::getAspectAuth());
112 7
        return $post;
113
    }
114
115
    /**
116
     * @return Aspects\AspectAuth (same instance)
117
     */
118 10
    public static function getAspectAuth()
119
    {
120 10
        static $auth;
121 10
        return $auth ?: $auth = new Aspects\AspectAuth;
122
    }
123
}
124