Completed
Pull Request — master (#13)
by Helpful
02:25 queued 10s
created

CurlLinkChecker   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1
Metric Value
wmc 4
lcom 0
cbo 1
dl 0
loc 51
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getCache() 0 8 1
B checkLink() 0 27 3
1
<?php
2
3
/**
4
 * Check links using curl
5
 */
6
class CurlLinkChecker implements LinkChecker
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
7
{
8
9
    /**
10
     * Return cache
11
     *
12
     * @return Zend_Cache_Frontend
13
     */
14
    protected function getCache()
15
    {
16
        return SS_Cache::factory(
17
            __CLASS__,
18
            'Output',
19
            array('automatic_serialization' => true)
20
        );
21
    }
22
23
    /**
24
     * Determine the http status code for a given link
25
     *
26
     * @param string $href URL to check
27
     * @return int HTTP status code, or null if not checkable (not a link)
28
     */
29
    public function checkLink($href)
30
    {
31
        // Skip non-external links
32
        if (!preg_match('/^https?[^:]*:\/\//', $href)) {
33
            return null;
34
        }
35
36
        // Check if we have a cached result
37
        $cacheKey = md5($href);
38
        $result = $this->getCache()->load($cacheKey);
39
        if ($result !== false) {
40
            return $result;
41
        }
42
43
        // No cached result so just request
44
        $handle = curl_init($href);
45
        curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
46
        curl_setopt($handle, CURLOPT_CONNECTTIMEOUT, 5);
47
        curl_setopt($handle, CURLOPT_TIMEOUT, 10);
48
        curl_exec($handle);
49
        $httpCode = curl_getinfo($handle, CURLINFO_HTTP_CODE);
50
        curl_close($handle);
51
52
        // Cache result
53
        $this->getCache()->save($httpCode, $cacheKey);
54
        return $httpCode;
55
    }
56
}
57