Passed
Pull Request — master (#75)
by Korotkov
12:47
created

AbstractSeed::createStmtString()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 1
dl 0
loc 11
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace App\Ship\Seed;
4
5
use Rudra\Container\Facades\Rudra;
6
use Rudra\Container\Facades\Request;
7
8
abstract class AbstractSeed
9
{
10
    public function __construct()
11
    {
12
        Request::server()->set([
13
            "REMOTE_ADDR" => "127.0.0.1",
14
            "HTTP_USER_AGENT" => "Mozilla"
15
        ]);
16
    }
17
18
    abstract public function create();
19
20
    protected function createStmtString(array $fields): array
21
    {
22
        $insert = [];
23
        $execute = [];
24
25
        foreach ($fields as $key => $data) {
26
            $insert[] = "{$key}";
27
            $execute[] = ":{$key}";
28
        }
29
30
        return [implode(",", $insert), implode(",", $execute)];
31
    }
32
33
    protected function execute(string $table, array $fields): void
34
    {
35
        $stmtString = $this->createStmtString($fields);
36
37
        $query = Rudra::get("DSN")->prepare("
38
                INSERT INTO {$table} ({$stmtString[0]}) 
39
                VALUES ({$stmtString[1]})");
40
41
        $query->execute($fields);
42
    }
43
}
44