Completed
Pull Request — master (#42)
by Phecho
04:22
created

DemoSeederCommand::seedUsers()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 18
rs 9.4286
cc 2
eloc 10
nc 2
nop 0
1
<?php
2
3
/*
4
 * This file is part of Gitamin.
5
 * 
6
 * Copyright (C) 2015-2016 The Gitamin Team
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Gitamin\Console\Commands;
13
14
use Gitamin\Models\Issue;
15
use Gitamin\Models\Owner;
16
use Gitamin\Models\Project;
17
use Gitamin\Models\Setting;
18
use Gitamin\Models\Subscriber;
19
use Gitamin\Models\User;
20
use Illuminate\Console\Command;
21
use Illuminate\Console\ConfirmableTrait;
22
use Symfony\Component\Console\Input\InputOption;
23
24
/**
25
 * This is the demo seeder command.
26
 */
27
class DemoSeederCommand extends Command
28
{
29
    use ConfirmableTrait;
30
31
    /**
32
     * The console command name.
33
     *
34
     * @var string
35
     */
36
    protected $name = 'gitamin:seed';
37
38
    /**
39
     * The console command description.
40
     *
41
     * @var string
42
     */
43
    protected $description = 'Seeds Gitamin with demo data.';
44
45
    /**
46
     * Execute the console command.
47
     *
48
     * @return void
49
     */
50
    public function fire()
51
    {
52
        if (!$this->confirmToProceed()) {
53
            return;
54
        }
55
56
        $this->seedOwners();
57
        $this->seedProjects();
58
        $this->seedIssues();
59
        $this->seedSettings();
60
        $this->seedSubscribers();
61
        $this->seedUsers();
62
63
        $this->info('Database seeded with demo data successfully!');
64
    }
65
66
    /**
67
     * Seed the project teams table.
68
     *
69
     * @return void
70
     */
71
    protected function seedOwners()
72
    {
73
        $defaultOwners = [
74
            [
75
                'name'        => 'Baidu Corp',
76
                'path'        => 'Baidu',
77
                'description' => 'www.baidu.com',
78
                'type'        => 'Group',
79
            ],
80
        ];
81
82
        Owner::truncate();
83
84
        foreach ($defaultOwners as $owner) {
85
            Owner::create($owner);
86
        }
87
    }
88
89
    /**
90
     * Seed the projects table.
91
     *
92
     * @return void
93
     */
94
    protected function seedProjects()
95
    {
96
        $defaultProjects = [
97
            [
98
                'name'             => 'API',
99
                'description'      => 'Used by third-parties to connect to us',
100
                'visibility_level' => 0,
101
                'owner_id'         => 1,
102
                'path'             => 'Gitamin',
103
            ], [
104
                'name'             => 'Documentation',
105
                'description'      => 'Kindly powered by Readme.io',
106
                'visibility_level' => 1,
107
                'owner_id'         => 1,
108
                'path'             => 'Baidu',
109
            ], [
110
                'name'             => 'Website',
111
                'description'      => '',
112
                'visibility_level' => 1,
113
                'owner_id'         => 1,
114
                'path'             => 'Alibaba',
115
            ], [
116
                'name'             => 'Blog',
117
                'description'      => 'The Gitamin Blog.',
118
                'visibility_level' => 1,
119
                'owner_id'         => 1,
120
                'path'             => 'Tencent',
121
            ],
122
        ];
123
124
        Project::truncate();
125
126
        foreach ($defaultProjects as $project) {
127
            Project::create($project);
128
        }
129
    }
130
131
    /**
132
     * Seed the issues table.
133
     *
134
     * @return void
135
     */
136
    protected function seedIssues()
137
    {
138
        $defaultIssues = [
139
            [
140
                'title'       => 'Awesome',
141
                'description' => ':+1: We totally nailed the fix.',
142
                'author_id'   => 1,
143
                'project_id'  => 1,
144
            ],
145
            [
146
                'title'       => 'Monitoring the fix',
147
                'description' => ":ship: We've deployed a fix.",
148
                'author_id'   => 3,
149
                'project_id'  => 2,
150
            ],
151
            [
152
                'title'       => 'Update',
153
                'description' => "We've identified the problem. Our engineers are currently looking at it.",
154
                'author_id'   => 2,
155
                'project_id'  => 1,
156
            ],
157
            [
158
                'title'       => 'Test Issue',
159
                'description' => 'Something went wrong, with something or another.',
160
                'author_id'   => 1,
161
                'project_id'  => 2,
162
            ],
163
            [
164
                'title'       => 'Investigating the API',
165
                'description' => ':zap: We\'ve seen high response times from our API. It looks to be fixing itself as time goes on.',
166
                'author_id'   => 1,
167
                'project_id'  => 3,
168
            ],
169
        ];
170
171
        Issue::truncate();
172
173
        foreach ($defaultIssues as $issue) {
174
            Issue::create($issue);
175
        }
176
    }
177
178
    /**
179
     * Seed the settings table.
180
     *
181
     * @return void
182
     */
183
    protected function seedSettings()
184
    {
185
        $defaultSettings = [
186
            [
187
                'name'  => 'app_name',
188
                'value' => 'Gitamin Demo',
189
            ],
190
            [
191
                'name'  => 'app_domain',
192
                'value' => 'https://demo.gitamin.com',
193
            ],
194
            [
195
                'name'  => 'app_locale',
196
                'value' => 'en',
197
            ],
198
            [
199
                'name'  => 'app_timezone',
200
                'value' => 'Asia/Shanghai',
201
            ],
202
            [
203
                'name'  => 'app_issue_days',
204
                'value' => '7',
205
            ],
206
        ];
207
208
        Setting::truncate();
209
210
        foreach ($defaultSettings as $setting) {
211
            Setting::create($setting);
212
        }
213
    }
214
215
    /**
216
     * Seed the subscribers.
217
     *
218
     * @return void
219
     */
220
    protected function seedSubscribers()
221
    {
222
        Subscriber::truncate();
223
    }
224
225
    /**
226
     * Seed the users table.
227
     *
228
     * @return void
229
     */
230
    protected function seedUsers()
231
    {
232
        $users = [
233
            [
234
                'username' => 'test',
235
                'password' => 'test123',
236
                'email'    => '[email protected]',
237
                'level'    => 1,
238
                'api_key'  => '9yMHsdioQosnyVK4iCVR',
239
            ],
240
        ];
241
242
        User::truncate();
243
244
        foreach ($users as $user) {
245
            User::create($user);
246
        }
247
    }
248
249
    /**
250
     * Get the console command options.
251
     *
252
     * @return array
253
     */
254
    protected function getOptions()
255
    {
256
        return [
257
            ['force', null, InputOption::VALUE_NONE, 'Force the operation to run when in production.'],
258
        ];
259
    }
260
}
261