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

StaticSiteUtils::log()   A

Complexity

Conditions 6
Paths 5

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 4
nc 5
nop 3
dl 0
loc 7
rs 9.2222
c 0
b 0
f 0
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