Links::push()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 7
dl 0
loc 15
rs 10
c 1
b 0
f 0
cc 3
nc 4
nop 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