Completed
Pull Request — master (#164)
by Manuel
01:37
created

ResolvesFields::isVisible()   B

Complexity

Conditions 9
Paths 5

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
c 0
b 0
f 0
rs 8.0555
cc 9
nc 5
nop 2
1
<?php
2
3
namespace Oscer\Cms\Backend\Resources;
4
5
use Illuminate\Database\Eloquent\Model;
6
use Oscer\Cms\Backend\Contracts\ElementContainer;
7
use Oscer\Cms\Backend\Resources\Fields\Field;
8
9
trait ResolvesFields
10
{
11
    protected function resolveFields(array $fields, Model $model, string $view = 'create')
12
    {
13
        return collect($fields)->map(function ($element) use ($model, $view) {
14
            if ($element instanceof ElementContainer) {
15
                return $element->resolveElements($model);
16
            }
17
            if ($element instanceof Field && $this->isVisible($element, $view)) {
0 ignored issues
show
Bug introduced by
The class Oscer\Cms\Backend\Resources\Fields\Field does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
18
                return $element->resolve($model);
19
            }
20
        })->all();
21
    }
22
23
    protected function isVisible(Field $field, string $view): bool
24
    {
25
        if ($view === 'create' && $field->showOnCreate === false) {
26
            return false;
27
        }
28
        if ($view === 'update' && $field->showOnUpdate === false) {
29
            return false;
30
        }
31
        if ($view === 'detail' && $field->showOnDetail === false) {
32
            return false;
33
        }
34
        if ($view === 'index' && $field->showOnIndex === false) {
35
            return false;
36
        }
37
38
        return true;
39
    }
40
}
41