Completed
Pull Request — master (#27)
by
unknown
03:13
created

CurlLinkChecker   A

Complexity

Total Complexity 4

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 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getCache() 0 6 1
B checkLink() 0 26 3
1
<?php
2
3
namespace SilverStripe\ExternalLinks\Tasks;
4
5
use SS_Cache;
0 ignored issues
show
Bug introduced by
The type SS_Cache 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
7
/**
8
 * Check links using curl
9
 */
10
class CurlLinkChecker implements LinkChecker
11
{
12
13
    /**
14
     * Return cache
15
     *
16
     * @return Zend_Cache_Frontend
0 ignored issues
show
Bug introduced by
The type SilverStripe\ExternalLin...sks\Zend_Cache_Frontend 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...
17
     */
18
    protected function getCache()
19
    {
20
        return SS_Cache::factory(
21
            __CLASS__,
22
            'Output',
23
            array('automatic_serialization' => true)
24
        );
25
    }
26
27
    /**
28
     * Determine the http status code for a given link
29
     *
30
     * @param string $href URL to check
31
     * @return int HTTP status code, or null if not checkable (not a link)
32
     */
33
    public function checkLink($href)
34
    {
35
        // Skip non-external links
36
        if (!preg_match('/^https?[^:]*:\/\//', $href)) {
37
            return null;
38
        }
39
40
        // Check if we have a cached result
41
        $cacheKey = md5($href);
42
        $result = $this->getCache()->load($cacheKey);
43
        if ($result !== false) {
44
            return $result;
45
        }
46
47
        // No cached result so just request
48
        $handle = curl_init($href);
49
        curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
50
        curl_setopt($handle, CURLOPT_CONNECTTIMEOUT, 5);
51
        curl_setopt($handle, CURLOPT_TIMEOUT, 10);
52
        curl_exec($handle);
53
        $httpCode = curl_getinfo($handle, CURLINFO_HTTP_CODE);
54
        curl_close($handle);
55
56
        // Cache result
57
        $this->getCache()->save($httpCode, $cacheKey);
58
        return $httpCode;
59
    }
60
}
61