GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

HydrateDataCommand::execute()   C
last analyzed

Complexity

Conditions 8
Paths 120

Size

Total Lines 76
Code Lines 43

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 76
rs 5.4648
c 0
b 0
f 0
cc 8
eloc 43
nc 120
nop 2

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 Application\Command\Database;
4
5
use Application\Entity\UserEntity;
6
use Application\Entity\ProfileEntity;
7
use Silex\Application;
8
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
9
use Symfony\Component\Console\Input\InputInterface;
10
use Symfony\Component\Console\Input\InputOption;
11
use Symfony\Component\Console\Output\OutputInterface;
12
13
/**
14
 * @author Borut Balažek <[email protected]>
15
 */
16
class HydrateDataCommand extends ContainerAwareCommand
17
{
18
    protected $app;
19
20
    public function __construct($name, Application $app)
21
    {
22
        parent::__construct($name);
23
24
        $this->app = $app;
25
    }
26
27
    protected function configure()
28
    {
29
        $this
30
            ->setName(
31
                'application:database:hydrate-data'
32
            )
33
            ->setDescription('Add an Test User to the database')
34
            ->addOption(
35
                'remove-existing-data',
36
                'r',
37
                InputOption::VALUE_NONE,
38
                'When the existing data should be removed'
39
            )
40
        ;
41
    }
42
43
    protected function execute(InputInterface $input, OutputInterface $output)
44
    {
45
        $app = $this->app;
46
47
        $removeExistingData = $input->getOption('remove-existing-data');
48
49
        if ($removeExistingData) {
50
            try {
51
                $app['db']->query('SET foreign_key_checks = 0;');
52
53
                $tables = $app['db']->getSchemaManager()->listTables();
54
55
                foreach ($tables as $table) {
56
                    $table = $table->getName();
57
58
                    $app['db']->query('TRUNCATE TABLE '.$table.';');
59
                }
60
61
                $app['db']->query('SET foreign_key_checks = 1;');
62
63
                $output->writeln('<info>All tables were successfully truncated!</info>');
64
            } catch (\Exception $e) {
65
                $output->writeln('<error>'.$e->getMessage().'</error>');
66
            }
67
        }
68
69
        /***** Users *****/
70
        $users = include APP_DIR.'/fixtures/users.php';
71
72
        foreach ($users as $user) {
73
            $userEntity = new UserEntity();
74
            $profileEntity = new ProfileEntity();
75
76
            // Profile
77
            $profileEntity
78
                ->setFirstName($user['profile']['firstName'])
79
                ->setLastName($user['profile']['lastName'])
80
            ;
81
82
            if (isset($user['profile']['gender'])) {
83
                $profileEntity
84
                    ->setGender($user['profile']['gender'])
85
                ;
86
            }
87
88
            if (isset($user['profile']['birthdate'])) {
89
                $profileEntity
90
                    ->setBirthdate($user['profile']['birthdate'])
91
                ;
92
            }
93
94
            // User
95
            $userEntity
96
                ->setId($user['id'])
97
                ->setUsername($user['username'])
98
                ->setEmail($user['email'])
99
                ->setPlainPassword(
100
                    $user['plainPassword'],
101
                    $app['security.encoder_factory']
102
                )
103
                ->setRoles($user['roles'])
104
                ->setProfile($profileEntity)
105
                ->enable()
106
            ;
107
108
            $app['orm.em']->persist($userEntity);
109
        }
110
111
        try {
112
            $app['orm.em']->flush();
113
114
            $output->writeln('<info>Data was successfully hydrated!</info>');
115
        } catch (\Exception $e) {
116
            $output->writeln('<error>'.$e->getMessage().'</error>');
117
        }
118
    }
119
}
120