Passed
Push — master ( 745178...3deda8 )
by Alexey
03:44
created

ProxybonanzaTestCommand   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 83
rs 10
c 0
b 0
f 0
wmc 7

2 Methods

Rating   Name   Duplication   Size   Complexity  
B execute() 0 66 6
A configure() 0 5 1
1
<?php
2
/**
3
 * This file is part of the wow-apps/symfony-proxybonanza project
4
 * https://github.com/wow-apps/symfony-proxybonanza
5
 *
6
 * (c) 2016 WoW-Apps
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace WowApps\ProxybonanzaBundle\Command;
13
14
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
15
use Symfony\Component\Console\Input\InputInterface;
16
use Symfony\Component\Console\Output\OutputInterface;
17
use Symfony\Component\Console\Style\SymfonyStyle;
18
use WowApps\ProxybonanzaBundle\Entity\Plan;
19
use WowApps\ProxybonanzaBundle\Entity\Proxy;
20
use WowApps\ProxybonanzaBundle\Service\ProxyBonanza;
21
use WowApps\ProxybonanzaBundle\Traits\HelperTrait;
22
23
/**
24
 * Class ProxybonanzaTestCommand
25
 * @author Alexey Samara <[email protected]>
26
 * @package wow-apps/symfony-proxybonanza
27
 */
28
class ProxybonanzaTestCommand extends ContainerAwareCommand
29
{
30
    use HelperTrait;
31
32
    protected function configure()
33
    {
34
        $this
35
            ->setName('wowapps:proxybonanza:test')
36
            ->setDescription('Test all local proxies')
37
        ;
38
    }
39
40
    /**
41
     * @param InputInterface $input
42
     * @param OutputInterface $output
43
     * @return int|null|void
44
     */
45
    protected function execute(InputInterface $input, OutputInterface $output)
46
    {
47
        $timeStart = microtime(true);
48
49
        /** @var ProxyBonanza $proxyBonanza */
50
        $proxyBonanza = $this->getContainer()->get('wowapps.proxybonanza');
51
52
        $symfonyStyle = new SymfonyStyle($input, $output);
53
54
55
        $symfonyStyle->title(' P R O X Y   B O N A N Z A   T E S T ');
56
57
        $pbPlans = $proxyBonanza->getLocalPlans();
58
        $pbPlans = $proxyBonanza->getLocalPlansProxies($pbPlans);
59
60
        $brokenProxies = new \ArrayObject();
61
62
        /** @var Plan $pbPlan */
63
        foreach ($pbPlans as $pbPlan) {
64
            $symfonyStyle->section(sprintf(' Start testing local proxies of plan #%d', $pbPlan->getId()));
65
66
            $symfonyStyle->createProgressBar();
67
            $symfonyStyle->progressStart($pbPlan->getPackageHowmanyIps());
68
69
            /** @var Proxy $proxy */
70
            foreach ($pbPlan->getProxy() as $proxy) {
71
                if (!$proxyBonanza->testProxyConnection($proxy)) {
72
                    $brokenProxies->append($proxy);
73
                }
74
75
                $symfonyStyle->progressAdvance(1);
76
            }
77
78
            $symfonyStyle->progressFinish();
79
        }
80
81
        if (!$brokenProxies->count()) {
82
            $symfonyStyle->success('All proxies works!');
83
        } else {
84
            $symfonyStyle->warning(sprintf('%d local proxies doesn\'t work!', $brokenProxies->count()));
85
86
            $header = [
87
                'ip',
88
                'port',
89
                'login',
90
                'password'
91
            ];
92
93
            $body = [];
94
95
            /** @var Proxy $brokenProxy */
96
            foreach ($brokenProxies as $brokenProxy) {
97
                $body[] = [
98
                    $brokenProxy->getProxyIp(),
99
                    $brokenProxy->getProxyPortHttp(),
100
                    $brokenProxy->getPlan()->getLogin(),
101
                    $brokenProxy->getPlan()->getPassword()
102
                ];
103
            }
104
105
            $symfonyStyle->table($header, $body);
106
        }
107
108
        $symfonyStyle->note(sprintf('Command is executed in %s seconds', $this->formatSpentTime($timeStart)));
109
110
        $symfonyStyle->newLine(2);
111
    }
112
}
113