Test Failed
Push — master ( 802928...59439d )
by Russell
12:23
created

StaticSiteUtils   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 17
dl 0
loc 51
rs 10
c 0
b 0
f 0
wmc 14

2 Methods

Rating   Name   Duplication   Size   Complexity  
B defineProxyOpts() 0 20 8
A log() 0 7 6
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 $filename
25
     * @param string $mime
26
     * @return null | void
27
     */
28
    public function log(string $message, string $filename = '', string $mime = ''): void
29
    {
30
        $logFile = $filename ?: $this->config()->get('log_file');
31
32
        if ($logFile && is_writable($logFile)) {
33
            $message = $message . ($filename ? ' ' . $filename : '') . ($mime ? ' (' . $mime . ')' : '');
34
            error_log($message. PHP_EOL, 3, $logFile);
35
        }
36
    }
37
38
    /**
39
     * If operating in a specific environment, set some proxy options for it for passing to curl and
40
     * to phpCrawler (if set in config).
41
     *
42
     * @param boolean $set e.g. !Director::isDev()
43
     * @param StaticSiteCrawler $crawler (Warning: Pass by reference)
44
     * @return array Returns an array of the config options in a format consumable by curl.
45
     */
46
    public function defineProxyOpts(bool $set, StaticSiteCrawler &$crawler = null): array
47
    {
48
        if ($set && is_bool($set) && $set !== false) {
49
            $proxyOpts = StaticSiteContentExtractor::config()->get('curl_opts_proxy');
50
51
            if (!$proxyOpts || !is_array($proxyOpts) || !count($proxyOpts) >0) {
52
                return [];
53
            }
54
55
            if ($crawler) {
56
                $crawler->setProxy($proxyOpts['hostname'], $proxyOpts['port']);
57
            }
58
59
            return [
60
                CURLOPT_PROXY => $proxyOpts['hostname'],
61
                CURLOPT_PROXYPORT => $proxyOpts['port']
62
            ];
63
        }
64
65
        return [];
66
    }
67
}
68