Passed
Push — feature/VSVGVQ-7-Entities ( 899613...a5b8a3 )
by Luc
02:47
created

AnswerEntity   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 95
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 7
dl 0
loc 95
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getText() 0 3 1
A toAnswer() 0 6 1
A fromAnswer() 0 6 1
A __construct() 0 9 1
A getQuestionEntity() 0 3 1
A setQuestionEntity() 0 3 1
A isCorrect() 0 3 1
1
<?php declare(strict_types=1);
2
3
namespace VSV\GVQ_API\Question\Repositories\Entities;
4
5
use Doctrine\ORM\Mapping as ORM;
6
use Ramsey\Uuid\Uuid;
7
use VSV\GVQ_API\Question\Models\Answer;
8
use VSV\GVQ_API\Question\ValueObjects\NotEmptyString;
9
10
/**
11
 * @ORM\Entity()
12
 * @ORM\Table(name="answer")
13
 */
14
class AnswerEntity extends Entity
15
{
16
    /**
17
     * @var string
18
     *
19
     * @ORM\Column(type="string", length=1024, nullable=false)
20
     */
21
    private $text;
22
23
    /**
24
     * @var bool
25
     *
26
     * @ORM\Column(type="boolean", nullable=false)
27
     */
28
    private $correct;
29
30
    /**
31
     * @var QuestionEntity
32
     *
33
     * @ORM\ManyToOne(targetEntity="QuestionEntity", inversedBy="answerEntities")
34
     * @ORM\JoinColumn(name="question_id", referencedColumnName="id")
35
     */
36
    private $questionEntity;
37
38
    /**
39
     * @param string $id
40
     * @param string $text
41
     * @param bool $correct
42
     */
43
    private function __construct(
44
        string $id,
45
        string $text,
46
        bool $correct
47
    ) {
48
        parent::__construct($id);
49
50
        $this->text = $text;
51
        $this->correct = $correct;
52
    }
53
54
    /**
55
     * @param Answer $answer
56
     * @return AnswerEntity
57
     */
58
    public static function fromAnswer(Answer $answer): AnswerEntity
59
    {
60
        return new AnswerEntity(
61
            $answer->getId()->toString(),
62
            $answer->getText()->toNative(),
63
            $answer->isCorrect()
64
        );
65
    }
66
67
    /**
68
     * @return Answer
69
     */
70
    public function toAnswer(): Answer
71
    {
72
        return new Answer(
73
            Uuid::fromString($this->getId()),
74
            new NotEmptyString($this->getText()),
75
            $this->isCorrect()
76
        );
77
    }
78
79
    /**
80
     * @return string
81
     */
82
    public function getText(): string
83
    {
84
        return $this->text;
85
    }
86
87
    /**
88
     * @return bool
89
     */
90
    public function isCorrect(): bool
91
    {
92
        return $this->correct;
93
    }
94
95
    /**
96
     * @return QuestionEntity
97
     */
98
    public function getQuestionEntity(): QuestionEntity
99
    {
100
        return $this->questionEntity;
101
    }
102
103
    /**
104
     * @param QuestionEntity $questionEntity
105
     */
106
    public function setQuestionEntity(QuestionEntity $questionEntity): void
107
    {
108
        $this->questionEntity = $questionEntity;
109
    }
110
}
111