Links   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
eloc 16
dl 0
loc 40
rs 10
c 1
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A push() 0 15 3
A when() 0 9 3
A all() 0 3 1
A make() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Sarala;
6
7
class Links
8
{
9
    private array $links = [];
10
11
    public static function make(): self
12
    {
13
        return new self();
14
    }
15
16
    public function push($link): self
17
    {
18
        if ($link instanceof \Closure) {
19
            $link = $link();
20
        }
21
22
        if (! $link instanceof Link) {
23
            throw new \InvalidArgumentException(
24
                'push() method expects an instance of '.Link::class.' or a closure returns a '.Link::class.'. '.gettype($link).' given'
25
            );
26
        }
27
28
        $this->links[$link->name()] = $link->data();
29
30
        return $this;
31
    }
32
33
    public function when($value, $link, $default = null): self
34
    {
35
        if ($value) {
36
            $this->push($link);
37
        } elseif ($default) {
38
            $this->push($default);
39
        }
40
41
        return $this;
42
    }
43
44
    public function all(): array
45
    {
46
        return $this->links;
47
    }
48
}
49