FreeProxyListProvider::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php declare(strict_types = 1);
2
3
namespace Wolnosciowiec\WebProxy\Providers\Proxy;
4
5
use Goutte\Client;
6
use Symfony\Component\DomCrawler\Crawler;
7
use Wolnosciowiec\WebProxy\Entity\ProxyServerAddress;
8
9
/**
10
 * @package Wolnosciowiec\WebProxy\Providers\Proxy
11
 */
12
class FreeProxyListProvider implements ProxyProviderInterface
13
{
14
    /**
15
     * @var Client $client
16
     */
17
    private $client;
18
19
    public function __construct(Client $client)
20
    {
21
        $this->client = $client;
22
    }
23
24
    /**
25
     * @inheritdoc
26
     */
27
    public function collectAddresses(): array
28
    {
29
        $crawler = $this->client->request('GET', 'http://free-proxy-list.net');
30
        $rows = $crawler->filter('#proxylisttable tr');
31
32
        $addresses = $rows->each(function (Crawler $node) {
33
            $collection = $node->filter('td');
34
35
            // cells mapping
36
            $lastVerificationTime = @$collection->getNode(7)->textContent;
37
            $proxyType = @$collection->getNode(4)->textContent;
38
            $proxyPort = (int)@$collection->getNode(1)->textContent;
39
            $proxyIP   = @$collection->getNode(0)->textContent;
40
            $proxySchema =@ $collection->getNode(6)->textContent == 'yes' ? 'https' : 'http';
41
42
            if (!$proxyIP || !$proxyPort || strlen($lastVerificationTime) === 0 || !$proxyType) {
43
                return null;
44
            }
45
46
            if ($this->isEnoughFresh($lastVerificationTime) === false) {
47
                return null;
48
            }
49
50
            if ($proxyType != 'elite proxy') {
51
                return null;
52
            }
53
54
            $address = new ProxyServerAddress();
55
            $address->setAddress($proxyIP);
56
            $address->setPort($proxyPort);
57
            $address->setSchema($proxySchema);
58
59
            return $address;
60
        });
61
62
        return array_filter($addresses);
63
    }
64
65
    /**
66
     * @param string $time eg. "2 seconds", "1 minute", "2 minutes"
67
     * @return bool
68
     */
69
    private function isEnoughFresh(string $time): bool
70
    {
71
        $parts = explode(' ', $time);
72
        $multiply = 60 * 60 * 24 * 2;
73
74
        if (in_array($parts[1], ['minute', 'minutes'])) {
75
            $multiply = 60;
76
        }
77 View Code Duplication
        elseif (in_array($parts[1], ['hour', 'hours'])) {
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...
78
            $multiply = 60 * 60;
79
        }
80 View Code Duplication
        elseif (in_array($parts[1], ['day', 'days'])) {
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...
81
            $multiply = 60 * 60 * 24;
82
        }
83
        elseif (in_array($parts[1], ['second', 'seconds'])) {
84
            $multiply = 1;
85
        }
86
87
        return ((int)$parts[0] * $multiply) <= (60 * 5); // 5 minutes
88
    }
89
}
90