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

InteractionDefinition   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 1
dl 0
loc 65
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A withCorrectResponsesPattern() 0 7 1
A getCorrectResponsesPattern() 0 4 1
C equals() 0 28 8
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
/**
18
 * Base class for interaction definitions of an {@link Activity}.
19
 *
20
 * @author Christian Flothmann <[email protected]>
21
 */
22
abstract class InteractionDefinition extends Definition
23
{
24
    private $correctResponsesPattern;
25
26
    /**
27
     * @param LanguageMap|null $name
28
     * @param LanguageMap|null $description
29
     * @param string|null      $type
30
     * @param string|null      $moreInfo
31
     * @param string[]|null    $correctResponsesPattern
32
     */
33
    public function __construct(LanguageMap $name = null, LanguageMap $description = null, $type = null, $moreInfo = null, array $correctResponsesPattern = null)
34
    {
35
        parent::__construct($name, $description, $type, $moreInfo);
36
37
        $this->correctResponsesPattern = $correctResponsesPattern;
38
    }
39
40
    /**
41
     * @param string[]|null $correctResponsesPattern
42
     *
43
     * @return static
44
     */
45
    public function withCorrectResponsesPattern(array $correctResponsesPattern = null)
46
    {
47
        $interaction = clone $this;
48
        $interaction->correctResponsesPattern = $correctResponsesPattern;
49
50
        return $interaction;
51
    }
52
53
    public function getCorrectResponsesPattern()
54
    {
55
        return $this->correctResponsesPattern;
56
    }
57
58
    public function equals(Definition $definition)
59
    {
60
        if (!parent::equals($definition)) {
61
            return false;
62
        }
63
64
        if (!$definition instanceof InteractionDefinition) {
65
            return false;
66
        }
67
68
        if (null !== $this->correctResponsesPattern xor null !== $definition->correctResponsesPattern) {
69
            return false;
70
        }
71
72
        if (null !== $this->correctResponsesPattern) {
73
            if (count($this->correctResponsesPattern) !== count($definition->correctResponsesPattern)) {
74
                return false;
75
            }
76
77
            foreach ($this->correctResponsesPattern as $value) {
78
                if (!in_array($value, $definition->correctResponsesPattern, true)) {
79
                    return false;
80
                }
81
            }
82
        }
83
84
        return true;
85
    }
86
}
87