Completed
Push — master ( 4ca583...a67af2 )
by Song
02:43
created

BelongsToRelation::getLoadUrl()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Encore\Admin\Grid\Displayers;
4
5
use Encore\Admin\Admin;
6
use Encore\Admin\Grid\Selectable;
7
8
trait BelongsToRelation
9
{
10
    /**
11
     * @var string
12
     */
13
    protected $modalID;
14
15
    /**
16
     * @var string
17
     */
18
    protected $selectable;
19
20
    /**
21
     * @var string
22
     */
23
    protected $columnName = '';
24
25
    /**
26
     * @param int $multiple
27
     * @return string
28
     */
29
    protected function getLoadUrl($multiple = 0)
30
    {
31
        $selectable = str_replace('\\', '_', $this->selectable);
32
33
        return route('admin.handle-selectable', compact('selectable', 'multiple'));
34
    }
35
36
    /**
37
     * @return $this
38
     */
39 View Code Duplication
    public function addHtml()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
40
    {
41
        $trans = [
42
            'choose' => admin_trans('admin.choose'),
43
            'cancal' => admin_trans('admin.cancel'),
44
            'submit' => admin_trans('admin.submit'),
45
        ];
46
47
        $html = <<<HTML
48
<div class="modal fade" id="{$this->modalID}" tabindex="-1" role="dialog">
49
  <div class="modal-dialog modal-lg" role="document">
50
    <div class="modal-content" style="border-radius: 5px;">
51
      <div class="modal-header">
52
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
53
            <span aria-hidden="true">&times;</span>
54
        </button>
55
        <h4 class="modal-title">{$trans['choose']}</h4>
56
      </div>
57
      <div class="modal-body">
58
        <i class="fa fa-spinner fa-pulse fa-3x fa-fw"></i>
59
      </div>
60
      <div class="modal-footer">
61
        <button type="button" class="btn btn-default" data-dismiss="modal">{$trans['cancal']}</button>
62
        <button type="button" class="btn btn-primary submit">{$trans['submit']}</button>
63
      </div>
64
    </div>
65
  </div>
66
</div>
67
HTML;
68
        Admin::html($html);
69
70
        return $this;
71
    }
72
73
    /**
74
     * @return $this
75
     */
76
    public function addStyle()
77
    {
78
        $style = <<<STYLE
79
#{$this->modalID} tr {
80
    cursor: pointer;
81
}
82
#{$this->modalID} .box {
83
    border-top: none;
84
    margin-bottom: 0;
85
    box-shadow: none;
86
}
87
STYLE;
88
89
        Admin::style($style);
90
91
        return $this;
92
    }
93
94
    /**
95
     * @param string $selectable
96
     * @param string $column
97
     * @return string
98
     */
99
    public function display($selectable = null, $column = '')
100
    {
101 View Code Duplication
        if (!class_exists($selectable) || !is_subclass_of($selectable, Selectable::class)) {
0 ignored issues
show
Bug introduced by
Due to PHP Bug #53727, is_subclass_of might return inconsistent results on some PHP versions if \Encore\Admin\Grid\Selectable::class can be an interface. If so, you could instead use ReflectionClass::implementsInterface.
Loading history...
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
102
            throw new \InvalidArgumentException(
103
                "[Class [{$selectable}] must be a sub class of Encore\Admin\Grid\Selectable"
104
            );
105
        }
106
107
        $this->columnName = $column ?: $this->getName();
0 ignored issues
show
Bug introduced by
It seems like getName() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
108
        $this->selectable = $selectable;
109
        $this->modalID    = sprintf('modal-grid-selector-%s', $this->getClassName());
0 ignored issues
show
Bug introduced by
It seems like getClassName() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
110
111
        $this->addHtml()->addScript()->addStyle();
0 ignored issues
show
Bug introduced by
It seems like addScript() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
112
113
        return <<<HTML
114
<span class="grid-selector" data-toggle="modal" data-target="#{$this->modalID}" key="{$this->getKey()}" data-val="{$this->getOriginalData()}">
0 ignored issues
show
Bug introduced by
It seems like getKey() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
Bug introduced by
It seems like getOriginalData() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
115
   <a href="javascript:void(0)" class="text-muted">
116
       <i class="fa fa-check-square-o"></i>&nbsp;&nbsp;
117
       <span class="text">{$this->value}</span>
0 ignored issues
show
Bug introduced by
The property value does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
118
   </a>
119
</span>
120
HTML;
121
    }
122
}
123