GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 9d7a62...7e48e6 )
by Rob
03:32
created

SafeUrls::isSafe()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 1
eloc 1
c 1
b 1
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
namespace RattfieldNz\SafeUrls;
4
5
use RattfieldNz\SafeUrls\Libraries\Data\Data;
6
use RattfieldNz\SafeUrls\Libraries\Curl\Curl;
7
use RattfieldNz\SafeUrls\Libraries\Traits\StaticCalling;
8
9
/**
10
 * Class SafeUrls.
11
 *
12
 * The main class used for the SafeUrls package.
13
 *
14
 * @category  PHP
15
 *
16
 * @author Rob Attfield <[email protected]>
17
 * @license https://github.com/rattfieldnz/safe-urls/blob/master/license.md MIT
18
 *
19
 * @link https://github.com/rattfieldnz/safe-urls/
20
 */
21
class SafeUrls
22
{
23
    use StaticCalling;
24
25
    /**
26
     * @var array Variable to hold list of urls to check.
27
     */
28
    private $urls;
29
30
    private $results;
31
32
    /**
33
     * SafeUrls constructor.
34
     */
35
    public function __construct()
36
    {
37
        // Initialise the list of urls as an empty array.
38
        $this->urls = [];
39
40
        // Initialise the list of urls as an empty array.
41
        $this->results = [];
42
    }
43
44
    /**
45
     * Checks a given set of URLs with Google's Safe Browsing API.
46
     *
47
     * @param array $urls An array of URLs to check.
48
     * @param bool $resultsAsArray Determines whether results will be returned as array or JSON.
49
     *
50
     * @return string|array The set of results, in JSON.
51
     * @throws \ErrorException
52
     */
53
    public static function check(array $urls, bool $resultsAsArray = false)
54
    {
55
        $payload = Data::payload($urls);
56
57
        $data = (new Curl($payload))->getData();
58
        return $resultsAsArray == false ? $data : json_decode($data, true);
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
59
60
    }
61
62
    /**
63
     * Function used to test 'check' static method with mocks in PHPUnit.
64
     *
65
     * @param array $urls An array of URLs to check.
66
     *
67
     * @return array|string The set of results, in JSON.
68
     */
69
    public function checkCallStatic(array $urls){
70
71
        return $this->callStatic(SafeUrls::class, "check", $urls);
72
    }
73
74
    public function execute(){
75
        $this->results = SafeUrls::check($this->urls);
76
        return $this;
77
    }
78
79
    /**
80
     * Add URLs to existing list for checking.
81
     *
82
     * @param array $urls An array of URLs to add.
83
     *
84
     * @return SafeUrls
85
     */
86
    public function add(array $urls): self
87
    {
88
        $this->urls = array_merge($this->urls, $urls);
89
90
        return $this;
91
    }
92
93
    /**
94
     * Remove URLs from existing list.
95
     *
96
     * @param array $urls An array of URLs to remove.
97
     *
98
     * @return SafeUrls
99
     */
100
    public function remove(array $urls): self
101
    {
102
        $this->urls = array_values(array_diff($this->urls, $urls));
103
104
        return $this;
105
    }
106
107
    /**
108
     * Get the URLs currently saved.
109
     *
110
     * @param bool $asArray Determines whether results to be returned as array or JSON.
111
     *
112
     * @return array|string
113
     */
114
    public function getCurrentUrls(bool $asArray = false){
115
        return $asArray == false ? json_encode($this->urls): $this->urls;
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
116
    }
117
118
    public function getResults(){
119
        return $this->results;
120
    }
121
    /**
122
     * Check to see if the URL has been marked as unsafe.
123
     *
124
     * @param string $url The URL to check.
125
     *
126
     * @return bool True if the URL is unsafe, and false if
127
     *              it is safe,
128
     */
129
    public function isDangerous(string $url): bool
130
    {
131
        $data = json_decode((string)$this->results);
132
        $matches = empty($data->response["matches"]) ? null : $data->response["matches"];
133
        if(empty($matches)){
134
            return false;
135
        }
136
        foreach ($matches as $result) {
137
            if ($result->threat->url == $url) {
138
                return true;
139
            }
140
        }
141
        return false;
142
    }
143
}
144