HasLinks   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 9
dl 0
loc 38
rs 10
c 2
b 0
f 0
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A links() 0 9 3
A withCollectionLinks() 0 5 1
A collectionLinks() 0 3 1
1
<?php
2
3
namespace Spatie\ResourceLinks;
4
5
/** @mixin \Illuminate\Http\Resources\Json\JsonResource */
6
trait HasLinks
7
{
8
    /** @var bool */
9
    private $withCollectionLinks = false;
10
11
    /**
12
     * @param string|\Closure|null|array $controller
13
     * @param null $parameters
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $parameters is correct as it would always require null to be passed?
Loading history...
14
     *
15
     * @return \Spatie\ResourceLinks\LinkResource
16
     */
17
    public function links($controller = null, $parameters = null): LinkResource
18
    {
19
        $resource = LinkResource::create($this->resource, LinkResourceType::ITEM)->link($controller, $parameters);
20
21
        if (property_exists($this, 'withCollectionLinks') && $this->withCollectionLinks) {
22
            $resource->withCollectionLinks();
23
        }
24
25
        return $resource;
26
    }
27
28
    /**
29
     * @param string|\Closure|null|array $controller
30
     * @param null $parameters
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $parameters is correct as it would always require null to be passed?
Loading history...
31
     *
32
     * @return \Spatie\ResourceLinks\LinkResource
33
     */
34
    public static function collectionLinks($controller = null, $parameters = null): LinkResource
35
    {
36
        return LinkResource::create(null, LinkResourceType::COLLECTION)->link($controller, $parameters);
37
    }
38
39
    public function withCollectionLinks()
40
    {
41
        $this->withCollectionLinks = true;
42
43
        return $this;
44
    }
45
}
46