Completed
Push — master ( 9180b0...5e386e )
by Phecho
05:41 queued 01:12
created

DemoSeederCommand   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 342
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

Changes 8
Bugs 2 Features 1
Metric Value
wmc 18
c 8
b 2
f 1
lcom 1
cbo 9
dl 0
loc 342
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A fire() 0 17 2
B seedOwners() 0 39 2
B seedProjects() 0 36 2
B seedIssues() 0 41 2
B seedComments() 0 46 2
B seedMoments() 0 27 2
B seedSettings() 0 31 2
A seedSubscribers() 0 4 1
A seedUsers() 0 18 2
A getOptions() 0 6 1
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\Comment;
15
use Gitamin\Models\Issue;
16
use Gitamin\Models\Moment;
17
use Gitamin\Models\Owner;
18
use Gitamin\Models\Project;
19
use Gitamin\Models\Setting;
20
use Gitamin\Models\Subscriber;
21
use Gitamin\Models\User;
22
use Illuminate\Console\Command;
23
use Illuminate\Console\ConfirmableTrait;
24
use Symfony\Component\Console\Input\InputOption;
25
26
/**
27
 * This is the demo seeder command.
28
 */
29
class DemoSeederCommand extends Command
30
{
31
    use ConfirmableTrait;
32
33
    /**
34
     * The console command name.
35
     *
36
     * @var string
37
     */
38
    protected $name = 'gitamin:seed';
39
40
    /**
41
     * The console command description.
42
     *
43
     * @var string
44
     */
45
    protected $description = 'Seeds Gitamin with demo data.';
46
47
    /**
48
     * Execute the console command.
49
     *
50
     * @return void
51
     */
52
    public function fire()
53
    {
54
        if (!$this->confirmToProceed()) {
55
            return;
56
        }
57
58
        $this->seedOwners();
59
        $this->seedProjects();
60
        $this->seedIssues();
61
        $this->seedComments();
62
        $this->seedMoments();
63
        $this->seedSettings();
64
        $this->seedSubscribers();
65
        $this->seedUsers();
66
67
        $this->info('Database seeded with demo data successfully!');
68
    }
69
70
    /**
71
     * Seed the project teams table.
72
     *
73
     * @return void
74
     */
75
    protected function seedOwners()
76
    {
77
        $defaultOwners = [
78
            [
79
                'name'        => 'Baidu',
80
                'path'        => 'Baidu',
81
                'user_id'     => 1,
82
                'description' => 'www.baidu.com',
83
                'type'        => 'Group',
84
            ],
85
            [
86
                'name'        => 'Alibaba',
87
                'path'        => 'Alibaba',
88
                'user_id'     => 1,
89
                'description' => 'www.alibaba.com',
90
                'type'        => 'Group',
91
            ],
92
            [
93
                'name'        => 'Tencent',
94
                'path'        => 'Tencent',
95
                'user_id'     => 1,
96
                'description' => 'www.qq.com',
97
                'type'        => 'Group',
98
            ],
99
            [
100
                'name'        => 'demo',
101
                'path'        => 'demo',
102
                'user_id'     => 1,
103
                'description' => '',
104
                'type'        => 'User',
105
            ],
106
        ];
107
108
        Owner::truncate();
109
110
        foreach ($defaultOwners as $owner) {
111
            Owner::create($owner);
112
        }
113
    }
114
115
    /**
116
     * Seed the projects table.
117
     *
118
     * @return void
119
     */
120
    protected function seedProjects()
121
    {
122
        $defaultProjects = [
123
            [
124
                'name'             => 'API',
125
                'description'      => 'Used by third-parties to connect to us',
126
                'visibility_level' => 0,
127
                'owner_id'         => 1,
128
                'path'             => 'api',
129
            ], [
130
                'name'             => 'Documentation',
131
                'description'      => 'Kindly powered by Readme.io',
132
                'visibility_level' => 1,
133
                'owner_id'         => 2,
134
                'path'             => 'doc',
135
            ], [
136
                'name'             => 'Website',
137
                'description'      => 'Tencent Holdings Limited is a Chinese investment holding company',
138
                'visibility_level' => 1,
139
                'owner_id'         => 3,
140
                'path'             => 'website',
141
            ], [
142
                'name'             => 'Blog',
143
                'description'      => 'The Gitamin Blog.',
144
                'visibility_level' => 1,
145
                'owner_id'         => 4,
146
                'path'             => 'blog',
147
            ],
148
        ];
149
150
        Project::truncate();
151
152
        foreach ($defaultProjects as $project) {
153
            Project::create($project);
154
        }
155
    }
156
157
    /**
158
     * Seed the issues table.
159
     *
160
     * @return void
161
     */
162
    protected function seedIssues()
163
    {
164
        $defaultIssues = [
165
            [
166
                'title'       => 'Awesome',
167
                'description' => ':+1: We totally nailed the fix.',
168
                'author_id'   => 1,
169
                'project_id'  => 1,
170
            ],
171
            [
172
                'title'       => 'Monitoring the fix',
173
                'description' => ":ship: We've deployed a fix.",
174
                'author_id'   => 3,
175
                'project_id'  => 2,
176
            ],
177
            [
178
                'title'       => 'Update',
179
                'description' => "We've identified the problem. Our engineers are currently looking at it.",
180
                'author_id'   => 2,
181
                'project_id'  => 1,
182
            ],
183
            [
184
                'title'       => 'Test Issue',
185
                'description' => 'Something went wrong, with something or another.',
186
                'author_id'   => 1,
187
                'project_id'  => 2,
188
            ],
189
            [
190
                'title'       => 'Investigating the API',
191
                'description' => ':zap: We\'ve seen high response times from our API. It looks to be fixing itself as time goes on.',
192
                'author_id'   => 1,
193
                'project_id'  => 3,
194
            ],
195
        ];
196
197
        Issue::truncate();
198
199
        foreach ($defaultIssues as $issue) {
200
            Issue::create($issue);
201
        }
202
    }
203
204
    /**
205
     * Seed the comments table.
206
     *
207
     * @return void
208
     */
209
    protected function seedComments()
210
    {
211
        $defaultComments = [
212
            [
213
                'message'     => ':+1: We totally nailed the fix.',
214
                'target_type' => 'Issue',
215
                'target_id'   => 3,
216
                'author_id'   => 1,
217
                'project_id'  => 1,
218
            ],
219
            [
220
                'message'     => ":ship: We've deployed a fix.",
221
                'target_type' => 'MergeRequest',
222
                'target_id'   => 1,
223
                'author_id'   => 3,
224
                'project_id'  => 2,
225
            ],
226
            [
227
                'message'     => "We've identified the problem. Our engineers are currently looking at it.",
228
                'target_type' => 'Issue',
229
                'target_id'   => 1,
230
                'author_id'   => 2,
231
                'project_id'  => 1,
232
            ],
233
            [
234
                'message'     => 'Something went wrong, with something or another.',
235
                'target_type' => 'Issue',
236
                'target_id'   => 1,
237
                'author_id'   => 1,
238
                'project_id'  => 2,
239
            ],
240
            [
241
                'message'     => ':zap: We\'ve seen high response times from our API. It looks to be fixing itself as time goes on.',
242
                'target_type' => 'MergeRequest',
243
                'target_id'   => 1,
244
                'author_id'   => 1,
245
                'project_id'  => 3,
246
            ],
247
        ];
248
249
        Comment::truncate();
250
251
        foreach ($defaultComments as $comment) {
252
            Comment::create($comment);
253
        }
254
    }
255
256
     /**
257
     * Seed the comments table.
258
     *
259
     * @return void
260
     */
261
    protected function seedMoments()
262
    {
263
        $defaultMoments = [
264
            [
265
                'message'     => ':+1: We totally nailed the fix.',
266
                'target_type' => 'Issue',
267
                'target_id'   => 3,
268
                'action'      => Moment::COMMENTED,
269
                'author_id'   => 1,
270
                'project_id'  => 1,
271
            ],
272
            [
273
                'message'     => ":ship: We've deployed a fix.",
274
                'target_type' => 'Issue',
275
                'target_id'   => 2,
276
                'action'      => Moment::CREATED,
277
                'author_id'   => 1,
278
                'project_id'  => 2,
279
            ],
280
        ];
281
282
        Moment::truncate();
283
284
        foreach ($defaultMoments as $moment) {
285
            Moment::create($moment);
286
        }
287
    }
288
    /**
289
     * Seed the settings table.
290
     *
291
     * @return void
292
     */
293
    protected function seedSettings()
294
    {
295
        $defaultSettings = [
296
            [
297
                'name'  => 'app_name',
298
                'value' => 'Gitamin Demo',
299
            ],
300
            [
301
                'name'  => 'app_domain',
302
                'value' => 'https://demo.gitamin.com',
303
            ],
304
            [
305
                'name'  => 'app_locale',
306
                'value' => 'en',
307
            ],
308
            [
309
                'name'  => 'app_timezone',
310
                'value' => 'Asia/Shanghai',
311
            ],
312
            [
313
                'name'  => 'app_issue_days',
314
                'value' => '7',
315
            ],
316
        ];
317
318
        Setting::truncate();
319
320
        foreach ($defaultSettings as $setting) {
321
            Setting::create($setting);
322
        }
323
    }
324
325
    /**
326
     * Seed the subscribers.
327
     *
328
     * @return void
329
     */
330
    protected function seedSubscribers()
331
    {
332
        Subscriber::truncate();
333
    }
334
335
    /**
336
     * Seed the users table.
337
     *
338
     * @return void
339
     */
340
    protected function seedUsers()
341
    {
342
        $users = [
343
            [
344
                'username' => 'demo',
345
                'password' => 'demo',
346
                'email'    => '[email protected]',
347
                'level'    => 1,
348
                'api_key'  => '9yMHsdioQosnyVK4iCVR',
349
            ],
350
        ];
351
352
        User::truncate();
353
354
        foreach ($users as $user) {
355
            User::create($user);
356
        }
357
    }
358
359
    /**
360
     * Get the console command options.
361
     *
362
     * @return array
363
     */
364
    protected function getOptions()
365
    {
366
        return [
367
            ['force', null, InputOption::VALUE_NONE, 'Force the operation to run when in production.'],
368
        ];
369
    }
370
}
371