FieldRule::slugBehaviorConfig()   A
last analyzed

Complexity

Conditions 3
Paths 1

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3.1406

Importance

Changes 0
Metric Value
cc 3
eloc 9
nc 1
nop 0
dl 0
loc 12
ccs 6
cts 8
cp 0.75
crap 3.1406
rs 9.9666
c 0
b 0
f 0
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, ContractTrait};
7
use yii\{helpers\Url, web\Link, web\NotFoundHttpException};
8
9
/**
10
 * ROA contract handling FieldRule records.
11
 */
12
class FieldRule extends base\FieldRule implements Contract
13
{
14
    use ContractTrait {
15
        getLinks as getContractLinks;
16
    }
17
18
    /**
19
     * @inheritdoc
20
     */
21
    protected $fieldClass = Field::class;
22
23
    /**
24
     * @inheritdoc
25
     */
26
27
    protected $propertyClass = FieldRuleProperty::class;
28
29
    /**
30
     * @inheritdoc
31
     */
32 9
    protected function slugBehaviorConfig(): array
33
    {
34
        return [
35 9
            'resourceName' => 'rule',
36 9
            'parentSlugRelation' => 'field',
37 9
            'checkAccess' => function ($params) {
38
                if (
39 8
                    isset($params['rule_id'])
40 8
                    && $params['rule_id'] != $this->id
41
                ) {
42
                    throw new NotFoundHttpException(
43
                        'Field Rule doesnt contain the requested route.'
44
                    );
45
                }
46 9
            }
47
        ];
48
    }
49
50
    /**
51
     * @inheritdoc
52
     */
53 4
    public function getLinks()
54
    {
55 4
        $selfLink = $this->getSelfLink();
56
57 4
        return array_merge($this->getContractLinks(), [
58 4
            'properties' => $selfLink . '/property',
59
            'curies' => [
60 4
                new Link([
61 4
                    'name' => 'embeddable',
62 4
                    '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

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