1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* webtrees-lib: MyArtJaub library for webtrees |
5
|
|
|
* |
6
|
|
|
* @package MyArtJaub\Webtrees |
7
|
|
|
* @subpackage WelcomeBlock |
8
|
|
|
* @author Jonathan Jaubart <[email protected]> |
9
|
|
|
* @copyright Copyright (c) 2020, Jonathan Jaubart |
10
|
|
|
* @license http://www.gnu.org/licenses/gpl.html GNU General Public License, version 3 |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
declare(strict_types=1); |
14
|
|
|
|
15
|
|
|
namespace MyArtJaub\Webtrees\Module\WelcomeBlock\Services; |
16
|
|
|
|
17
|
|
|
use Fig\Http\Message\StatusCodeInterface; |
18
|
|
|
use Fisharebest\Webtrees\Cache; |
19
|
|
|
use Fisharebest\Webtrees\Carbon; |
20
|
|
|
use GuzzleHttp\Client; |
21
|
|
|
use GuzzleHttp\RequestOptions; |
22
|
|
|
use GuzzleHttp\Exception\RequestException; |
23
|
|
|
use MyArtJaub\Webtrees\Module\WelcomeBlock\WelcomeBlockModule; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Service for retrieving Matomo statistics |
27
|
|
|
* |
28
|
|
|
*/ |
29
|
|
|
class MatomoStatsService |
30
|
|
|
{ |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Returns the number of visits for the current year (up to the day before). |
34
|
|
|
* That statistic is cached for the day, to avoid unecessary calls to Matomo API. |
35
|
|
|
* |
36
|
|
|
* @param WelcomeBlockModule $module |
37
|
|
|
* @param int $block_id |
38
|
|
|
* @return int|NULL |
39
|
|
|
*/ |
40
|
|
|
public function visitsThisYear(WelcomeBlockModule $module, int $block_id): ?int |
41
|
|
|
{ |
42
|
|
|
/** @var Cache $cache */ |
43
|
|
|
$cache = app('cache.files'); |
44
|
|
|
assert($cache instanceof Cache); |
45
|
|
|
|
46
|
|
|
return $cache->remember( |
47
|
|
|
$module->name() . '-matomovisits-yearly-' . $block_id, |
48
|
|
|
function () use ($module, $block_id): ?int { |
49
|
|
|
$visits_year = $this->visits($module, $block_id, 'year'); |
50
|
|
|
if ($visits_year === null) { |
51
|
|
|
return null; |
52
|
|
|
} |
53
|
|
|
$visits_today = $this->visits($module, $block_id, 'day'); |
54
|
|
|
|
55
|
|
|
return $visits_year - $visits_today; |
56
|
|
|
}, |
57
|
|
|
Carbon::now()->addDay()->startOfDay()->diffInSeconds(Carbon::now()) // Valid until midnight |
58
|
|
|
); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Returns the number of visits for the current day. |
63
|
|
|
* |
64
|
|
|
* @param WelcomeBlockModule $module |
65
|
|
|
* @param int $block_id |
66
|
|
|
* @return int|NULL |
67
|
|
|
*/ |
68
|
|
|
public function visitsToday(WelcomeBlockModule $module, int $block_id): ?int |
69
|
|
|
{ |
70
|
|
|
return app('cache.array')->remember( |
71
|
|
|
$module->name() . '-matomovisits-daily-' . $block_id, |
72
|
|
|
function () use ($module, $block_id): ?int { |
73
|
|
|
return $this->visits($module, $block_id, 'day'); |
74
|
|
|
} |
75
|
|
|
); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Invoke the Matomo API to retrieve the number of visits over a period. |
80
|
|
|
* |
81
|
|
|
* @param WelcomeBlockModule $module |
82
|
|
|
* @param int $block_id |
83
|
|
|
* @param string $period |
84
|
|
|
* @return int|NULL |
85
|
|
|
*/ |
86
|
|
|
protected function visits(WelcomeBlockModule $module, int $block_id, string $period): ?int |
87
|
|
|
{ |
88
|
|
|
$settings = $module->matomoSettings($block_id); |
89
|
|
|
|
90
|
|
|
if ( |
91
|
|
|
$settings['matomo_enabled'] === true |
92
|
|
|
&& mb_strlen($settings['matomo_url']) > 0 |
93
|
|
|
&& mb_strlen($settings['matomo_token']) > 0 |
94
|
|
|
&& $settings['matomo_siteid'] > 0 |
95
|
|
|
) { |
96
|
|
|
try { |
97
|
|
|
$http_client = new Client([ |
98
|
|
|
RequestOptions::TIMEOUT => 30 |
99
|
|
|
]); |
100
|
|
|
|
101
|
|
|
$response = $http_client->get($settings['matomo_url'], [ |
102
|
|
|
'query' => [ |
103
|
|
|
'module' => 'API', |
104
|
|
|
'method' => 'VisitsSummary.getVisits', |
105
|
|
|
'idSite' => $settings['matomo_siteid'], |
106
|
|
|
'period' => $period, |
107
|
|
|
'date' => 'today', |
108
|
|
|
'token_auth' => $settings['matomo_token'], |
109
|
|
|
'format' => 'json' |
110
|
|
|
] |
111
|
|
|
]); |
112
|
|
|
|
113
|
|
|
if ($response->getStatusCode() === StatusCodeInterface::STATUS_OK) { |
114
|
|
|
$result = json_decode((string) $response->getBody(), true)['value'] ?? null; |
115
|
|
|
if ($result !== null) { |
116
|
|
|
return (int)$result; |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
} catch (RequestException $ex) { |
|
|
|
|
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
return null; |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|