Completed
Pull Request — master (#112)
by
unknown
02:44
created

Mozscape::getDomainAuthority()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 6
ccs 2
cts 2
cp 1
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
crap 2
1
<?php
2
namespace SEOstats\Services;
3
4
/**
5
 * SEOstats extension for Mozscape (f.k.a. Seomoz) metrics.
6
 *
7
 * @package    SEOstats
8
 * @author     Stephan Schmitz &lt;[email protected]&gt;
9
 * @copyright  Copyright (c) 2010 - present Stephan Schmitz
10
 * @license    http://eyecatchup.mit-license.org/  MIT License
11
 * @updated    2013/12/11
12
 */
13
14
use SEOstats\Common\SEOstatsException as E;
15
use SEOstats\SEOstats as SEOstats;
16
use SEOstats\Config as Config;
17
use SEOstats\Helper as Helper;
18
19
class Mozscape extends SEOstats
20
{
21
22
    protected static $lastLoadedDomain;
23 2
    protected static $lastLoadedPage;
24
    // A normalized 100-point score representing the likelihood
25 2
    // of the URL to rank well in search engine results.
26 2
    public static function getPageAuthority($url = false)
27 2
    {
28
        $data = static::getCols($url);
0 ignored issues
show
Documentation introduced by
$url is of type boolean, but the function expects a false|string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
29
        return (parent::noDataDefaultValue() == $data) ? $data :
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (noDataDefaultValue() instead of getPageAuthority()). Are you sure this is correct? If so, you might want to change this to $this->noDataDefaultValue().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
30
            $data['upa'];
31
    }
32 2
33
    // A normalized 100-point score representing the likelihood
34 2
    // of the domain of the URL to rank well in search engine results.
35 2
    public static function getDomainAuthority($url = false)
36 2
    {
37
        $data = static::getCols(Helper\Url::parseHost($url));
38
        return (parent::noDataDefaultValue() == $data) ? $data :
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (noDataDefaultValue() instead of getDomainAuthority()). Are you sure this is correct? If so, you might want to change this to $this->noDataDefaultValue().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
39
            $data['pda'];
40
    }
41 2
42
    // The number of external equity links to the URL.
43 2
    // http://apiwiki.moz.com/glossary#equity
44 2
    public static function getEquityLinkCount($url = false)
45 2
    {
46
        $data = static::getCols($url);
0 ignored issues
show
Documentation introduced by
$url is of type boolean, but the function expects a false|string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
47
        return (parent::noDataDefaultValue() == $data) ? $data :
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (noDataDefaultValue() instead of getEquityLinkCount()). Are you sure this is correct? If so, you might want to change this to $this->noDataDefaultValue().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
48
            $data['uid'];
49 2
    }
50
51 2
    // The number of links (equity or nonequity or not, internal or external) to the URL.
52 2
    public static function getLinkCount($url = false)
53 2
    {
54
        $data = static::getCols($url);
0 ignored issues
show
Documentation introduced by
$url is of type boolean, but the function expects a false|string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
55
        return (parent::noDataDefaultValue() == $data) ? $data :
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (noDataDefaultValue() instead of getLinkCount()). Are you sure this is correct? If so, you might want to change this to $this->noDataDefaultValue().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
56
            $data['uid'];
57 2
    }
58
59 2
    // The normalized 10-point MozRank score of the URL.
60 2
    public static function getMozRank($url = false)
61 2
    {
62
        $data = static::getCols($url);
0 ignored issues
show
Documentation introduced by
$url is of type boolean, but the function expects a false|string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
63
        return (parent::noDataDefaultValue() == $data) ? $data :
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (noDataDefaultValue() instead of getMozRank()). Are you sure this is correct? If so, you might want to change this to $this->noDataDefaultValue().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
64
            $data['umrp'];
65 2
    }
66
67 2
    // The raw MozRank score of the URL.
68 2
    public static function getMozRankRaw($url = false)
69 2
    {
70
        $data = static::getCols($url);
0 ignored issues
show
Documentation introduced by
$url is of type boolean, but the function expects a false|string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
71
        return (parent::noDataDefaultValue() == $data) ? $data :
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (noDataDefaultValue() instead of getMozRankRaw()). Are you sure this is correct? If so, you might want to change this to $this->noDataDefaultValue().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
72
            number_format($data['umrr'], 16);
73
    }
74
75
    /**
76
     * Return Link metrics from the (free) Mozscape (f.k.a. Seomoz) API.
77
     *
78
     * @access        public
79 5
     * @param   cols  string     The bit flags you want returned.
80
     * @param   url   string     The URL to get metrics for.
81 5
     */
82 5
    public static function getCols($url = false)
83
    {
84
        if ('' == Config\ApiKeys::get('MOZSCAPE_ACCESS_ID') ||
85
            '' == Config\ApiKeys::get('MOZSCAPE_SECRET_KEY')
86
        ) {
87
            throw new E('In order to use the Mozscape API, you must obtain
88 5
                and set an API key first (see SEOstats\Config\ApiKeys.php).');
89
        }
90 5
        $host = Helper\Url::parseHost(parent::getUrl($url));
0 ignored issues
show
Bug introduced by
It seems like $url defined by parameter $url on line 82 can also be of type string; however, SEOstats\SEOstats::getUrl() does only seem to accept boolean, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
Comprehensibility Bug introduced by
It seems like you call parent on a different method (getUrl() instead of getCols()). Are you sure this is correct? If so, you might want to change this to $this->getUrl().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
91 5
92 5
        if (static::$lastLoadedDomain == $host) {
93 5
            return static::$lastLoadedPage;
94 5
        }
95 5
96 5
        static::$lastLoadedDomain = $host;
97
98 5
        $expires = time() + 300;
99
        $apiEndpoint = sprintf(Config\Services::MOZSCAPE_API_URL,
100 5
            urlencode($host),
101 5
            Config\ApiKeys::get('MOZSCAPE_ACCESS_ID'),
102 5
            $expires,
103
            urlencode(self::_getUrlSafeSignature($expires))
104
        );
105 6
106
        $ret = static::_getPage($apiEndpoint);
107 6
108 6
        static::$lastLoadedPage = (!$ret || empty($ret) || '{}' == (string)$ret)
109
            ? parent::noDataDefaultValue()
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (noDataDefaultValue() instead of getCols()). Are you sure this is correct? If so, you might want to change this to $this->noDataDefaultValue().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
110 6
            : Helper\Json::decode($ret, true);
111
        return static::$lastLoadedPage;
112
    }
113 7
114
    private static function _getUrlSafeSignature($expires)
115
    {
116
        $data = Config\ApiKeys::get('MOZSCAPE_ACCESS_ID') . "\n{$expires}";
117 7
        $sig = self::_hmacsha1($data, Config\ApiKeys::get('MOZSCAPE_SECRET_KEY'));
118 7
119
        return base64_encode($sig);
120
    }
121
122
    private static function _hmacsha1($data, $key)
123
    {
124 1
        // Use PHP's built in functionality if available
125
        // (~20% faster than the custom implementation below).
126 1
        if (function_exists('hash_hmac')) {
127 1
            return hash_hmac('sha1', $data, $key, true);
128
        }
129 1
130
        return self::_hmacsha1Rebuild($data, $key);
131
    }
132
133 1
    private static function _hmacsha1Rebuild($data, $key)
134 1
    {
135 1
        $blocksize = 64;
136 1
        $hashfunc = 'sha1';
137 1
138 1
        if (strlen($key) > $blocksize) {
139
            $key = pack('H*', $hashfunc($key));
140
        }
141
142
        $key = str_pad($key, $blocksize, chr(0x00));
143
        $ipad = str_repeat(chr(0x36), $blocksize);
144
        $opad = str_repeat(chr(0x5c), $blocksize);
145
        $hmac = pack('H*', $hashfunc(($key ^ $opad) .
146
            pack('H*', $hashfunc(($key ^ $ipad) . $data))));
147
        return $hmac;
148
    }
149
}
150