Completed
Push — 1.4 ( 85dbd7...326d11 )
by Paweł
25:51 queued 16:44
created

ArticleEventsExtension   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 81
Duplicated Lines 7.41 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 4
dl 6
loc 81
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getFunctions() 0 7 1
A renderLinkImpressionCount() 3 23 2
A renderPageViewCount() 3 26 4

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Superdesk Web Publisher Core Bundle.
7
 *
8
 * Copyright 2017 Sourcefabric z.u. and contributors.
9
 *
10
 * For the full copyright and license information, please see the
11
 * AUTHORS and LICENSE files distributed with this source code.
12
 *
13
 * @copyright 2017 Sourcefabric z.ú
14
 * @license http://www.superdesk.org/license
15
 */
16
17
namespace SWP\Bundle\CoreBundle\Twig;
18
19
use SWP\Bundle\CoreBundle\Model\ArticleInterface;
20
use SWP\Component\TemplatesSystem\Gimme\Meta\Meta;
21
use Twig\Extension\AbstractExtension;
22
use Twig\TwigFunction;
23
24
class ArticleEventsExtension extends AbstractExtension
25
{
26
    protected $analyticsHost;
27
28
    public function __construct(string $analyticsHost = null)
29
    {
30
        $this->analyticsHost = $analyticsHost;
31
    }
32
33
    /**
34
     * @return TwigFunction[]
35
     */
36
    public function getFunctions()
37
    {
38
        return [
39
            new TwigFunction('countPageView', [$this, 'renderPageViewCount'], ['is_safe' => ['html']]),
40
            new TwigFunction('countArticlesImpressions', [$this, 'renderLinkImpressionCount'], ['is_safe' => ['html']]),
41
        ];
42
    }
43
44
    /**
45
     * @param Meta $meta
0 ignored issues
show
Bug introduced by
There is no parameter named $meta. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
46
     *
47
     * @return string|void
48
     */
49
    public function renderLinkImpressionCount()
50
    {
51
        $jsTemplate = <<<'EOT'
52
<script type="text/javascript">
53
var arr = [], l = document.links;
54
for(var i=0; i<l.length; i++) {
55
  if(arr.indexOf(l[i].href) === -1){arr.push(l[i].href);}
56
}
57
var xhr = new XMLHttpRequest();
58
var read_date = new Date();
59
var request_randomizer = "&" + read_date.getTime() + Math.random();
60
xhr.open('POST', '/_swp_analytics?type=impression'+request_randomizer);
61
xhr.setRequestHeader("Content-Type", "application/json");
62
xhr.send(JSON.stringify(arr));
63
</script>
64
EOT;
65
66 View Code Duplication
        if (null !== $this->analyticsHost) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
67
            $jsTemplate = str_replace('/_swp_analytics', $this->analyticsHost.'/_swp_analytics', $jsTemplate);
68
        }
69
70
        return $jsTemplate;
71
    }
72
73
    /**
74
     * @param Meta $meta
75
     *
76
     * @return string|void
77
     */
78
    public function renderPageViewCount(Meta $meta = null)
79
    {
80
        if (null === $meta) {
81
            return;
82
        }
83
84
        $jsTemplate = <<<'EOT'
85
<script type="text/javascript">
86
var xhr = new XMLHttpRequest();
87
var read_date = new Date();
88
var request_randomizer = "&" + read_date.getTime() + Math.random();
89
xhr.open('GET', '/_swp_analytics?articleId=%s'+request_randomizer+'&ref='+document.referrer);
90
xhr.send();
91
</script>
92
EOT;
93
        $article = $meta->getValues();
94
        if (!$article instanceof ArticleInterface) {
95
            return;
96
        }
97
98 View Code Duplication
        if (null !== $this->analyticsHost) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
99
            $jsTemplate = str_replace('/_swp_analytics', $this->analyticsHost.'/_swp_analytics', $jsTemplate);
100
        }
101
102
        return sprintf($jsTemplate, $article->getId());
103
    }
104
}
105