Completed
Push — master ( 22d622...cf667e )
by Yoshiaki
19s
created

Checker   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 81
Duplicated Lines 19.75 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 6
Bugs 0 Features 0
Metric Value
wmc 12
c 6
b 0
f 0
lcom 1
cbo 2
dl 16
loc 81
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 21 3
B fetch() 0 33 6
A getException() 0 6 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 10 and the first side effect is on line 5.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
3
namespace Maintenance;
4
5
require_once (__DIR__ . '/vendor/autoload.php');
6
7
/**
8
 * @author bootjp
9
 */
10
class Checker
11
{
12
    protected $client;
13
14
    protected static $ipAddress;
15
16
    protected $error = '';
17
18
    public function __construct($iniFilePath = null)
19
    {
20
        $this->client = new \GuzzleHttp\Client([
21
                'defaults' => [
22
                    'headers' => [
23
                        'User-Agent' => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_2) ' .
24
                        'AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.111 Safari/537.36'
25
                    ]
26
                ]
27
            ]
28
        );
29
        if (!is_null($iniFilePath)) {
30
            if (!file_exists($iniFilePath)) {
31
                throw new \InvalidArgumentException('ini file not found');
32
            }
33
34
            self::$ipAddress = parse_ini_file($iniFilePath, true);
35
        } else {
36
            self::$ipAddress = parse_ini_file(__DIR__ . '/ipaddress.ini', true);
37
        }
38
    }
39
40
    /**
41
     * @param string $url [optional]
42
     * @return array
43
     */
44
    public function fetch($url = 'http://support.sakura.ad.jp/mainte/mainteindex.php?service=vps')
45
    {
46
        $existsIpAddress = [];
47
48
        try {
49
            preg_match_all('{/mainte/mainteentry.php\?id=(?<page>.+?)"}s', $this->client->get($url)->getBody()->getContents(), $matches);
50
            foreach ($matches['page'] as $page) {
51
                $pageUrl = 'http://support.sakura.ad.jp/mainte/mainteentry.php?id=' . $page;
52
                $contents = $this->client->get($pageUrl)->getBody()->getContents();
53
                preg_match_all('{(?<ipaddress>[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)}s', $contents, $matches);
54
55
                $onPageIpAddress = $matches['ipaddress'];
56
57
                foreach(array_keys(self::$ipAddress) as $type) {
58
                    if (array_key_exists($type, self::$ipAddress)) {
59
                        $result = array_filter(self::$ipAddress[$type], function ($ipAddress) use ($onPageIpAddress) {
60
                            return in_array($ipAddress, $onPageIpAddress);
61
                        });
62
                        if (count($result) !== 0) {
63
                            $existsIpAddress[$type][$pageUrl] = $result;
64
                        }
65
                    }
66
                }
67
                usleep(500000);
68
            }
69
70
71
        } catch (\Exception $e) {
72
            $this->error .= "{$e->getMessage()}\n";
73
        }
74
75
        return $existsIpAddress;
76
    }
77
78
    public function getException()
79
    {
80
        if ($this->error !== '') {
81
            throw new \RuntimeException($this->error);
82
        }
83
    }
84
}
85