Lifetime::getId()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Alpha\VisitorTrackingBundle\Entity;
6
7
use Doctrine\Common\Collections\ArrayCollection;
8
use Doctrine\Common\Collections\Collection;
9
use Doctrine\ORM\Mapping as ORM;
10
use Gedmo\Mapping\Annotation as Gedmo;
11
12
/**
13
 * @ORM\Entity
14
 */
15
class Lifetime
16
{
17
    /**
18
     * @var string
19
     *
20
     * @ORM\Column(type="guid")
21
     * @ORM\Id
22
     * @ORM\GeneratedValue(strategy="UUID")
23
     */
24
    protected $id;
25
26
    /**
27
     * @var \DateTime
28
     *
29
     * @ORM\Column(type="datetime")
30
     * @Gedmo\Timestampable(on="create")
31
     */
32
    protected $created;
33
34
    /**
35
     * @var Collection|Session[]
36
     *
37
     * @ORM\OneToMany(targetEntity="Session", mappedBy="lifetime", cascade={"persist"})
38
     */
39
    protected $sessions;
40
41
    /**
42
     * @var Collection|Seed[]
43
     *
44
     * @ORM\OneToMany(targetEntity="Seed", mappedBy="lifetime", cascade={"persist"})
45
     */
46
    protected $seeds;
47
48
    public function __construct()
49
    {
50
        $this->sessions = new ArrayCollection();
51
        $this->seeds = new ArrayCollection();
52
    }
53
54
    /**
55
     * @return string
56
     */
57
    public function getId()
58
    {
59
        return $this->id;
60
    }
61
62
    /**
63
     * @return \DateTime
64
     */
65
    public function getCreated()
66
    {
67
        return $this->created;
68
    }
69
70
    /**
71
     * @param  \DateTime $created
72
     *
73
     * @return $this
74
     */
75
    public function setCreated(\DateTime $created)
76
    {
77
        $this->created = $created;
78
79
        return $this;
80
    }
81
82
    /**
83
     * @param Session $session
84
     *
85
     * @return $this
86
     */
87
    public function addSession(Session $session)
88
    {
89
        if (!$this->sessions->contains($session)) {
90
            $this->sessions->add($session);
91
        }
92
93
        if ($session->getLifetime() !== $this) {
94
            $session->setLifetime($this);
95
        }
96
97
        return $this;
98
    }
99
100
    /**
101
     * @param Session $session
102
     *
103
     * @return $this
104
     */
105
    public function removeSession(Session $session)
106
    {
107
        $this->sessions->removeElement($session);
108
109
        return $this;
110
    }
111
112
    /**
113
     * @return Collection|Session[]
114
     */
115
    public function getSessions(): Collection
116
    {
117
        return $this->sessions;
118
    }
119
120
    /**
121
     * @param Seed $seed
122
     *
123
     * @return $this
124
     */
125
    public function addSeed(Seed $seed)
126
    {
127
        if (!$this->seeds->contains($seed)) {
128
            $this->seeds->add($seed);
129
        }
130
131
        $seed->setLifetime($this);
132
133
        return $this;
134
    }
135
136
    /**
137
     * @param Seed $seed
138
     *
139
     * @return $this
140
     */
141
    public function removeSeed(Seed $seed)
142
    {
143
        $this->seeds->removeElement($seed);
144
145
        return $this;
146
    }
147
148
    /**
149
     * @return Collection|Seed[]
150
     */
151
    public function getSeeds(): Collection
152
    {
153
        return $this->seeds;
154
    }
155
156
    /**
157
     * Gets a pre-existing seed, or creates a fresh seed if one does not exist for the given name.
158
     *
159
     * $weights is an optional array, ideally associative to name your variations and set their likelihood
160
     * eg you want 75% of people to see a green button and 25% to see red use:
161
     * $name = "button-colour-test";
162
     * $numberOfValues = 2;
163
     * $weights = ["green" => 3, "red" => 1]; (or ["green" => 75, "red" => 25])
164
     * This method will then return the string "green" 75% of the time and "red" 25% of the time. Since this cookie lasts 2 years, its very sticky
165
     * Querying your test results then becomes very easy and descriptive
166
     *
167
     * @param string $name
168
     * @param int $numberOfValues
169
     * @param array|null $weights
170
     *
171
     * @return string
172
     */
173
    public function getSeed(string $name, int $numberOfValues, array $weights = null): string
174
    {
175
        foreach ($this->seeds as $seed) {
176
            if ($seed->getName() === $name) {
177
                return $seed->getValue();
178
            }
179
        }
180
181
        $seed = new Seed($name, $numberOfValues, $weights);
182
        $this->addSeed($seed);
183
184
        return $seed->getValue();
185
    }
186
187
    public function hasSeed(string $name): bool
188
    {
189
        foreach ($this->seeds as $seed) {
190
            if ($seed->getName() === $name) {
191
                return true;
192
            }
193
        }
194
195
        return false;
196
    }
197
}
198