Passed
Branch feature/2.0 (ef99fd)
by Jonathan
11:25
created

MatomoStats   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 15
c 2
b 0
f 0
dl 0
loc 46
rs 10
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A handle() 0 15 3
A __construct() 0 6 1
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) 2011-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\Http\RequestHandlers;
16
17
use Fisharebest\Webtrees\Http\ViewResponseTrait;
18
use Fisharebest\Webtrees\Services\ModuleService;
19
use MyArtJaub\Webtrees\Module\WelcomeBlock\WelcomeBlockModule;
20
use MyArtJaub\Webtrees\Module\WelcomeBlock\Services\MatomoStatsService;
21
use Psr\Http\Message\ResponseInterface;
22
use Psr\Http\Message\ServerRequestInterface;
23
use Psr\Http\Server\RequestHandlerInterface;
24
25
/**
26
 * Request handler for retrieving Matomo statistics
27
 */
28
class MatomoStats implements RequestHandlerInterface
29
{
30
    use ViewResponseTrait;
31
    
32
    /**
33
     * @var WelcomeBlockModule
34
     */
35
    private $module;
36
    
37
    /**
38
     * @var MatomoStatsService $matomo_service
39
     */
40
    private $matomo_service;
41
42
    /**
43
     * Constructor for MatomoStats request handler
44
     * @param ModuleService $module_service
45
     * @param MatomoStatsService $matomo_service
46
     */
47
    public function __construct(
48
        ModuleService $module_service,
49
        MatomoStatsService $matomo_service
50
    ) {
51
        $this->module = $module_service->findByInterface(WelcomeBlockModule::class)->first();
52
        $this->matomo_service = $matomo_service;
53
    }
54
55
    /**
56
     * {@inheritDoc}
57
     * @see \Psr\Http\Server\RequestHandlerInterface::handle()
58
     */
59
    public function handle(ServerRequestInterface $request): ResponseInterface
60
    {
61
        $this->layout = 'layouts/ajax';
62
        
63
        $block_id = filter_var($request->getAttribute('block_id'), FILTER_VALIDATE_INT);
64
        $nb_visits_year = $nb_visits_today = null;
65
        
66
        if ($block_id !== false && $this->module->isMatomoEnabled($block_id)) {
67
            $nb_visits_today = $this->matomo_service->visitsToday($this->module, (int) $block_id);
68
            $nb_visits_year = $this->matomo_service->visitsThisYear($this->module, (int) $block_id) + $nb_visits_today;
69
        }
70
        
71
        return $this->viewResponse($this->module->name() . '::matomo-stats', [
72
            'visits_year'   =>  $nb_visits_year,
73
            'visits_today'  =>  $nb_visits_today
74
        ]);
75
    }
76
}
77