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

BrokenExternalLink::getHTTPCodeDescription()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 9
nc 3
nop 0
dl 0
loc 14
rs 9.2
c 0
b 0
f 0
1
<?php
2
3
namespace SilverStripe\ExternalLinks\Model;
4
5
use SilverStripe\ExternalLinks\Model\BrokenExternalPageTrack;
6
use SilverStripe\ExternalLinks\Model\BrokenExternalPageTrackStatus;
7
use SilverStripe\Security\Member;
8
use SilverStripe\Security\Permission;
9
use SilverStripe\Core\Config\Config;
10
use SilverStripe\Control\HTTPResponse;
11
use SilverStripe\ORM\DataObject;
12
13
/**
14
 * Represents a single link checked for a single run that is broken
15
 *
16
 * @method BrokenExternalPageTrack Track()
17
 * @method BrokenExternalPageTrackStatus Status()
18
 */
19
class BrokenExternalLink extends DataObject
20
{
21
    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...
22
23
    private static $db = array(
0 ignored issues
show
introduced by
The private property $db is not used, and could be removed.
Loading history...
24
        'Link' => 'Varchar(2083)', // 2083 is the maximum length of a URL in Internet Explorer.
25
        'HTTPCode' =>'Int'
26
    );
27
28
    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...
29
        'Track' => BrokenExternalPageTrack::class,
30
        'Status' => BrokenExternalPageTrackStatus::class
31
    );
32
33
    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...
34
        'Created' => 'Checked',
35
        'Link' => 'External Link',
36
        'HTTPCodeDescription' => 'HTTP Error Code',
37
        'Page.Title' => 'Page link is on'
38
    );
39
40
    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...
41
        'HTTPCode' => array('title' => 'HTTP Code')
42
    );
43
44
    /**
45
     * @return SiteTree
0 ignored issues
show
Bug introduced by
The type SilverStripe\ExternalLinks\Model\SiteTree 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...
46
     */
47
    public function Page()
48
    {
49
        return $this->Track()->Page();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->Track()->Page() returns the type SilverStripe\CMS\Model\SiteTree which is incompatible with the documented return type SilverStripe\ExternalLinks\Model\SiteTree.
Loading history...
50
    }
51
52
    public function canEdit($member = false)
53
    {
54
        return false;
55
    }
56
57
    public function canView($member = false)
58
    {
59
        $member = $member ? $member : Member::currentUser();
0 ignored issues
show
Deprecated Code introduced by
The function SilverStripe\Security\Member::currentUser() has been deprecated: 5.0.0 use Security::getCurrentUser() ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

59
        $member = $member ? $member : /** @scrutinizer ignore-deprecated */ Member::currentUser();

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
60
        $codes = array('content-authors', 'administrators');
61
        return Permission::checkMember($member, $codes);
62
    }
63
64
    /**
65
     * Retrieve a human readable description of a response code
66
     *
67
     * @return string
68
     */
69
    public function getHTTPCodeDescription()
70
    {
71
        $code = $this->HTTPCode;
0 ignored issues
show
Bug Best Practice introduced by
The property HTTPCode does not exist on SilverStripe\ExternalLin...odel\BrokenExternalLink. Since you implemented __get, consider adding a @property annotation.
Loading history...
72
        if (empty($code)) {
73
            // Assume that $code = 0 means there was no response
74
            $description = _t('BrokenExternalLink.NOTAVAILABLE', 'Server Not Available');
75
        } elseif (($descriptions = Config::inst()->get(HTTPResponse::class, 'status_codes'))
76
            && isset($descriptions[$code])
77
        ) {
78
            $description = $descriptions[$code];
79
        } else {
80
            $description = _t('BrokenExternalLink.UNKNOWNRESPONSE', 'Unknown Response Code');
81
        }
82
        return sprintf("%d (%s)", $code, $description);
83
    }
84
}
85