Completed
Pull Request — master (#27)
by Christian
02:44
created

MatchingInteractionDefinition   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 111
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 20
lcom 1
cbo 2
dl 0
loc 111
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A withSource() 0 7 1
A withTarget() 0 7 1
A getSource() 0 4 1
A getTarget() 0 4 1
C equals() 0 52 15
1
<?php
2
3
/*
4
 * This file is part of the xAPI package.
5
 *
6
 * (c) Christian Flothmann <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Xabbuh\XApi\Model\Interaction;
13
14
use Xabbuh\XApi\Model\Definition;
15
use Xabbuh\XApi\Model\LanguageMap;
16
17
final class MatchingInteractionDefinition extends InteractionDefinition
18
{
19
    private $source;
20
    private $target;
21
22
    /**
23
     * @param LanguageMap|null            $name
24
     * @param LanguageMap|null            $description
25
     * @param string|null                 $type
26
     * @param string|null                 $moreInfo
27
     * @param string[]|null               $correctResponsesPattern
28
     * @param InteractionComponent[]|null $source
29
     * @param InteractionComponent[]|null $target
30
     */
31
    public function __construct(LanguageMap $name = null, LanguageMap $description = null, $type = null, $moreInfo = null, array $correctResponsesPattern = null, array $source = null, array $target = null)
32
    {
33
        parent::__construct($name, $description, $type, $moreInfo, $correctResponsesPattern);
34
35
        $this->source = $source;
36
        $this->target = $target;
37
    }
38
39
    /**
40
     * @param InteractionComponent[]|null $source
41
     *
42
     * @return static
43
     */
44
    public function withSource(array $source = null)
45
    {
46
        $interaction = clone $this;
47
        $interaction->source = $source;
48
49
        return $interaction;
50
    }
51
52
    /**
53
     * @param InteractionComponent[]|null $target
54
     *
55
     * @return static
56
     */
57
    public function withTarget(array $target = null)
58
    {
59
        $interaction = clone $this;
60
        $interaction->target = $target;
61
62
        return $interaction;
63
    }
64
65
    public function getSource()
66
    {
67
        return $this->source;
68
    }
69
70
    public function getTarget()
71
    {
72
        return $this->target;
73
    }
74
75
    public function equals(Definition $definition)
76
    {
77
        if (!parent::equals($definition)) {
78
            return false;
79
        }
80
81
        if (!$definition instanceof MatchingInteractionDefinition) {
82
            return false;
83
        }
84
85
        if (null !== $this->source xor null !== $definition->source) {
86
            return false;
87
        }
88
89
        if (null !== $this->target xor null !== $definition->target) {
90
            return false;
91
        }
92
93
        if (null !== $this->source) {
94
            if (count($this->source) !== count($definition->source)) {
95
                return false;
96
            }
97
98
            foreach ($this->source as $key => $source) {
99
                if (!isset($definition->source[$key])) {
100
                    return false;
101
                }
102
103
                if (!$source->equals($definition->source[$key])) {
104
                    return false;
105
                }
106
            }
107
        }
108
109
        if (null !== $this->target) {
110
            if (count($this->target) !== count($definition->target)) {
111
                return false;
112
            }
113
114
            foreach ($this->target as $key => $target) {
115
                if (!isset($definition->target[$key])) {
116
                    return false;
117
                }
118
119
                if (!$target->equals($definition->target[$key])) {
120
                    return false;
121
                }
122
            }
123
        }
124
125
        return true;
126
    }
127
}
128