Completed
Pull Request — master (#1)
by Angel
07:01
created

Field   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 28
dl 0
loc 68
ccs 24
cts 24
cp 1
rs 10
c 0
b 0
f 0
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getLinks() 0 20 1
A extraFields() 0 3 1
A slugBehaviorConfig() 0 11 3
1
<?php
2
3
namespace roaresearch\yii2\formgenerator\roa\models;
4
5
use roaresearch\yii2\formgenerator\models as base;
6
use roaresearch\yii2\roa\hal\Contract;
7
use roaresearch\yii2\roa\hal\ContractTrait;
8
use yii\helpers\Url;
9
use yii\web\Link;
10
use yii\web\NotFoundHttpException;
11
12
/**
13
 * ROA contract handling Field records.
14
 */
15
class Field extends base\Field implements Contract
16
{
17
    use ContractTrait {
18
        getLinks as getContractLinks;
19
    }
20
21
    /**
22
     * @inheritdoc
23
     */
24
    protected $dataTypeClass = DataType::class;
25
26
    /**
27
     * @inheritdoc
28
     */
29
    protected $ruleClass = FieldRule::class;
30
31
    /**
32
     * @inheritdoc
33
     */
34 23
    protected function slugBehaviorConfig(): array
35
    {
36
        return [
37 23
            'resourceName' => 'field',
38 23
            'checkAccess' => function ($params) {
39 12
                if (
40 12
                    isset($params['field_id'])
41
                    && $params['field_id'] != $this->id
42 1
                ) {
43 1
                    throw new NotFoundHttpException(
44
                        'Field doesnt contain the requested route.'
45
                    );
46 23
                }
47
            },
48
        ];
49
    }
50
51
    /**
52
     * @inheritdoc
53 5
     */
54
    public function getLinks()
55 5
    {
56
        $selfLink = $this->getSelfLink();
57 5
58 5
        return array_merge($this->getContractLinks(), [
59
            'rules' => $selfLink . '/rule',
60 5
            'curies' => [
61 5
                new Link([
62 5
                    'name' => 'embeddable',
63 5
                    'href' => Url::to($selfLink, ['expand' => '{rel}']),
0 ignored issues
show
Bug introduced by
array('expand' => '{rel}') of type array<string,string> is incompatible with the type boolean|string expected by parameter $scheme of yii\helpers\BaseUrl::to(). ( Ignorable by Annotation )

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

63
                    'href' => Url::to($selfLink, /** @scrutinizer ignore-type */ ['expand' => '{rel}']),
Loading history...
64
                    'title' => 'Embeddable and not Nestable related resources.',
65 5
                ]),
66 5
                new Link([
67 5
                    'name' => 'nestable',
68 5
                    'href' => Url::to($selfLink, ['expand' => '{rel}']),
69
                    'title' => 'Embeddable and Nestable related resources.',
70
                ]),
71 5
            ],
72 5
            'embeddable:rules' => 'rules',
73
            'nestable:dataType' => 'dataType',
74
        ]);
75
    }
76
77
    /**
78
     * @inheritdoc
79 5
     */
80
    public function extraFields()
81 5
    {
82
        return ['dataType', 'rules'];
83
    }
84
}
85