Failed Conditions
Pull Request — experimental/3.1 (#2578)
by Kentaro
52:37 queued 16:02
created

GenerateDummyDataCommand::configure()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 24
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 13
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 24
rs 8.9713
1
<?php
2
3
namespace Eccube\Command;
4
5
use Symfony\Component\Console\Input\InputArgument;
6
use Symfony\Component\Console\Input\InputInterface;
7
use Symfony\Component\Console\Input\InputOption;
8
use Symfony\Component\Console\Output\OutputInterface;
9
use Faker\Factory as Faker;
10
11
class GenerateDummyDataCommand extends \Knp\Command\Command
0 ignored issues
show
introduced by
Missing class doc comment
Loading history...
12
{
13
14
    protected $app;
15
16
    protected function configure() {
17
        $this
18
            ->setName('dummydata:generate')
19
            ->setDescription('Dummy data generator')
20
            ->addOption('with-locale', null, InputOption::VALUE_REQUIRED, 'Set to the locale.', 'ja_JP')
21
            ->addOption('without-image', null, InputOption::VALUE_NONE, 'Do not generate images.')
22
            ->addOption('with-image', null, InputOption::VALUE_REQUIRED, 'Generate image type of abstract|animals|business|cats|city|food|nightlife|fashion|people|nature|sports|technics|transport', 'cats')
23
            ->addOption('products', null, InputOption::VALUE_REQUIRED, 'Number of Products.', 100)
24
            ->addOption('orders', null, InputOption::VALUE_REQUIRED, 'Number of Orders.', 10)
25
            ->addOption('customers', null, InputOption::VALUE_REQUIRED, 'Number of Customers.', 100)
26
            ->setHelp(<<<EOF
27
The <info>%command.name%</info> command generate of dummy data.
28
29
  <info>php %command.full_name%</info>
30
31
Generate of dummy data with images.
32
33
  <info>php %command.full_name% --without-image</info>
34
35
Generate of dummy data without images, use for options to faster.
36
;
37
EOF
38
            );
39
    }
40
41
    protected function execute(InputInterface $input, OutputInterface $output) {
42
43
        $locale = $input->getOption('with-locale');
44
        $imageType = $input->getOption('with-image');
45
        $notImage = $input->getOption('without-image');
46
        $numberOfProducts = $input->getOption('products');
47
        $numberOfOrder = $input->getOption('orders');
48
        $numberOfCustomer = $input->getOption('customers');
49
50
        $this->app = $this->getSilexApplication();
51
        $this->app->register(new \Eccube\Tests\ServiceProvider\FixtureServiceProvider());
52
        $Customers = [];
53
        $Products = [];
54
55
        $faker = Faker::create($locale);
56
        for ($i = 0; $i < $numberOfCustomer; $i++) {
57
            $email = microtime(true).'.'.$faker->safeEmail;
58
            $Customer = $this->app['eccube.fixture.generator.locale']($locale)->createCustomer($email);
59
            $Customer->setBirth($faker->dateTimeBetween('-110 years', '- 5 years'));
60 View Code Duplication
            switch ($output->getVerbosity()) {
61
                case OutputInterface::VERBOSITY_QUIET:
62
                    break;
63
                case OutputInterface::VERBOSITY_NORMAL:
64
                    $output->write('C');
65
                    break;
66
                case OutputInterface::VERBOSITY_VERBOSE:
67
                case OutputInterface::VERBOSITY_VERY_VERBOSE:
68
                case OutputInterface::VERBOSITY_DEBUG:
69
                    $output->writeln('Customer: id='.$Customer->getId().' '.$Customer->getEmail());
70
                    break;
71
            }
72 View Code Duplication
            if ($output->getVerbosity() >= OutputInterface::VERBOSITY_NORMAL && ($i % 100) === 0 && $i > 0) {
73
                $output->writeln(' ...'.$i);
74
            }
75
            $Customers[] = $Customer;
76
        }
77
        for ($i = 0; $i < $numberOfProducts; $i++) {
78
            $Product = $this->app['eccube.fixture.generator.locale']($locale)->createProduct(null, 3, $notImage ? null : $imageType);
79 View Code Duplication
            switch ($output->getVerbosity()) {
80
                case OutputInterface::VERBOSITY_QUIET:
81
                    break;
82
                case OutputInterface::VERBOSITY_NORMAL:
83
                    $output->write('P');
84
                    break;
85
                case OutputInterface::VERBOSITY_VERBOSE:
86
                case OutputInterface::VERBOSITY_VERY_VERBOSE:
87
                case OutputInterface::VERBOSITY_DEBUG:
88
                    $output->writeln('Product: id='.$Product->getId().' '.$Product->getName());
89
                    break;
90
            }
91 View Code Duplication
            if ($output->getVerbosity() >= OutputInterface::VERBOSITY_NORMAL && ($i % 100) === 0 && $i > 0) {
92
                $output->writeln(' ...'.$i);
93
            }
94
            $Products[] = $Product;
95
        }
96
        $Deliveries = $this->app['eccube.repository.delivery']->findAll();
97
        $j = 0;
98
        foreach ($Customers as $Customer) {
99
            $Delivery = $Deliveries[$faker->numberBetween(0, count($Deliveries) - 1)];
100
            $Product = $Products[$faker->numberBetween(0, $numberOfProducts - 1)];
101
            $charge = $faker->randomNumber(4);
102
            $discount = $faker->randomNumber(4);
103
            for ($i = 0; $i < $numberOfOrder; $i++) {
104
                $Order = $this->app['eccube.fixture.generator.locale']($locale)->createOrder($Customer, $Product->getProductClasses()->toArray(), $Delivery, $charge, $discount);
105
                $Status = $this->app['eccube.repository.order_status']->find($faker->numberBetween(1, 8));
106
                $Order->setOrderStatus($Status);
107
                $Order->setOrderDate($faker->dateTimeThisYear());
108
                switch ($output->getVerbosity()) {
109
                    case OutputInterface::VERBOSITY_QUIET:
110
                        break;
111
                    case OutputInterface::VERBOSITY_NORMAL:
112
                        $output->write('O');
113
                        break;
114
                    case OutputInterface::VERBOSITY_VERBOSE:
115
                    case OutputInterface::VERBOSITY_VERY_VERBOSE:
116
                    case OutputInterface::VERBOSITY_DEBUG:
117
                        $output->writeln('Order: id='.$Order->getId());
118
                        break;
119
                }
120
                $this->app['orm.em']->flush($Order);
121
                $j++;
122 View Code Duplication
                if ($output->getVerbosity() >= OutputInterface::VERBOSITY_NORMAL && ($j % 100) === 0 && $j > 0) {
123
                    $output->writeln(' ...'.$j);
124
                }
125
            }
126
        }
127
        $output->writeln('');
128
        $output->writeln(sprintf("%s <info>success</info>", 'dummydata:generate'));
129
    }
130
}
131