-
Notifications
You must be signed in to change notification settings - Fork 0
/
API.php
101 lines (86 loc) · 3.38 KB
/
API.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
<?php
namespace Piwik\Plugins\IndexationViewer;
use Exception;
use Piwik\Period\Factory;
use Piwik\Piwik;
use Piwik\Plugin\SettingsProvider;
use Piwik\Plugins\API\API as BaseAPI;
use Piwik\Plugins\API\ProcessedReport;
use Piwik\Plugins\IndexationViewer\Dao\IndexationStatus as IndexationStatusDao;
use Piwik\Plugins\IndexationViewer\Dao\IndexationStatusInterface;
use Piwik\Plugins\IndexationViewer\Dto\IndexationStatus as IndexationStatusDto;
use Piwik\Plugins\IndexationViewer\Dto\SegmentOption as SegmentOptionDto;
use Piwik\Plugins\IndexationViewer\Model\SegmentOption as SegmentOptionModel;
use Piwik\Plugins\IndexationViewer\Utils\ArchiveStatus;
use Piwik\Plugins\SegmentEditor;
use Piwik\Plugins\UsersManager\UserPreferences;
use Piwik\Segment;
final class API extends BaseAPI
{
/** @var IndexationStatusInterface */
private $indexationStatusDao;
/** @var UserPreferences */
private $userPreferences;
public function __construct(
SettingsProvider $settingsProvider,
ProcessedReport $processedReport,
IndexationStatusDao $indexationStatusDao,
UserPreferences $userPreferences
) {
parent::__construct($settingsProvider, $processedReport);
$this->indexationStatusDao = $indexationStatusDao;
$this->userPreferences = $userPreferences;
}
// Public methods -------------------------------------------------------------------------------------------------
/**
* @param int|null $idSite
* @param string|null $date
* @param string|null $period
* @param string|null $segment
* @param string|null $status
* @param int|null $page
* @param int|null $limitPerPage
* @return array<IndexationStatusDto>
* @throws Exception If user does not have superuser access.
*/
public function getIndexationStatuses(
?int $idSite = null,
?string $date = 'today',
?string $period = 'day',
?string $segment = '',
?string $status = '',
?int $page = 1,
?int $limitPerPage = 15
): array {
Piwik::checkUserHasSuperUserAccess();
try {
$idSite = $idSite ?? intval($this->userPreferences->getDefaultWebsiteId());
$segment = new Segment($segment, [$idSite]);
$period = Factory::build($period, $date);
$status = ArchiveStatus::resolve($status);
} catch (Exception $e) {
return [];
}
$data = $this->indexationStatusDao->findAll($idSite, $period, $segment, $status, $page, $limitPerPage);
return IndexationStatusDto::fromModelList($data);
}
/**
* @param int|null $idSite
* @return array<SegmentOptionDto>
* @throws Exception If user does not have superuser access.
*/
public function getSegmentOptions(?int $idSite = null): array
{
Piwik::checkUserHasSuperUserAccess();
$segmentOptions = array_map(function (array $data): SegmentOptionModel {
return new SegmentOptionModel($data);
}, SegmentEditor\API::getInstance()->getAll($idSite ?? false));
// Add default segment to the list.
$defaultSegment = new SegmentOptionModel([
'definition' => '',
'name' => Piwik::translate('IndexationViewer_DefaultSegment'),
]);
array_unshift($segmentOptions, $defaultSegment);
return SegmentOptionDto::fromModelList($segmentOptions);
}
}