ParseUsersCommand::execute()   B
last analyzed

Complexity

Conditions 7
Paths 4

Size

Total Lines 60
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 60
ccs 0
cts 43
cp 0
rs 7.4661
cc 7
eloc 37
nc 4
nop 2
crap 56

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Comrade42\PhpBBParser\Command;
4
5
use Goutte\Client;
6
use Symfony\Component\Console\Input\InputInterface;
7
use Symfony\Component\Console\Output\OutputInterface;
8
use Symfony\Component\DomCrawler\Crawler;
9
10
/**
11
 * Class ParseUsersCommand
12
 * @package Comrade42\PhpBBParser\Command
13
 */
14
class ParseUsersCommand extends ContainerAwareCommand
15
{
16
    const USERS_PER_PAGE = 50;
17
18
    protected function configure()
19
    {
20
        $this->setName('parse:users')->setDescription('Parse users');
21
    }
22
23
    protected function execute(InputInterface $input, OutputInterface $output)
24
    {
25
        /** @var \Doctrine\ORM\EntityManager $entityManager */
26
        $entityManager = $this->container->get('doctrine');
27
        /** @var \Comrade42\PhpBBParser\Bridge\BridgeInterface $entityBridge */
28
        $entityBridge = $this->container->get('bridge');
29
        /** @var \Symfony\Component\Console\Helper\DialogHelper $dialogHelper */
30
        $dialogHelper = $this->getHelperSet()->get('dialog');
31
32
        $client = new Client();
33
        $baseUrl = rtrim($this->container->getParameter('forum_url'), '/');
34
35
        for ($offset = 0; ; $offset += static::USERS_PER_PAGE)
36
        {
37
            $url = $baseUrl . '/memberlist?mode=joined&start=' . $offset;
38
            $output->write(str_pad("<info>⇒ HTTP GET: {$url}</info> ", 90));
39
40
            $crawler = $client->request('GET', $url);
41
42
            $status = $client->getInternalResponse()->getStatus();
43
            $output->writeln("<info>[{$status}]</info>");
44
45
            if ($status != 200)
46
            {
47
                $offset -= static::USERS_PER_PAGE;
48
                sleep(1);
49
                continue;
50
            }
51
52
            $crawler = $crawler->filter('#memberlist tbody tr');
53
            $crawler->each(function (Crawler $node) use ($output, $entityManager, $entityBridge, $dialogHelper) {
54
                $columns = $node->children();
55
                $id = substr($columns->eq(1)->filter('a')->attr('href'), 2);
56
                $nickname = substr($columns->eq(1)->text(), 2);
57
58
                $entity = $entityBridge->getMemberEntity($entityManager, $id);
59
                $memberName = $entity->getNickname();
60
61
                $question = "<question>Member name doesn't match for #{$id} ({$memberName} → {$nickname}). Update entity? [Y/n]:</question> ";
62
                if (!empty($memberName) && $memberName != $nickname && !$dialogHelper->askConfirmation($output, $question)) return;
63
64
                $regDate = \DateTime::createFromFormat('Y-m-d H:i:s', $columns->eq(3)->text() . ' 00:00:00');
65
                $avatarUrl = $columns->eq(1)->filter('a img')->attr('src');
66
                $uuid = uniqid();
67
68
                $entity->setNickname($nickname)
69
                    ->setRealName($nickname)
70
                    ->setRegDate($regDate)
0 ignored issues
show
Security Bug introduced by
It seems like $regDate defined by \DateTime::createFromFor...->text() . ' 00:00:00') on line 64 can also be of type false; however, Comrade42\PhpBBParser\En...Interface::setRegDate() does only seem to accept object<DateTime>, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
71
                    ->setPassword($uuid)
72
                    ->setEmailAddress($uuid . '@mailforspam.com')
73
                    ->setAvatarUrl($avatarUrl);
74
75
                $entityManager->persist($entity);
76
            });
77
78
            $entityManager->flush();
79
80
            if ($crawler->count() < static::USERS_PER_PAGE) break;
81
        }
82
    }
83
}
84