Completed
Branch master (4042af)
by Aleksandar
02:32
created

AdminUsers::up()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 36
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 30
nc 1
nop 0
dl 0
loc 36
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
use MysqlUuid\Formats\Binary;
4
use MysqlUuid\Uuid;
5
use Phinx\Migration\AbstractMigration;
6
7
class AdminUsers extends AbstractMigration
8
{
9
    public function up()
10
    {
11
        $this->table('admin_users', ['id' => false, 'primary_key' => 'admin_user_uuid'])
12
            ->addColumn('admin_user_uuid', 'binary', ['limit' => 16])
13
            ->addColumn('admin_user_id', 'text')
14
            ->addColumn('first_name', 'text')
15
            ->addColumn('last_name', 'text')
16
            ->addColumn('introduction', 'text', ['null' => true])
17
            ->addColumn('biography', 'text', ['null' => true])
18
            ->addColumn('email', 'string', ['limit' => 128])
19
            ->addColumn('password', 'char', ['limit' => 60])
20
            ->addColumn('status', 'integer', ['default' => 0])// 0 => not active, 1 = active
21
            ->addColumn('face_img', 'text', ['null' => true])
22
            ->addColumn('profile_img', 'text', ['null' => true])
23
            ->addColumn('created_at', 'datetime', ['default' => 'CURRENT_TIMESTAMP'])
24
            ->addColumn('last_login', 'datetime', ['null' => true])
25
            ->addIndex(['email'], ['name' => 'email_INDEX'])
26
            ->create();
27
28
        // Insert one default admin user with password testtest
29
        $faker = Faker\Factory::create();
30
        $id = $faker->uuid;
31
        $mysqlUuid = (new Uuid($id))->toFormat(new Binary());
32
33
        $this->insert('admin_users', [
34
            'admin_user_uuid' => $mysqlUuid,
35
            'admin_user_id'   => $id,
36
            'first_name'      => 'Unfinished',
37
            'last_name'       => 'Admin',
38
            'email'           => '[email protected]',
39
            'password'        => '$2y$10$jhGH8RXl269ho1CrLaDiregVuW84HegLHmBFUCKTgDQTH2XgPZyBK',
40
            'status'          => 1,
41
            'face_img'        => $this->getImage($faker, 'people'),
42
            'profile_img'     => $this->getImage($faker, 'sports'),
43
        ]);
44
    }
45
46
    public function down()
47
    {
48
        $this->dropTable('admin_users');
49
    }
50
51
    private function getImage($faker, $type = 'people', $width = 400, $height = 400)
52
    {
53
        $conf = include __DIR__.'/../../../config/autoload/local.php';
54
        $p = $conf['upload']['public_path'];
55
        $upload = new \UploadHelper\Upload($p, '');
56
        $filename = md5(rand()).'.jpg';
57
        $path = $p.$filename[0].'/'.$filename[1].'/'.$filename[2].'/'.$filename;
58
        $upload->getPath($filename); // create sub folders
59
        $img = file_get_contents($faker->imageUrl(400, 400, $type));
60
        file_put_contents($path, $img);
61
62
        return $upload->getWebPath($filename);
63
    }
64
}
65