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

Checker.php (3 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
require_once (__DIR__ . '/vendor/autoload.php');
4
5
/**
6
 * @author bootjp
7
 */
8
class Checker
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

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