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

ProxybonanzaUpdateCommand::execute()   B

Complexity

Conditions 3
Paths 2

Size

Total Lines 27
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 12
nc 2
nop 2
dl 0
loc 27
rs 8.8571
c 0
b 0
f 0
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\Input\InputOption;
17
use Symfony\Component\Console\Output\OutputInterface;
18
use Symfony\Component\Console\Style\SymfonyStyle;
19
use WowApps\ProxybonanzaBundle\Entity\Plan;
20
use WowApps\ProxybonanzaBundle\Entity\Proxy;
21
use WowApps\ProxybonanzaBundle\Service\ProxyBonanza;
22
use WowApps\ProxybonanzaBundle\Traits\HelperTrait;
23
24
/**
25
 * Class ProxybonanzaUpdateCommand
26
 * @author Alexey Samara <[email protected]>
27
 * @package wow-apps/symfony-proxybonanza
28
 */
29
class ProxybonanzaUpdateCommand extends ContainerAwareCommand
30
{
31
    use HelperTrait;
32
33
    protected function configure()
34
    {
35
        $this
36
            ->setName('wowapps:proxybonanza:update')
37
            ->setDescription('Update proxy list from ProxyBonanza')
38
            ->addOption('skip-tests', null, InputOption::VALUE_NONE, 'Skip test of every remote proxy')
39
        ;
40
    }
41
42
    /**
43
     * @param InputInterface $input
44
     * @param OutputInterface $output
45
     * @return int|null|void
46
     */
47
    protected function execute(InputInterface $input, OutputInterface $output)
48
    {
49
        $timeStart = microtime(true);
50
51
        /** @var ProxyBonanza $proxyBonanza */
52
        $proxyBonanza = $this->getContainer()->get('wowapps.proxybonanza');
53
54
        $symfonyStyle = new SymfonyStyle($input, $output);
55
56
57
        $symfonyStyle->title(' P R O X Y   B O N A N Z A   U P D A T E ');
58
59
        $pbPlans = $this->getRemotePlans($proxyBonanza, $symfonyStyle);
60
61
        if (!$input->getOption('skip-tests') && $pbPlans->count()) {
62
            $this->testProxies($proxyBonanza, $pbPlans, $symfonyStyle);
63
        }
64
65
        $symfonyStyle->section('Updating local data from remote ...');
66
67
        $proxyBonanza->updateLocalDataFromRemote($pbPlans);
68
69
        $symfonyStyle->success('Proxy list has been updated');
70
71
        $symfonyStyle->note(sprintf('Command is executed in %s seconds', $this->formatSpentTime($timeStart)));
72
73
        $symfonyStyle->newLine(2);
74
    }
75
76
    /**
77
     * @param ProxyBonanza $proxyBonanza
78
     * @param SymfonyStyle $symfonyStyle
79
     * @return \ArrayObject|Plan[]
80
     */
81
    private function getRemotePlans(ProxyBonanza $proxyBonanza, SymfonyStyle $symfonyStyle)
82
    {
83
84
        $symfonyStyle->section(' Getting remote data from ProxyBonanza ');
85
86
        $symfonyStyle->createProgressBar(100);
87
        $symfonyStyle->progressStart(0);
88
89
        $pbPlans = $proxyBonanza->getRemotePlans();
90
        if (!$pbPlans->count()) {
91
            return $pbPlans;
92
        }
93
94
        $symfonyStyle->progressAdvance(50);
95
96
        $pbPlans = $proxyBonanza->getRemotePacks($pbPlans);
97
98
        $symfonyStyle->progressAdvance(50);
99
100
        $symfonyStyle->progressFinish();
101
102
        $header = [
103
            'id',
104
            'login',
105
            'password',
106
            'expires',
107
            'bandwidth',
108
            'last ip change',
109
            'name',
110
            'bandwidth',
111
            'price',
112
            'ip\'s count',
113
            'price per gig',
114
            'type'
115
        ];
116
117
        $body = [];
118
119
        /** @var Plan $pbPlan */
120
        foreach ($pbPlans as $pbPlan) {
121
            $body[] = [
122
                $pbPlan->getId(),
123
                $pbPlan->getLogin(),
124
                $pbPlan->getPassword(),
125
                $pbPlan->getExpires()->format('Y-m-d H:i'),
126
                $this->formatSizeUnits($pbPlan->getBandwidth(), 2),
127
                $pbPlan->getLastIpChange()->format('Y-m-d H:i'),
128
                $pbPlan->getPackageName(),
129
                $this->formatSizeUnits($pbPlan->getPackageBandwidth(), 2),
130
                $pbPlan->getPackagePrice(),
131
                $pbPlan->getPackageHowmanyIps(),
132
                $pbPlan->getPackagePricePerGig(),
133
                $pbPlan->getPackageType()
134
            ];
135
        }
136
137
        $symfonyStyle->table($header, $body);
138
139
        return $pbPlans;
140
    }
141
142
    /**
143
     * @param ProxyBonanza $proxyBonanza
144
     * @param \ArrayObject|Plan[] $pbPlans
145
     * @param SymfonyStyle $symfonyStyle
146
     */
147
    private function testProxies(
148
        ProxyBonanza $proxyBonanza,
149
        \ArrayObject $pbPlans,
150
        SymfonyStyle $symfonyStyle
151
    ) {
152
        /** @var Plan $pbPlans */
153
        foreach ($pbPlans as $pbPlan) {
154
            $brokenProxies = new \ArrayObject();
155
156
            $symfonyStyle->section(sprintf(' Testing remote proxies of plan #%d ', $pbPlan->getId()));
157
158
            $symfonyStyle->createProgressBar();
159
            $symfonyStyle->progressStart($pbPlan->getPackageHowmanyIps());
160
161
            /** @var Proxy $proxy */
162
            foreach ($pbPlan->getProxy() as $proxy) {
163
                if (!$proxyBonanza->testProxyConnection($proxy)) {
164
                    $brokenProxies->append($proxy);
165
                }
166
167
                $symfonyStyle->progressAdvance(1);
168
            }
169
170
            $symfonyStyle->progressFinish();
171
172
            if ($brokenProxies->count()) {
173
                $symfonyStyle->warning(sprintf('%d remote proxies doesn\'t work!', $brokenProxies->count()));
174
175
                $header = [
176
                    'ip',
177
                    'port',
178
                    'login',
179
                    'password'
180
                ];
181
182
                $body = [];
183
184
                /** @var Proxy $brokenProxy */
185
                foreach ($brokenProxies as $brokenProxy) {
186
                    $body[] = [
187
                        $brokenProxy->getProxyIp(),
188
                        $brokenProxy->getProxyPortHttp(),
189
                        $brokenProxy->getPlan()->getLogin(),
190
                        $brokenProxy->getPlan()->getPassword()
191
                    ];
192
                }
193
194
                $symfonyStyle->table($header, $body);
195
            } else {
196
                $symfonyStyle->success('All proxies works!');
197
            }
198
        }
199
    }
200
}
201