Completed
Push — 2.1 ( 9942fb...3ad235 )
by
unknown
11s
created

MaintenanceProxyExtension   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 49
rs 10
c 0
b 0
f 0
wmc 9

3 Methods

Rating   Name   Duplication   Size   Complexity  
A onAfterBuild() 0 10 4
A getCwpProxy() 0 10 3
A updateClientOptions() 0 4 2
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 18 and the first side effect is on line 10.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
3
namespace CWP\CWP\Extension;
4
5
use BringYourOwnIdeas\Maintenance\Reports\SiteSummary;
0 ignored issues
show
Bug introduced by
The type BringYourOwnIdeas\Maintenance\Reports\SiteSummary was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use SilverStripe\Core\Environment;
7
use SilverStripe\Core\Extension;
8
9
if (!class_exists(SiteSummary::class)) {
10
    return;
11
}
12
13
/**
14
 * Used to configure proxy settings for bringyourownideas/silverstripe-maintenance and its related modules
15
 *
16
 * @see https://www.cwp.govt.nz/developer-docs/en/2/how_tos/external_http_requests_with_proxy
17
 */
18
class MaintenanceProxyExtension extends Extension
19
{
20
    /**
21
     * Configures required environment settings for Composer's use, applies to
22
     * {@link \BringYourOwnIdeas\Maintenance\Util\ComposerLoader} and is applied before ComposerLoaderExtension in
23
     * bringyourownideas/silverstripe-composer-update-checker to ensure the proxy information is set before Composer
24
     * is created
25
     */
26
    public function onAfterBuild()
27
    {
28
        // Mock COMPOSER_HOME if it's not defined already. Composer requires one of the two to be set.
29
        if (!Environment::getEnv('HOME') && !Environment::getEnv('COMPOSER_HOME')) {
30
            putenv('COMPOSER_HOME=/tmp');
31
        }
32
33
        // Provide access for Composer's StreamContextFactory, since it creates its own stream context
34
        if ($proxy = $this->getCwpProxy()) {
35
            $_SERVER['CGI_HTTP_PROXY'] = $proxy;
36
        }
37
    }
38
39
    /**
40
     * Provide proxy options for {@link \BringYourOwnIdeas\Maintenance\Util\ApiLoader} instances to use in
41
     * their Guzzle clients
42
     *
43
     * @param array $options
44
     */
45
    public function updateClientOptions(&$options)
46
    {
47
        if ($proxy = $this->getCwpProxy()) {
48
            $options['proxy'] = $proxy;
49
        }
50
    }
51
52
    /**
53
     * Returns a formatted CWP proxy string, e.g. `tcp://proxy.cwp.govt.nz:1234`
54
     *
55
     * @return string
56
     */
57
    protected function getCwpProxy()
58
    {
59
        if (!Environment::getEnv('SS_OUTBOUND_PROXY') || !Environment::getEnv('SS_OUTBOUND_PROXY_PORT')) {
60
            return '';
61
        }
62
63
        return sprintf(
64
            'tcp://%s:%d',
65
            Environment::getEnv('SS_OUTBOUND_PROXY'),
66
            Environment::getEnv('SS_OUTBOUND_PROXY_PORT')
67
        );
68
    }
69
}
70