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

AbstractSeed   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 15
dl 0
loc 34
rs 10
c 0
b 0
f 0
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A execute() 0 9 1
A __construct() 0 5 1
A createStmtString() 0 11 2
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