Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 37
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
MediaWikiServices
0.00% covered (danger)
0.00%
0 / 37
0.00% covered (danger)
0.00%
0 / 1
6
0.00% covered (danger)
0.00%
0 / 1
 onMediaWikiServices
0.00% covered (danger)
0.00%
0 / 37
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2
3namespace MediaWiki\Extension\PageViewInfoGA\Hooks;
4
5use MediaWiki\Extension\PageViewInfo\CachedPageViewService;
6use MediaWiki\Extension\PageViewInfoGA\GoogleAnalyticsPageViewService;
7use MediaWiki\Logger\LoggerFactory;
8use ObjectCache;
9
10class MediaWikiServices implements \MediaWiki\Hook\MediaWikiServicesHook {
11
12    /**
13     * @inheritDoc
14     */
15    public function onMediaWikiServices( $services ) {
16        global $wgPageViewInfoGAProfileId,
17            $wgPageViewInfoGACredentialsFile,
18            $wgPageViewInfoGAWriteCustomMap,
19            $wgPageViewInfoGAReadCustomDimensions,
20            $wgPageViewApiMaxDays;
21
22        $profileId = $wgPageViewInfoGAProfileId;
23        if ( !$profileId ) {
24            return;
25        }
26        $credentialsFile = $wgPageViewInfoGACredentialsFile;
27        $customMap = $wgPageViewInfoGAWriteCustomMap;
28        $readCustomDimensions = $wgPageViewInfoGAReadCustomDimensions;
29        $cache = ObjectCache::getLocalClusterInstance();
30        $logger = LoggerFactory::getInstance( 'PageViewInfoGA' );
31        $cachedDays = max( 30, $wgPageViewApiMaxDays );
32
33        $services->redefineService(
34            'PageViewService',
35            static function () use (
36                $credentialsFile,
37                $profileId,
38                $customMap,
39                $readCustomDimensions,
40                $cache,
41                $logger,
42                $cachedDays
43                ) {
44                $service = new GoogleAnalyticsPageViewService( [
45                    'credentialsFile' => $credentialsFile,
46                    'profileId' => $profileId,
47                    'customMap' => $customMap,
48                    'readCustomDimensions' => $readCustomDimensions,
49                ] );
50
51                $cachedService = new CachedPageViewService( $service, $cache );
52                $cachedService->setCachedDays( $cachedDays );
53                $cachedService->setLogger( $logger );
54                return $cachedService;
55            }
56        );
57    }
58}