Passed
Push — master ( 5403d4...38a665 )
by Ferry
04:29
created

SelectTable::optionsFromTable()   B

Complexity

Conditions 7
Paths 12

Size

Total Lines 26
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 17
nc 12
nop 4
dl 0
loc 26
rs 8.8333
c 0
b 0
f 0
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: User
5
 * Date: 1/26/2019
6
 * Time: 6:00 PM
7
 */
8
9
namespace crocodicstudio\crudbooster\types;
10
11
use crocodicstudio\crudbooster\controllers\scaffolding\traits\DefaultOption;
12
use crocodicstudio\crudbooster\controllers\scaffolding\traits\Join;
13
use crocodicstudio\crudbooster\models\ColumnModel;
14
use crocodicstudio\crudbooster\types\select_table\SelectTableModel;
15
use Illuminate\Support\Facades\DB;
16
17
class SelectTable
18
{
19
    use DefaultOption, Join;
20
21
    /**
22
     * @param string $field_name
23
     * @return $this
24
     */
25
    public function foreignKey($field_name)
26
    {
27
        $data = columnSingleton()->getColumn($this->index);
28
        /** @var SelectTableModel $data */
29
        $data->setForeignKey($field_name);
30
        columnSingleton()->setColumn($this->index, $data);
31
32
        return $this;
33
    }
34
35
    /**
36
     * @param $table string|Model
0 ignored issues
show
Bug introduced by
The type crocodicstudio\crudbooster\types\Model was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
37
     * @param $value_option string
38
     * @param $display_option string
39
     * @param $SQLCondition string|callable DB Query Builder|SQL Raw
40
     * @return $this
41
     */
42
    public function optionsFromTable($table, $value_option, $display_option, $SQLCondition = null) {
43
44
        if(strpos($table,"App\Models")!==false) {
45
            $table = new $table();
46
            $table = $table::$tableName;
47
        }
48
49
        $data = DB::table($table);
50
        if($SQLCondition && is_callable($SQLCondition)) {
51
            $data = call_user_func($SQLCondition, $data);
52
        }elseif ($SQLCondition && is_string($SQLCondition)) {
53
            $data->whereRaw($SQLCondition);
54
        }
55
        $data = $data->get();
56
        $options = [];
57
        foreach ($data as $d) {
58
            $options[ $d->$value_option ] = $d->$display_option;
59
        }
60
        $data = columnSingleton()->getColumn($this->index);
61
        /** @var $data SelectTableModel */
62
        $data->setOptionsFromTable(["table"=>$table,"key_field"=>$value_option,"display_field"=>$display_option,"sql_condition"=>$SQLCondition]);
63
        columnSingleton()->setColumn($this->index, $data);
64
65
        $this->options($options);
66
67
        return $this;
68
    }
69
70
    private function options($data_options) {
71
        $data = columnSingleton()->getColumn($this->index);
72
        /** @var $data SelectTableModel */
73
        $data->setOptions($data_options);
74
75
        columnSingleton()->setColumn($this->index, $data);
76
77
        return $this;
78
    }
79
}