Completed
Pull Request — master (#4)
by Guillaume
02:36
created

AutopromoBannerScriptGenerator::generate()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 24
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 24
ccs 0
cts 19
cp 0
rs 8.9713
c 0
b 0
f 0
cc 3
eloc 12
nc 4
nop 1
crap 12
1
<?php
2
3
namespace Dekalee\AdbackAnalytics\Generator;
4
5
/**
6
 * Class AutopromoBannerScriptGenerator
7
 */
8
class AutopromoBannerScriptGenerator extends AbstractScriptGenerator implements ScriptGeneratorInterface
9
{
10
    /**
11
     * @param int $id
12
     *
13
     * @return string
14
     */
15
    public function generate($id = null)
16
    {
17
        if(is_null($id)) {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
18
            // TODO throw exception
19
        }
20
21
        if (!$this->cache->isAutopromoBannerConfigured()) {
22
            return '';
23
        }
24
25
        $url = $this->cache->getAutopromoBannerUrl();
26
        $script = $this->cache->getAutopromoBannerScript();
27
28
        $script = <<<EOS
29
(function (a,d){var s,t,cs,ds,dd;s=d.createElement('script');cs=d.currentScript;
30
    ds=d.createElement('span');ds.id=Math.random().toString(36).substring(7);
31
    dd=cs.parentNode.insertBefore(ds,cs);
32
    s.src=a;s.async=1;s.setAttribute('data-dd',ds.id);s.setAttribute('data-id',$id);
33
    t=d.getElementsByTagName('script')[0];t.parentNode.insertBefore(s,t);})
34
("https://$url/$script.js", document);
35
EOS;
36
37
        return $script;
38
    }
39
}
40