Test Failed
Push — master ( f2a606...7148c0 )
by Terzi
03:41
created

HasValuePresenter   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 33
rs 10
c 0
b 0
f 0
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A hasPresenter() 0 6 3
A callPresenter() 0 3 1
A presenterMethod() 0 3 1
1
<?php
2
3
namespace Terranet\Administrator\Field\Traits;
4
5
use Illuminate\Database\Eloquent\Model;
6
use Terranet\Presentable\PresentableInterface;
7
8
trait HasValuePresenter
9
{
10
    /**
11
     * Check if a model has field's presenter.
12
     *
13
     * @param Model $model
14
     * @param string $field
15
     * @return null|callable
16
     */
17
    public function hasPresenter(Model $model, string $field): ?callable
18
    {
19
        return $model instanceof PresentableInterface
20
        && method_exists($model->present(), $method = $this->presenterMethod($field))
21
            ? [$model->present(), $method]
22
            : null;
23
    }
24
25
    /**
26
     * @param callable $presenter
27
     * @return mixed
28
     */
29
    public function callPresenter(callable $presenter)
30
    {
31
        return call_user_func_array($presenter, [$this->value()]);
0 ignored issues
show
Bug introduced by
It seems like value() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

31
        return call_user_func_array($presenter, [$this->/** @scrutinizer ignore-call */ value()]);
Loading history...
32
    }
33
34
    /**
35
     * @param string $fieldName
36
     * @return string
37
     */
38
    public function presenterMethod(string $fieldName): string
39
    {
40
        return 'admin'.title_case(camel_case($fieldName));
41
    }
42
}
43