1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* (c) Christian Gripp <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Core23\MatomoBundle\Block\Service; |
13
|
|
|
|
14
|
|
|
use Core23\MatomoBundle\Client\ClientFactoryInterface; |
15
|
|
|
use Core23\MatomoBundle\Exception\MatomoException; |
16
|
|
|
use Psr\Log\LoggerAwareInterface; |
17
|
|
|
use Psr\Log\LoggerAwareTrait; |
18
|
|
|
use Psr\Log\NullLogger; |
19
|
|
|
use Sonata\AdminBundle\Form\FormMapper; |
20
|
|
|
use Sonata\BlockBundle\Block\BlockContextInterface; |
21
|
|
|
use Sonata\BlockBundle\Block\Service\AbstractAdminBlockService; |
22
|
|
|
use Sonata\BlockBundle\Model\BlockInterface; |
23
|
|
|
use Sonata\CoreBundle\Form\Type\ImmutableArrayType; |
24
|
|
|
use Sonata\CoreBundle\Model\Metadata; |
25
|
|
|
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface; |
26
|
|
|
use Symfony\Component\Form\Extension\Core\Type\ChoiceType; |
27
|
|
|
use Symfony\Component\Form\Extension\Core\Type\NumberType; |
28
|
|
|
use Symfony\Component\Form\Extension\Core\Type\TextType; |
29
|
|
|
use Symfony\Component\HttpFoundation\Response; |
30
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
31
|
|
|
|
32
|
|
|
final class MatomoStatisticBlockService extends AbstractAdminBlockService implements LoggerAwareInterface |
33
|
|
|
{ |
34
|
|
|
use LoggerAwareTrait; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var ClientFactoryInterface |
38
|
|
|
*/ |
39
|
|
|
private $factory; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param string $name |
43
|
|
|
* @param EngineInterface $templating |
44
|
|
|
* @param ClientFactoryInterface $factory |
45
|
|
|
*/ |
46
|
|
|
public function __construct(string $name, EngineInterface $templating, ClientFactoryInterface $factory) |
47
|
|
|
{ |
48
|
|
|
parent::__construct($name, $templating); |
49
|
|
|
|
50
|
|
|
$this->factory = $factory; |
51
|
|
|
$this->logger = new NullLogger(); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* {@inheritdoc} |
56
|
|
|
*/ |
57
|
|
|
public function execute(BlockContextInterface $blockContext, Response $response = null) |
58
|
|
|
{ |
59
|
|
|
return $this->renderResponse($blockContext->getTemplate(), [ |
60
|
|
|
'context' => $blockContext, |
61
|
|
|
'settings' => $blockContext->getSettings(), |
62
|
|
|
'block' => $blockContext->getBlock(), |
63
|
|
|
'data' => $this->getData($blockContext->getSettings()), |
64
|
|
|
], $response); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* {@inheritdoc} |
69
|
|
|
*/ |
70
|
|
|
public function buildEditForm(FormMapper $formMapper, BlockInterface $block): void |
71
|
|
|
{ |
72
|
|
|
$formMapper->add('settings', ImmutableArrayType::class, [ |
73
|
|
|
'keys' => [ |
74
|
|
|
['title', TextType::class, [ |
75
|
|
|
'required' => false, |
76
|
|
|
'label' => 'form.label_title', |
77
|
|
|
]], |
78
|
|
|
['translation_domain', TextType::class, [ |
79
|
|
|
'label' => 'form.label_translation_domain', |
80
|
|
|
'required' => false, |
81
|
|
|
]], |
82
|
|
|
['icon', TextType::class, [ |
83
|
|
|
'label' => 'form.label_icon', |
84
|
|
|
'required' => false, |
85
|
|
|
]], |
86
|
|
|
['class', TextType::class, [ |
87
|
|
|
'label' => 'form.label_class', |
88
|
|
|
'required' => false, |
89
|
|
|
]], |
90
|
|
|
['host', TextType::class, [ |
91
|
|
|
'required' => false, |
92
|
|
|
'label' => 'form.label_host', |
93
|
|
|
]], |
94
|
|
|
['token', TextType::class, [ |
95
|
|
|
'required' => false, |
96
|
|
|
'label' => 'form.label_token', |
97
|
|
|
]], |
98
|
|
|
['site', NumberType::class, [ |
99
|
|
|
'label' => 'form.label_site', |
100
|
|
|
]], |
101
|
|
|
['method', ChoiceType::class, [ |
102
|
|
|
'choices' => [ |
103
|
|
|
'form.choice_visitors' => 'VisitsSummary.getVisits', |
104
|
|
|
'form.choice_unique_visitors' => 'VisitsSummary.getUniqueVisitors', |
105
|
|
|
'form.choice_hits' => 'VisitsSummary.getActions ', |
106
|
|
|
], |
107
|
|
|
'label' => 'form.label_method', |
108
|
|
|
]], |
109
|
|
|
['period', ChoiceType::class, [ |
110
|
|
|
'choices' => [ |
111
|
|
|
'form.choice_day' => 'day', |
112
|
|
|
'form.choice_week' => 'week', |
113
|
|
|
'form.choice_month' => 'month', |
114
|
|
|
'form.choice_year' => 'year', |
115
|
|
|
], |
116
|
|
|
'label' => 'form.label_period', |
117
|
|
|
]], |
118
|
|
|
['date', ChoiceType::class, [ |
119
|
|
|
'choices' => [ |
120
|
|
|
'form.choice_today' => 'last1', |
121
|
|
|
'form.choice_1_week' => 'last7', |
122
|
|
|
'form.choice_2_weeks' => 'last14', |
123
|
|
|
'form.choice_1_month' => 'last30', |
124
|
|
|
'form.choice_3_months' => 'last90', |
125
|
|
|
'form.choice_6_months' => 'last180', |
126
|
|
|
'form.choice_1_year' => 'last360', |
127
|
|
|
], |
128
|
|
|
'label' => 'form.label_date', |
129
|
|
|
]], |
130
|
|
|
], |
131
|
|
|
'translation_domain' => 'Core23MatomoBundle', |
132
|
|
|
]); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* {@inheritdoc} |
137
|
|
|
*/ |
138
|
|
|
public function configureSettings(OptionsResolver $resolver): void |
139
|
|
|
{ |
140
|
|
|
$resolver->setDefaults([ |
141
|
|
|
'title' => null, |
142
|
|
|
'translation_domain' => null, |
143
|
|
|
'icon' => 'fa fa-bar-chart-o', |
144
|
|
|
'class' => null, |
145
|
|
|
'site' => null, |
146
|
|
|
'method' => 'VisitsSummary.getVisits', |
147
|
|
|
'host' => null, |
148
|
|
|
'token' => null, |
149
|
|
|
'period' => 'day', |
150
|
|
|
'date' => 'last30', |
151
|
|
|
'template' => '@Core23Matomo/Block/block_matomo_statistic.html.twig', |
152
|
|
|
]); |
153
|
|
|
|
154
|
|
|
$resolver->setRequired(['site', 'host', 'token']); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* {@inheritdoc} |
159
|
|
|
*/ |
160
|
|
|
public function getBlockMetadata($code = null) |
161
|
|
|
{ |
162
|
|
|
return new Metadata($this->getName(), $code ?? $this->getName(), false, 'Core23MatomoBundle', [ |
163
|
|
|
'class' => 'fa fa-area-chart', |
164
|
|
|
]); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* @param array $settings |
169
|
|
|
* |
170
|
|
|
* @return array|null |
171
|
|
|
*/ |
172
|
|
|
protected function getData(array $settings = []): ?array |
173
|
|
|
{ |
174
|
|
|
try { |
175
|
|
|
$client = $this->factory->createClient($settings['host'], $settings['token']); |
176
|
|
|
|
177
|
|
|
$response = $client->call($settings['method'], [ |
178
|
|
|
'idSite' => $settings['site'], |
179
|
|
|
'period' => $settings['period'], |
180
|
|
|
'date' => $settings['date'], |
181
|
|
|
]); |
182
|
|
|
|
183
|
|
|
return $response; |
184
|
|
|
} catch (MatomoException $ce) { |
185
|
|
|
$this->logger->warning('Error retrieving Matomo url: '.$settings['host'], [ |
186
|
|
|
'exception' => $ce, |
187
|
|
|
]); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
return null; |
191
|
|
|
} |
192
|
|
|
} |
193
|
|
|
|