StaticSiteUtils::defineProxyOpts()   B
last analyzed

Complexity

Conditions 8
Paths 4

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 10
c 0
b 0
f 0
dl 0
loc 20
rs 8.4444
cc 8
nc 4
nop 2
1
<?php
2
3
namespace PhpTek\Exodus\Tool;
4
5
use SilverStripe\Core\Config\Configurable;
6
use SilverStripe\Core\Injector\Injectable;
7
use PhpTek\Exodus\Crawl\StaticSiteCrawler;
8
9
/**
10
 * Exodus utility methods.
11
 *
12
 * @package phptek/silverstripe-exodus
13
 * @author Russell Michell <[email protected]>
14
 */
15
class StaticSiteUtils
16
{
17
    use Injectable;
18
    use Configurable;
19
20
    /**
21
     * Log a message if the logging has been setup according to docs
22
     *
23
     * @param string $message
24
     * @param string $_args Unused so that we need not have to update calling logic:
25
     * @return void
26
     */
27
    public function log(string $message, ...$_args): void
0 ignored issues
show
Unused Code introduced by
The parameter $_args is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

27
    public function log(string $message, /** @scrutinizer ignore-unused */ ...$_args): void

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
28
    {
29
        $logFile = $this->config()->get('log_file');
30
31
        if ($logFile) {
32
            file_put_contents($logFile, $message . PHP_EOL, FILE_APPEND);
33
        }
34
    }
35
36
    /**
37
     * If operating in a specific environment, set some proxy options for it for passing to curl and
38
     * to phpCrawler (if set in config).
39
     *
40
     * @param boolean $set e.g. !Director::isDev()
41
     * @param StaticSiteCrawler $crawler (Warning: Pass by reference)
42
     * @return array Returns an array of the config options in a format consumable by curl.
43
     */
44
    public function defineProxyOpts(bool $set, StaticSiteCrawler &$crawler = null): array
45
    {
46
        if ($set && is_bool($set) && $set !== false) {
47
            $proxyOpts = StaticSiteContentExtractor::config()->get('curl_opts_proxy');
48
49
            if (!$proxyOpts || !is_array($proxyOpts) || !count($proxyOpts) > 0) {
50
                return [];
51
            }
52
53
            if ($crawler) {
54
                $crawler->setProxy($proxyOpts['hostname'], $proxyOpts['port']);
55
            }
56
57
            return [
58
                CURLOPT_PROXY => $proxyOpts['hostname'],
59
                CURLOPT_PROXYPORT => $proxyOpts['port']
60
            ];
61
        }
62
63
        return [];
64
    }
65
}
66