Passed
Push — master ( 7c7fa2...d7cd6a )
by Michael
04:35
created

LinksContainer   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 2
dl 0
loc 54
ccs 15
cts 15
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A addLink() 0 10 2
A getLinks() 0 4 1
A mergeLinks() 0 12 3
1
<?php
2
declare(strict_types = 1);
3
4
namespace Mikemirten\Component\JsonApi\Mapper\Definition\Behaviour;
5
6
use Mikemirten\Component\JsonApi\Mapper\Definition\Link;
7
8
/**
9
 * An implementation of links-container behaviour
10
 *
11
 * @package Mikemirten\Component\JsonApi\Mapper\Definition\Behaviour
12
 */
13
trait LinksContainer
14
{
15
    /**
16
     * Collection of links
17
     *
18
     * @var Link[]
19
     */
20
    protected $links = [];
21
22
    /**
23
     * Set link
24
     *
25
     * @param Link $link
26
     */
27 12
    public function addLink(Link $link)
28
    {
29 12
        $name = $link->getName();
30
31 12
        if (isset($this->links[$name])) {
32 1
            throw new \LogicException(sprintf('A link name by "%s" is already exists.', $name));
33
        }
34
35 12
        $this->links[$name] = $link;
36 12
    }
37
38
    /**
39
     * Get links
40
     * [name => Link]
41
     *
42
     * @return Link[]
43
     */
44 8
    public function getLinks(): array
45
    {
46 8
        return $this->links;
47
    }
48
49
    /**
50
     * Merge links
51
     *
52
     * @param LinksAwareInterface $container
53
     */
54 6
    protected function mergeLinks(LinksAwareInterface $container)
55
    {
56 6
        foreach ($container->getLinks() as $name => $link)
57
        {
58 2
            if (isset($this->links[$name])) {
59 2
                $this->links[$name]->merge($link);
60 2
                continue;
61
            }
62
63 2
            $this->links[$name] = $link;
64
        }
65
    }
66
}