Sistrix::getDBs()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 1

Importance

Changes 2
Bugs 2 Features 1
Metric Value
c 2
b 2
f 1
dl 0
loc 13
ccs 10
cts 10
cp 1
rs 9.4285
cc 1
eloc 10
nc 1
nop 0
crap 1
1
<?php
2
namespace SEOstats\Services;
3
4
/**
5
 * SEOstats extension for Sistrix data.
6
 *
7
 * @package    SEOstats
8
 * @author     Stephan Schmitz <[email protected]>
9
 * @copyright  Copyright (c) 2010 - present Stephan Schmitz
10
 * @license    http://eyecatchup.mit-license.org/  MIT License
11
 * @updated    2013/08/14
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 Sistrix extends SEOstats
20
{
21 3
    public static function getDBs()
22
    {
23
        return array(
24 3
            'de', //   de – Germany
25 3
            'at', //   at – Austria
26 3
            'ch', //   ch – Switzerland
27 3
            'us', //   us – USA
28 3
            'uk', //   uk – England
29 3
            'es', //   es – Spain
30 3
            'fr', //   fr – France
31 3
            'it', //   it – Italy
32 3
        );
33
    }
34
35
    /**
36
     * Returns the Sistrix visibility index
37
     *
38
     * @access        public
39
     * @param   url   string     The URL to check.
40
     * @return        integer    Returns the Sistrix visibility index.
41
     * @link    http://www.sistrix.com/blog/870-sistrix-visibilityindex.html
42
     */
43 2 View Code Duplication
    public static function getVisibilityIndex($url = false)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
44
    {
45 2
        $url     = parent::getUrl($url);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (getUrl() instead of getVisibilityIndex()). 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...
Bug introduced by
It seems like $url defined by parent::getUrl($url) on line 45 can also be of type string; however, SEOstats\SEOstats::getUrl() does only seem to accept boolean, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
46 2
        $domain  = Helper\Url::parseHost($url);
47 2
        $dataUrl = sprintf(Config\Services::SISTRIX_VI_URL, urlencode($domain));
48
49 2
        $html = static::_getPage($dataUrl);
50 2
        @preg_match_all('#<h3>(.*?)<\/h3>#si', $html, $matches);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
51
52 2
        return isset($matches[1][0]) ? $matches[1][0] : parent::noDataDefaultValue();
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (noDataDefaultValue() instead of getVisibilityIndex()). 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...
53
    }
54
55
    /**
56
     * Returns the Sistrix visibility index by using the SISTRIX API
57
     *
58
     * @access        public
59
     * @param   url   string     The URL to check.
60
     * @return        integer    Returns the Sistrix visibility index.
61
     * @link    http://www.sistrix.com/blog/870-sistrix-visibilityindex.html
62
     */
63 3
    public static function getVisibilityIndexByApi($url = false, $db = false)
64
    {
65 3
        self::guardApiKey();
66 3
        self::guardApiCredits();
67
68 3
        $url = parent::getUrl($url);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (getUrl() instead of getVisibilityIndexByApi()). 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...
Bug introduced by
It seems like $url defined by parent::getUrl($url) on line 68 can also be of type string; however, SEOstats\SEOstats::getUrl() does only seem to accept boolean, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
69 3
        $domain = static::getDomainFromUrl($url);
70 3
        $database = static::getValidDatabase($db);
71
72 3
        $dataUrl = sprintf(Config\Services::SISTRIX_API_VI_URL, Config\ApiKeys::getSistrixApiAccessKey(), urlencode($domain), $database);
73
74 3
        $json = static::_getPage($dataUrl);
75
76 3
        if(empty($json)) {
77 1
            return parent::noDataDefaultValue();
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (noDataDefaultValue() instead of getVisibilityIndexByApi()). 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...
78
        }
79
80 2
        $json_decoded = (Helper\Json::decode($json, true));
81 2 View Code Duplication
        if (!isset($json_decoded['answer'][0]['sichtbarkeitsindex'][0]['value'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
82 1
            return parent::noDataDefaultValue();
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (noDataDefaultValue() instead of getVisibilityIndexByApi()). 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...
83
        }
84 1
        return $json_decoded['answer'][0]['sichtbarkeitsindex'][0]['value'];
85
    }
86
87 10
    public static function getApiCredits()
88
    {
89 10
        self::guardApiKey();
90
91 10
        $dataUrl = sprintf(Config\Services::SISTRIX_API_CREDITS_URL, Config\ApiKeys::getSistrixApiAccessKey());
92 10
        $json = static::_getPage($dataUrl);
93
94 10
        if(empty($json)) {
95 2
            return parent::noDataDefaultValue();
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (noDataDefaultValue() instead of getApiCredits()). 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...
96
        }
97
98 8
        $json_decoded = (Helper\Json::decode($json, true));
99 8 View Code Duplication
        if (!isset($json_decoded['answer'][0]['credits'][0]['value'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
100 6
            return parent::noDataDefaultValue();
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (noDataDefaultValue() instead of getApiCredits()). 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...
101
        }
102 2
        return $json_decoded['answer'][0]['credits'][0]['value'];
103
    }
104
105 5
    public static function checkApiCredits()
106
    {
107 5
        return static::getApiCredits() > 0;
108
    }
109
110 13
    protected static function guardApiKey()
111
    {
112 13
        if(!static::hasApiKey()) {
113
            self::exc('In order to use the SISTRIX API, you must obtain and set an ' .
114
                      'API key first (see SEOstats\Config\ApiKeys.php).' . PHP_EOL);
115
        }
116 13
    }
117
118
    protected static function hasApiKey()
119
    {
120
        if ('' == Config\ApiKeys::getSistrixApiAccessKey()) {
121
            return false;
122
        }
123
124
        return true;
125
    }
126
127 3
    protected static function guardApiCredits()
128
    {
129 3
        if(!static::checkApiCredits()) {
130
            self::exc('Not enough API credits.'.PHP_EOL);
131
        }
132 3
    }
133
134 3
    private static function checkDatabase($db)
135
    {
136 3
        return !in_array($db, self::getDBs()) ? false : $db;
137
    }
138
139 3
    protected static function getDomainFromUrl($url)
140
    {
141 3
        $url      = parent::getUrl($url);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (getUrl() instead of getDomainFromUrl()). 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...
142 3
        $domain   = Helper\Url::parseHost($url);
143 3
        static::guardDomainIsValid($domain);
144
145 3
        return $domain;
146
    }
147
148 3
    protected static function getValidDatabase($db)
149
    {
150 3
        $db = ($db == false) ? Config\DefaultSettings::SISTRIX_DB : $db;
151
152 3
        $database = self::checkDatabase($db);
153 3
        static::guardDatabaseIsValid($database);
154
155 3
        return $database;
156
    }
157
158 3
    protected static function guardDatabaseIsValid($database)
159
    {
160 3
        if (false === $database) {
161
            self::exc('db');
162
        }
163 3
    }
164
165 3
    protected static function guardDomainIsValid($domain)
166
    {
167 3
        if (false == $domain) {
168
            self::exc('Invalid domain name.');
169
        }
170 3
    }
171
172 View Code Duplication
    private static function exc($err)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
173
    {
174
        $e = ($err == 'db')
175
            ? "Invalid database. Choose one of: " .
176
               substr( implode(", ", self::getDBs()), 0, -2)
177
            : $err;
178
        throw new E($e);
179
    }
180
}
181