ImportPacksCommand   B
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 132
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 18

Importance

Changes 2
Bugs 1 Features 1
Metric Value
wmc 11
c 2
b 1
f 1
lcom 0
cbo 18
dl 0
loc 132
rs 7.3333

3 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 6 1
A execute() 0 16 3
C updatePacks() 0 79 7
1
<?php
2
3
namespace Xdaysaysay\CoreBundle\Command;
4
5
use Doctrine\Common\Collections\ArrayCollection;
6
use GuzzleHttp\Client;
7
use Sabre\Xml\Service;
8
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
9
use Symfony\Component\Console\Helper\ProgressBar;
10
use Symfony\Component\Console\Input\InputArgument;
11
use Symfony\Component\Console\Input\InputInterface;
12
use Symfony\Component\Console\Input\InputOption;
13
use Symfony\Component\Console\Output\OutputInterface;
14
use Xdaysaysay\CoreBundle\Entity\Pack;
15
use Xdaysaysay\CoreBundle\Entity\Xdcc;
16
use Xdaysaysay\CoreBundle\Entity\XdccName;
17
use Xdaysaysay\CoreBundle\XmlMapping\Iroffer;
18
19
class ImportPacksCommand extends ContainerAwareCommand
20
{
21
    /**
22
     * @inheritdoc
23
     */
24
    protected function configure()
25
    {
26
        $this
27
            ->setName('xdaysaysay:import:packs')
28
            ->setDescription('Import packs for registered xdccs');
29
    }
30
31
    /**
32
     * @inheritdoc
33
     *
34
     * @throws \Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException
35
     * @throws \Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException
36
     * @throws \LogicException
37
     * @throws \RuntimeException
38
     * @throws \InvalidArgumentException
39
     * @throws \Sabre\Xml\ParseException
40
     */
41
    protected function execute(InputInterface $input, OutputInterface $output)
42
    {
43
        $xdccs = $this->getContainer()->get('doctrine')->getRepository('XdaysaysayCoreBundle:Xdcc')->findAll();
44
45
        $progress = new ProgressBar($output, count($xdccs));
46
        $progress->start();
47
48
        foreach ($xdccs as $xdcc) {
49
            if ($xdcc->getVisible()) {
50
                $this->updatePacks($xdcc);
51
            }
52
            $progress->advance();
53
        }
54
55
        $progress->finish();
56
    }
57
58
    /**
59
     * Update packs for a xdcc
60
     *
61
     * @param Xdcc $xdcc
62
     *
63
     * @throws \RuntimeException
64
     * @throws \Sabre\Xml\ParseException
65
     * @throws \LogicException
66
     * @throws \RuntimeException
67
     * @throws \InvalidArgumentException
68
     * @throws \Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException
69
     * @throws \Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException
70
     */
71
    private function updatePacks(Xdcc $xdcc)
72
    {
73
        $uri = $xdcc->getServer()->getFullUri().$xdcc->getUrl();
74
75
        $options = [];
76
        if ($xdcc->getServer()->getSsl()) {
77
            $options['curl.options'] = [
78
                'CURLOPT_SSL_CIPHER_LIST' => 'ECDHE-ECDSA-AES256-SHA:ECDH-ECDSA-AES256-SHA:ECDHE-ECDSA-AES128-SHA:ECDH-ECDSA-AES128-SHA:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-SHA384:ECDH-ECDSA-AES256-GCM-SHA384:ECDH-ECDSA-AES256-SHA384:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-SHA256:ECDH-ECDSA-AES128-GCM-SHA256:ECDH-ECDSA-AES128-SHA256',
79
                'CURLOPT_SSL_VERIFYPEER'  => false,
80
            ];
81
        }
82
83
        $client = new Client();
84
        $reponse = $client->get($uri, $options);
85
        $service = new Service();
86
        $service->elementMap = [
87
            '{}iroffer'   => 'Xdaysaysay\CoreBundle\XmlMapping\Iroffer',
88
            '{}packlist'  => 'Xdaysaysay\CoreBundle\XmlMapping\Packs',
89
            '{}pack'      => 'Xdaysaysay\CoreBundle\XmlMapping\Pack',
90
            '{}network'   => 'Xdaysaysay\CoreBundle\XmlMapping\Network',
91
            '{}sysinfo'   => 'Xdaysaysay\CoreBundle\XmlMapping\Sysinfo',
92
            '{}mainqueue' => 'Xdaysaysay\CoreBundle\XmlMapping\Mainqueue',
93
            '{}slots'     => 'Xdaysaysay\CoreBundle\XmlMapping\Slots',
94
            '{}idlequeue' => 'Xdaysaysay\CoreBundle\XmlMapping\Idlequeue',
95
            '{}bandwidth' => 'Xdaysaysay\CoreBundle\XmlMapping\Bandwidth',
96
            '{}quota'     => 'Xdaysaysay\CoreBundle\XmlMapping\Quota',
97
            '{}limits'    => 'Xdaysaysay\CoreBundle\XmlMapping\Limits',
98
            '{}stats'     => 'Xdaysaysay\CoreBundle\XmlMapping\Stats',
99
        ];
100
101
        /** @var Iroffer $iroffer */
102
        $iroffer = $service->parse($reponse->getBody()->getContents());
103
104
        foreach($xdcc->getXdccnames() as $xdccName) {
105
            $xdcc->removeXdccname($xdccName);
106
        }
107
        foreach($xdcc->getPacks() as $pack) {
108
            $xdcc->removePack($pack);
109
        }
110
111
        $this->getContainer()->get('doctrine')->getManager()->persist($xdcc);
112
        $this->getContainer()->get('doctrine')->getManager()->flush();
113
114
        foreach ($iroffer->packlist->packs as $pack) {
115
            $addDate = new \DateTime();
116
            $addDate->setTimestamp($pack->adddate);
117
118
            $xdccPack = new Pack();
119
            $xdccPack->setId($pack->packnr);
120
            $xdccPack->setName($pack->packname);
121
            $xdccPack->setSize($pack->packbytes);
122
            $xdccPack->setGets($pack->packgets);
123
            $xdccPack->setAdddate($addDate);
124
            $xdccPack->setMd5sum($pack->md5sum);
125
            $xdccPack->setCrc32($pack->crc32);
126
            $xdccPack->setXdcc($xdcc);
127
            $xdcc->addPack($xdccPack);
128
        }
129
130
        foreach ($iroffer->sysinfo->networks as $network) {
131
            $ircServer = $this->getContainer()->get('doctrine')->getRepository('XdaysaysayCoreBundle:IRCServer')->findOneByHost($network->servername);
132
            if($ircServer) {
133
                $xdccName = new XdccName();
134
                $xdccName->setXdcc($xdcc);
135
                $xdccName->setName($network->currentnick);
136
                $xdccName->setIrcServer($ircServer);
137
                $xdcc->addXdccname($xdccName);
138
            }
139
        }
140
141
        $lastUpdate = new \DateTime();
142
        $lastUpdate->setTimestamp($iroffer->sysinfo->stats->lastupdate);
143
        $xdcc->setLastupdate($lastUpdate);
144
        $xdcc->setTotaluptime($iroffer->sysinfo->stats->totaluptime);
145
        $xdcc->setDiskspace($iroffer->sysinfo->quota->diskspace);
146
147
        $this->getContainer()->get('doctrine')->getManager()->persist($xdcc);
148
        $this->getContainer()->get('doctrine')->getManager()->flush();
149
    }
150
}