Completed
Push — master ( 6db6da...5370d9 )
by Angel
03:42
created

ContractTrait   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 3
dl 0
loc 52
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A behaviors() 0 9 1
slugBehaviorConfig() 0 1 ?
A curiesBehaviorConfig() 0 4 1
A getSlugBehaviorId() 0 4 1
A getCuriesBehaviorId() 0 4 1
A getSlugBehavior() 0 4 1
A getCuriesBehavior() 0 4 1
A getSelfLink() 0 4 1
A getLinks() 0 5 1
1
<?php
2
3
namespace roaresearch\yii2\roa\hal;
4
5
use roaresearch\yii2\roa\behaviors\{Curies, Slug};
6
7
/**
8
 * Trait which gives the basic support for HAL contracts.
9
 */
10
trait ContractTrait
11
{
12
    use EmbeddableTrait;
13
14
    public function behaviors()
15
    {
16
        return array_merge(parent::behaviors(), [
17
            $this->getSlugBehaviorId() => ['class' => Slug::class]
18
                + $this->slugBehaviorConfig(),
19
            $this->getCuriesBehaviorId() => ['class' => Curies::class]
20
                + $this->curiesBehaviorConfig(),
21
        ]);
22
    }
23
24
    abstract protected function slugBehaviorConfig(): array;
25
26
    protected function curiesBehaviorConfig(): array
27
    {
28
        return [];
29
    }
30
31
    protected function getSlugBehaviorId(): string
32
    {
33
        return 'slug';
34
    }
35
36
    protected function getCuriesBehaviorId(): string
37
    {
38
        return 'curies';
39
    }
40
41
    public function getSlugBehavior(): Slug
42
    {
43
        return $this->getBehavior($this->getSlugBehaviorId());
0 ignored issues
show
Bug introduced by
It seems like getBehavior() 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...
44
    }
45
46
    public function getCuriesBehavior(): Curies
47
    {
48
        return $this->getBehavior($this->getCuriesBehaviorId());
0 ignored issues
show
Bug introduced by
It seems like getBehavior() 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...
49
    }
50
51
    public function getSelfLink(): string
52
    {
53
        return $this->getSlugBehavior()->getSelfLink();
54
    }
55
56
    public function getLinks()
57
    {
58
        return $this->getSlugBehavior()->getSlugLinks()
59
            + $this->getCuriesBehavior()->getCuriesLinks();
60
    }
61
}
62