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 |
|
|
|
|
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) { |
|
|
|
|
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) { |
|
|
|
|
99
|
|
|
$jsTemplate = str_replace('/_swp_analytics', $this->analyticsHost.'/_swp_analytics', $jsTemplate); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
return sprintf($jsTemplate, $article->getId()); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
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 methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.