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