Skip to content

Commit d1a65ff

Browse files
committed
add content field in Template to get the current body according to the variation parameter
1 parent 26fef73 commit d1a65ff

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

Entity/Template.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,11 @@ class Template
6868
*/
6969
private $createdAt;
7070

71+
/**
72+
* @var boolean $isVariation
73+
*/
74+
private $variation;
75+
7176
/**
7277
* Get id
7378
*
@@ -164,6 +169,25 @@ public function setExperimentCode($experimentCode)
164169
$this->experimentCode = $experimentCode;
165170
}
166171

172+
/**
173+
* Get variation
174+
*
175+
* @return true|false
176+
*/
177+
public function isVariation()
178+
{
179+
return $this->variation;
180+
}
181+
182+
/**
183+
* Set variation (boolean)
184+
* @param variation the value to set true|false
185+
*/
186+
public function setVariation($variation)
187+
{
188+
$this->variation = $variation;
189+
}
190+
167191
/**
168192
* Set createdAt
169193
*
@@ -232,8 +256,21 @@ public function beforeUpdate()
232256
$this->setUpdatedAt(new \DateTime());
233257
}
234258

259+
/**
260+
* Return the body|variationBody according to isVariation
261+
*/
262+
public function getContent()
263+
{
264+
return $this->isVariation() && $this->isValidBody($this->getVariationBody()) ?
265+
$this->getVariationBody() : $this->getBody();
266+
}
267+
235268
public function getAnalyticsScript()
236269
{
270+
if (null === $this->getExperimentCode() || strlen($this->getExperimenteCode) < 5) {
271+
return;
272+
}
273+
237274
$template = <<<EOF
238275
<!-- Google Analytics Content Experiment code -->
239276
<script>function utmx_section(){}function utmx(){}(function(){var
@@ -252,4 +289,9 @@ function f(n){if(c){var i=c.indexOf(n+'=');if(i>-1){var j=c.
252289
EOF;
253290
return str_replace('%EXPERIMENT_CODE%', $this->getExperimentCode(), $template);
254291
}
292+
293+
protected function isValidBody($body)
294+
{
295+
return (null !== $body && strlen(trim($body)) > 0);
296+
}
255297
}

0 commit comments

Comments
 (0)