Browse Source

Add demo contacts generator

Paweł Jedwabny 3 years ago
parent
commit
5326941896
4 changed files with 162 additions and 3 deletions
  1. 2
    1
      composer.json
  2. 50
    2
      composer.lock
  3. 1
    0
      console.php
  4. 109
    0
      console/Demo/GenerateContactsCommand.php

+ 2
- 1
composer.json View File

@@ -4,7 +4,8 @@
4 4
         "twig/twig": "^1.24",
5 5
         "symfony/console": "^2.7",
6 6
         "symfony/http-foundation": "^2.7",
7
-        "phpdocumentor/reflection-docblock": "^2.0.4"
7
+        "phpdocumentor/reflection-docblock": "^2.0.4",
8
+        "fzaninotto/faker": "^1.6"
8 9
     },
9 10
     "scripts": {
10 11
         "post-install-cmd": [

+ 50
- 2
composer.lock View File

@@ -4,10 +4,58 @@
4 4
         "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file",
5 5
         "This file is @generated automatically"
6 6
     ],
7
-    "hash": "5351ab59fd20fec1fe517dba597a09fd",
8
-    "content-hash": "dd13d59541b6ce113cd2a165874516d0",
7
+    "hash": "805a72a4fb5bc94bbc7bc7ef46685f73",
8
+    "content-hash": "a637abf974de6f43d7f8d88e92c63cdc",
9 9
     "packages": [
10 10
         {
11
+            "name": "fzaninotto/faker",
12
+            "version": "v1.6.0",
13
+            "source": {
14
+                "type": "git",
15
+                "url": "https://github.com/fzaninotto/Faker.git",
16
+                "reference": "44f9a286a04b80c76a4e5fb7aad8bb539b920123"
17
+            },
18
+            "dist": {
19
+                "type": "zip",
20
+                "url": "https://api.github.com/repos/fzaninotto/Faker/zipball/44f9a286a04b80c76a4e5fb7aad8bb539b920123",
21
+                "reference": "44f9a286a04b80c76a4e5fb7aad8bb539b920123",
22
+                "shasum": ""
23
+            },
24
+            "require": {
25
+                "php": "^5.3.3|^7.0"
26
+            },
27
+            "require-dev": {
28
+                "ext-intl": "*",
29
+                "phpunit/phpunit": "~4.0",
30
+                "squizlabs/php_codesniffer": "~1.5"
31
+            },
32
+            "type": "library",
33
+            "extra": {
34
+                "branch-alias": []
35
+            },
36
+            "autoload": {
37
+                "psr-4": {
38
+                    "Faker\\": "src/Faker/"
39
+                }
40
+            },
41
+            "notification-url": "https://packagist.org/downloads/",
42
+            "license": [
43
+                "MIT"
44
+            ],
45
+            "authors": [
46
+                {
47
+                    "name": "François Zaninotto"
48
+                }
49
+            ],
50
+            "description": "Faker is a PHP library that generates fake data for you.",
51
+            "keywords": [
52
+                "data",
53
+                "faker",
54
+                "fixtures"
55
+            ],
56
+            "time": "2016-04-29 12:21:54"
57
+        },
58
+        {
11 59
             "name": "ircmaxell/password-compat",
12 60
             "version": "v1.0.4",
13 61
             "source": {

+ 1
- 0
console.php View File

@@ -23,4 +23,5 @@ $application->add(new \Epesi\Console\Maintenance\MaintenanceOnCommand());
23 23
 $application->add(new \Epesi\Console\Maintenance\MaintenanceOffCommand());
24 24
 $application->add(new \Epesi\Console\SearchClearCommand());
25 25
 $application->add(new \Epesi\Console\SearchIndexCommand());
26
+$application->add(new \Epesi\Console\Demo\GenerateContactsCommand());
26 27
 $application->run();

+ 109
- 0
console/Demo/GenerateContactsCommand.php View File

@@ -0,0 +1,109 @@
1
+<?php
2
+
3
+namespace Epesi\Console\Demo;
4
+
5
+use DB;
6
+use ModuleManager;
7
+use Symfony\Component\Console\Command\Command;
8
+use Symfony\Component\Console\Helper\Table;
9
+use Symfony\Component\Console\Input\InputArgument;
10
+use Symfony\Component\Console\Input\InputInterface;
11
+use Symfony\Component\Console\Input\InputOption;
12
+use Symfony\Component\Console\Output\OutputInterface;
13
+use Symfony\Component\Console\Helper\ProgressBar;
14
+
15
+class GenerateContactsCommand extends Command
16
+{
17
+    protected function configure()
18
+    {
19
+        $this
20
+            ->setName('demo:generate:contacts')
21
+            ->setDescription('Generate demo contacts')
22
+            ->addOption('create-user', null, InputOption::VALUE_NONE, 'Create user')
23
+            ->addOption('create-company', null, InputOption::VALUE_NONE, 'Create company related to contact')
24
+            ->addOption('count', null, InputOption::VALUE_REQUIRED, 'Count of generated records');
25
+    }
26
+
27
+    protected function execute(InputInterface $input, OutputInterface $output)
28
+    {
29
+        \Variable::set('anonymous_setup', 1);
30
+        $count = $input->getOption('count') ?: 1;
31
+
32
+        $progress = new ProgressBar($output, $count);
33
+
34
+        $table = new Table($output);
35
+        $headers = array(
36
+            '<fg=white;options=bold>Id</fg=white;options=bold>',
37
+            '<fg=white;options=bold>First Name</fg=white;options=bold>',
38
+            '<fg=white;options=bold>Last Name</fg=white;options=bold>'
39
+        );
40
+
41
+        if($input->getOption('create-company')){
42
+            $headers[] = '<fg=white;options=bold>Company</fg=white;options=bold>';
43
+        }
44
+
45
+        if($input->getOption('create-user')) {
46
+            $headers[] = '<fg=white;options=bold>Login</fg=white;options=bold>';
47
+            $headers[] = '<fg=white;options=bold>Password</fg=white;options=bold>';
48
+        }
49
+
50
+        $table->setHeaders($headers);
51
+
52
+
53
+        $progress->start();
54
+        for ($i = 0; $i < $count; $i++) {
55
+            $faker = \Faker\Factory::create();
56
+            $values = [];
57
+            $values['submited'] = '';
58
+            $values['last_name'] = $faker->lastName;
59
+            $values['first_name'] = $faker->firstName;
60
+            $values['country'] = $faker->countryCode;
61
+            $values['permission'] = 0;
62
+            $values['title'] = $faker->title;
63
+            $values['work_phone'] = $faker->phoneNumber;
64
+            $values['mobile_phone'] = $faker->phoneNumber;
65
+            $values['fax'] = $faker->phoneNumber;
66
+            $values['email'] = $faker->email;
67
+            $values['web_address'] = $faker->url;
68
+            $values['address_1'] = $faker->streetAddress;
69
+            $values['address_2'] = $faker->streetAddress;
70
+            $values['city'] = $faker->city;
71
+            $values['postal_code'] = $faker->postcode;
72
+            $values['home_phone'] = $faker->phoneNumber;
73
+            $values['home_address_1'] = $faker->streetAddress;
74
+            $values['home_address_2'] = $faker->streetAddress;
75
+            $values['home_city'] = $faker->city;
76
+            $values['home_country'] = $faker->countryCode;
77
+            $values['home_postal_code'] = $faker->postcode;
78
+            $values['birth_date'] = $faker->dateTimeBetween($startDate = '-30 years', $endDate = 'now', $timezone = date_default_timezone_get())->format('Y-m-d');
79
+
80
+            $row = [$values['first_name'], $values['last_name']];
81
+
82
+
83
+
84
+            if ($input->getOption('create-company')) {
85
+                $values['create_company'] = 1;
86
+                $values['create_company_name'] = $faker->company;
87
+                $row[] = $values['create_company_name'];
88
+            }
89
+
90
+            if ($input->getOption('create-user')) {
91
+                $values['login'] = 'new';
92
+                $values['username'] = $faker->userName;
93
+                $values['set_password'] = $faker->password;
94
+                $values['confirm_password'] = $values['set_password'];
95
+                $values['admin'] = 0;
96
+                $row[] = $values['username'];
97
+                $row[] = $values['set_password'];
98
+            }
99
+
100
+            $id = \Utils_RecordBrowserCommon::new_record('contact', $values);
101
+            array_unshift($row, $id);
102
+            $table->addRow($row);
103
+            $progress->advance();
104
+        }
105
+        $progress->finish();
106
+        $output->write('', true);
107
+        $table->render();
108
+    }
109
+}

Loading…
Cancel
Save