Completed
Pull Request — master (#212)
by jelmer
61:21 queued 59:18
created

OtherChoiceOption::getOtherOption()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 0
cts 4
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 1
crap 2
1
<?php
2
3
namespace SumoCoders\FrameworkCoreBundle\Entity;
4
5
use Doctrine\ORM\Mapping as ORM;
6
use InvalidArgumentException;
7
use RuntimeException;
8
9
/**
10
 * @ORM\Entity
11
 * @ORM\Table(
12
 *     name="OtherChoiceOption",
13
 *     indexes={@ORM\Index(name="category_index", columns={"category"})},
14
 *     uniqueConstraints={@ORM\UniqueConstraint(name="unique_name_index", columns={"category", "label"})}
15
 * )
16
 */
17
class OtherChoiceOption
18
{
19
    /**
20
     * @var string
21
     *
22
     * @ORM\Column(type="guid")
23
     * @ORM\Id
24
     * @ORM\GeneratedValue(strategy="UUID")
25
     */
26
    private $id;
27
28
    /**
29
     * @var string
30
     *
31
     * @ORM\Column(type="string")
32
     */
33
    private $category;
34
35
    /**
36
     * @var string
37
     *
38
     * @ORM\Column(type="string")
39
     */
40
    private $label;
41
42
    /**
43
     * Used to create new choice options in the form type
44
     *
45
     * @var string
46
     */
47
    private $newChoiceOptionCategory;
48
49
    public function __construct(string $category, string $label)
50
    {
51
        if ($category === 'other') {
52
            throw new InvalidArgumentException('The category "other" is reserved');
53
        }
54
55
        $this->category = $category;
56
        $this->label = $label;
57
    }
58
59
    public function getId(): string
60
    {
61
        return $this->id;
62
    }
63
64
    public function getCategory(): string
65
    {
66
        return $this->category;
67
    }
68
69
    public function getValue(): string
70
    {
71
        if ($this->category === 'other') {
72
            return $this->category;
73
        }
74
75
        return $this->label;
76
    }
77
78
    public function getLabel(): string
79
    {
80
        return $this->label;
81
    }
82
83
    public function __toString(): string
84
    {
85
        return $this->label;
86
    }
87
88
    public static function getOtherOption(string $label): self
89
    {
90
        $otherOption = new self('tmp', $label);
91
        // hack to be able to set other while it is restricted;
92
        $otherOption->category = 'other';
93
94
        return $otherOption;
95
    }
96
97
    public function getWithNewChoiceOptionCategory(string $newChoiceOptionCategory): self
98
    {
99
        if ($this->category !== 'other') {
100
            throw new RuntimeException('This method can only be called when the category is other');
101
        }
102
103
        $choiceOption = clone $this;
104
        $choiceOption->newChoiceOptionCategory = $newChoiceOptionCategory;
105
106
        return $choiceOption;
107
    }
108
109
    /**
110
     * This method is used to transform an other choice option into the actual choice option
111
     *
112
     * @param string $label
113
     */
114
    public function transformToActualChoiceOption(string $label): void
115
    {
116
        if ($this->category !== 'other') {
117
            throw new RuntimeException('This method can only be called when the category is other');
118
        }
119
120
        $this->category = $this->newChoiceOptionCategory;
121
        $this->label = $label;
122
        $this->newChoiceOptionCategory = null;
123
    }
124
}
125