BrokenExternalLink::getHTTPCodeDescription()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 9
nc 4
nop 0
dl 0
loc 15
rs 9.9666
c 0
b 0
f 0
1
<?php
2
3
namespace SilverStripe\ExternalLinks\Model;
4
5
use InvalidArgumentException;
6
use SilverStripe\CMS\Model\SiteTree;
7
use SilverStripe\Control\HTTPResponse;
8
use SilverStripe\ORM\DataObject;
9
use SilverStripe\Security\Permission;
10
use SilverStripe\Security\Security;
11
12
/**
13
 * Represents a single link checked for a single run that is broken
14
 *
15
 * @property string Link
16
 * @property int HTTPCode
17
 * @method BrokenExternalPageTrack Track()
18
 * @method BrokenExternalPageTrackStatus Status()
19
 */
20
class BrokenExternalLink extends DataObject
21
{
22
    private static $table_name = 'BrokenExternalLink';
0 ignored issues
show
introduced by
The private property $table_name is not used, and could be removed.
Loading history...
23
24
    private static $db = array(
0 ignored issues
show
introduced by
The private property $db is not used, and could be removed.
Loading history...
25
        'Link' => 'Varchar(2083)', // 2083 is the maximum length of a URL in Internet Explorer.
26
        'HTTPCode' =>'Int'
27
    );
28
29
    private static $has_one = array(
0 ignored issues
show
introduced by
The private property $has_one is not used, and could be removed.
Loading history...
30
        'Track' => BrokenExternalPageTrack::class,
31
        'Status' => BrokenExternalPageTrackStatus::class
32
    );
33
34
    private static $summary_fields = array(
0 ignored issues
show
introduced by
The private property $summary_fields is not used, and could be removed.
Loading history...
35
        'Created' => 'Checked',
36
        'Link' => 'External Link',
37
        'HTTPCodeDescription' => 'HTTP Error Code',
38
        'Page.Title' => 'Page link is on'
39
    );
40
41
    private static $searchable_fields = array(
0 ignored issues
show
introduced by
The private property $searchable_fields is not used, and could be removed.
Loading history...
42
        'HTTPCode' => array('title' => 'HTTP Code')
43
    );
44
45
    /**
46
     * @return SiteTree
47
     */
48
    public function Page()
49
    {
50
        return $this->Track()->Page();
51
    }
52
53
    public function canEdit($member = false)
54
    {
55
        return false;
56
    }
57
58
    public function canView($member = false)
59
    {
60
        $member = $member ? $member : Security::getCurrentUser();
61
        $codes = array('content-authors', 'administrators');
62
        return Permission::checkMember($member, $codes);
63
    }
64
65
    /**
66
     * Retrieve a human readable description of a response code
67
     *
68
     * @return string
69
     */
70
    public function getHTTPCodeDescription()
71
    {
72
        $code = $this->HTTPCode;
73
74
        try {
75
            $response = HTTPResponse::create('', $code);
76
            // Assume that $code = 0 means there was no response
77
            $description = $code ?
78
                $response->getStatusDescription() :
79
                _t(__CLASS__ . '.NOTAVAILABLE', 'Server Not Available');
80
        } catch (InvalidArgumentException $e) {
81
            $description = _t(__CLASS__ . '.UNKNOWNRESPONSE', 'Unknown Response Code');
82
        }
83
84
        return sprintf("%d (%s)", $code, $description);
85
    }
86
}
87