Completed
Push — dev ( a6e664...9a28bf )
by Marc
02:08
created

hasOne   A

Complexity

Total Complexity 27

Size/Duplication

Total Lines 136
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 14
Bugs 3 Features 1
Metric Value
wmc 27
c 14
b 3
f 1
lcom 1
cbo 4
dl 0
loc 136
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A guessRelatedMethod() 0 14 4
D select() 0 36 9
B buttons() 0 29 2
A getData() 0 3 1
A input() 0 9 2
D show() 0 34 9
1
<?php namespace Mascame\Artificer\Fields\Types\Relations;
2
3
use Form;
4
use Illuminate\Support\Str;
5
use Input;
6
use Request;
7
use URL;
8
9
class hasOne extends Relation
10
{
11
    protected $id;
12
13
    public function guessRelatedMethod() {
14
        // case 'model_id'
15
        $method = str_replace('_id', '', $this->name);
16
17
        if ($this->modelHasMethod($method)) return $method;
18
19
        // case 'my_current_model_id'
20
        $method = explode('_', $this->name);
21
        $method = isset(array_reverse($method)[1]) ? array_reverse($method)[1] : null;
22
23
        if ($this->modelHasMethod($method)) return $method;
24
25
        return null;
26
    }
27
28
    protected function select($data, $show)
29
    {
30
        $select = array();
31
        foreach ($data as $d) {
32
33
            if (is_array($show)) {
34
                $value = '';
35
                foreach ($show as $show_key) {
36
                    $value .= Str::title($show_key) . ': ' . $d[$show_key];
37
38
                    if (end($show) != $show_key) {
39
                        $value .= ' | ';
40
                    }
41
                }
42
            } elseif (is_callable($show)) {
43
                $value = $show($d);
44
            } else {
45
                $value = $d[$show];
46
            }
47
48
            $select[$d['id']] = $value;
49
        }
50
51
        if (Input::has($this->name)) {
52
            $this->id = Input::get($this->name);
53
        } else {
54
            if (isset($this->value->id)) {
55
                $this->id = $this->value->id;
56
            } else {
57
                $this->id = $this->value;
58
            }
59
        }
60
61
        print Form::select($this->name, array('0' => Request::ajax() ? '(current)' : '(none)') + $select, $this->id,
0 ignored issues
show
Bug introduced by
The method select() does not seem to exist on object<form>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
62
            $this->attributes);
63
    }
64
65
    protected function buttons()
66
    {
67
        // Todo: $this->showFullField ?
68
//        if (!Request::ajax() || $this->showFullField) {
69
        if ( ! Request::ajax()) {
70
            $new_url = \URL::route('admin.model.create', array('slug' => $this->relatedModel['route']));
71
            $edit_url = \URL::route('admin.model.edit', array('slug' => $this->relatedModel['route'], 'id' => ':id:'));
72
            ?>
73
            <br>
74
            <div class="text-right">
75
                <div class="btn-group">
76
                    <button class="btn btn-default" data-toggle="modal"
77
                            data-url="<?= $edit_url ?>"
78
                            data-target="#form-modal-<?= $this->relatedModel['route'] ?>">
79
                        <i class="fa fa-edit"></i>
80
                    </button>
81
82
                    <button class="btn btn-default" data-toggle="modal"
83
                            data-url="<?= $new_url ?>"
84
                            data-target="#form-modal-<?= $this->relatedModel['route'] ?>">
85
                        <i class="fa fa-plus"></i>
86
                    </button>
87
                </div>
88
            </div>
89
            <?php
90
91
            $this->relationModal($this->relatedModel['route'], $this->id);
92
        }
93
    }
94
95
    protected function getData() {
96
        return \View::getShared()['data'];
97
    }
98
99
    public function input()
100
    {
101
        $data = $this->getRelatedInstance()->all(
102
            (is_string($this->getShow())) ? ['id', $this->getShow()] : ['*']
103
        )->toArray();
104
105
        $this->select($data, $this->getShow());
106
        $this->buttons();
107
    }
108
109
    public function show($value = null)
110
    {
111
        $value = ($value) ?: $this->value;
112
113
        if ( ! $value) return "<em>(none)</em>";
114
115
        $show = $this->getShow();
116
117
        if ( ! is_object($value)) {
118
            $data = $this->getRelatedInstance()->findOrFail($value);
119
120
            if ( ! $data) return '(none)';
121
122
            if (is_array($show)) {
123
                foreach ($show as $item) {
124
                    print $data->$item . "<br>";
125
                }
126
127
                return null;
128
            } elseif (is_callable($show)) {
129
                return $show($data);
130
            } else {
131
                return $data->$show;
132
            }
133
        }
134
135
        if ( ! $value) {
136
            throw new \Exception('The (hasOne) value is null');
137
        }
138
139
        print $value->{$this->getShow()};
140
141
        return null;
142
    }
143
144
}