Passed
Pull Request — 1.9 (#127)
by Robbie
03:34 queued 58s
created

MaintenanceProxyExtension::updateClientOptions()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace CWP\CWP\Extension;
4
5
use Extension;
6
7
/**
8
 * Used to configure proxy settings for bringyourownideas/silverstripe-maintenance and its related modules
9
 *
10
 * @see https://www.cwp.govt.nz/developer-docs/en/2/how_tos/external_http_requests_with_proxy
11
 */
12
class MaintenanceProxyExtension extends Extension
13
{
14
    /**
15
     * Configures required environment settings for Composer's use, applies to
16
     * {@link \BringYourOwnIdeas\Maintenance\Util\ComposerLoader} and is applied before ComposerLoaderExtension in
17
     * bringyourownideas/silverstripe-composer-update-checker to ensure the proxy information is set before Composer
18
     * is created
19
     */
20
    public function onAfterBuild()
21
    {
22
        // Provide access for Composer's StreamContextFactory, since it creates its own stream context
23
        if ($proxy = $this->getCwpProxy()) {
24
            $_SERVER['CGI_HTTP_PROXY'] = $proxy;
25
        }
26
    }
27
28
    /**
29
     * Provide proxy options for {@link \BringYourOwnIdeas\Maintenance\Util\ApiLoader} instances to use in
30
     * their Guzzle clients
31
     *
32
     * @param array $options
33
     */
34
    public function updateClientOptions(&$options)
35
    {
36
        if ($proxy = $this->getCwpProxy()) {
37
            $options['proxy'] = $proxy;
38
        }
39
    }
40
41
    /**
42
     * Returns a formatted CWP proxy string, e.g. `tcp://proxy.cwp.govt.nz:1234`
43
     *
44
     * @return string
45
     */
46
    protected function getCwpProxy()
47
    {
48
        if (!defined('SS_OUTBOUND_PROXY') || !defined('SS_OUTBOUND_PROXY_PORT')) {
49
            return '';
50
        }
51
52
        return sprintf(
53
            'tcp://%s:%d',
54
            SS_OUTBOUND_PROXY,
0 ignored issues
show
Bug introduced by
The constant CWP\CWP\Extension\SS_OUTBOUND_PROXY was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
55
            SS_OUTBOUND_PROXY_PORT
0 ignored issues
show
Bug introduced by
The constant CWP\CWP\Extension\SS_OUTBOUND_PROXY_PORT was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
56
        );
57
    }
58
}
59