Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
Total | |
0.00% |
0 / 1 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 29 |
Statistics | |
0.00% |
0 / 1 |
|
0.00% |
0 / 3 |
30 | |
0.00% |
0 / 29 |
__construct | |
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 2 |
|||
main | |
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 12 |
|||
update | |
0.00% |
0 / 1 |
12 | |
0.00% |
0 / 15 |
1 | <?php |
2 | |
3 | namespace Miniframe\Statistics\Controller; |
4 | |
5 | use GeoIp2\Database\Reader; |
6 | use Miniframe\Core\AbstractController; |
7 | use Miniframe\Core\Config; |
8 | use Miniframe\Core\Request; |
9 | use Miniframe\Core\Response; |
10 | use Miniframe\Response\PhpResponse; |
11 | use Miniframe\Response\ForbiddenResponse; |
12 | use Miniframe\Statistics\Service\Browscap; |
13 | use Miniframe\Statistics\Service\GeoipUpdater; |
14 | use Miniframe\Statistics\Service\Storage; |
15 | |
16 | class Statistics extends AbstractController |
17 | { |
18 | /** |
19 | * Reference to statistics storage |
20 | * |
21 | * @var Storage |
22 | */ |
23 | protected $storage; |
24 | |
25 | /** |
26 | * Initializes the Statistics Controller |
27 | * |
28 | * @param Request $request Reference to the Request object. |
29 | * @param Config $config Reference to the Config object. |
30 | */ |
31 | public function __construct(Request $request, Config $config) |
32 | { |
33 | parent::__construct($request, $config); |
34 | |
35 | $this->storage = new Storage($config->getPath('statistics', 'storage_path')); |
36 | } |
37 | |
38 | /** |
39 | * Returns the main page |
40 | * |
41 | * @return Response |
42 | */ |
43 | public function main(): Response |
44 | { |
45 | $from = new \DateTime(); |
46 | $from->modify('-1 year'); |
47 | $till = new \DateTime(); |
48 | $hitsPerDay = $this->storage->getHitsPerDay($from, $till); |
49 | $hitsPerHourPerWeekday = $this->storage->getHitsPerHourPerWeekday($from, $till); |
50 | $hitsPerPage = $this->storage->getHitsPerPage($from, $till, 25); |
51 | $visitorsPerCountry = $this->storage->getVisitorsPerCountry($from, $till, 25); |
52 | $visitorsPerPlatform = $this->storage->getVisitorsPerPlatform($from, $till, 25); |
53 | $visitorsPerBrowser = $this->storage->getVisitorsPerBrowser($from, $till, 25); |
54 | $visitorsPerBrowserVersion = $this->storage->getVisitorsPerBrowserVersion($from, $till, 25); |
55 | $visitorsPerDeviceType = $this->storage->getVisitorsPerDeviceType($from, $till, 25); |
56 | |
57 | return new PhpResponse(__DIR__ . '/../../templates/statistics.html.php', [ |
58 | 'hitsPerDay' => $hitsPerDay, |
59 | 'hitsPerHourPerWeekday' => $hitsPerHourPerWeekday, |
60 | 'hitsPerPage' => $hitsPerPage, |
61 | 'visitorsPerCountry' => $visitorsPerCountry, |
62 | 'visitorsPerPlatform' => $visitorsPerPlatform, |
63 | 'visitorsPerBrowser' => $visitorsPerBrowser, |
64 | 'visitorsPerBrowserVersion' => $visitorsPerBrowserVersion, |
65 | 'visitorsPerDeviceType' => $visitorsPerDeviceType, |
66 | ]); |
67 | } |
68 | |
69 | /** |
70 | * This method responds to `php public/index.php _STATISTICS update` |
71 | * |
72 | * @return Response |
73 | */ |
74 | public function update(): Response |
75 | { |
76 | if (!$this->request->isShellRequest()) { |
77 | throw new ForbiddenResponse(); |
78 | } |
79 | |
80 | // Update and configure browscap |
81 | echo 'Updating browscap.ini file...' . PHP_EOL; |
82 | flush(); |
83 | |
84 | $browscap = new Browscap($this->config->getPath('statistics', 'browscap_database_path')); |
85 | $browscap->getDatabase(); |
86 | |
87 | // When geoip2/geoip2 is installed, update database files |
88 | if (class_exists(Reader::class)) { |
89 | echo 'Updating GeoIP database...' . PHP_EOL; |
90 | flush(); |
91 | |
92 | $updater = new GeoipUpdater( |
93 | $this->config->get('statistics', 'geoip_licensekey'), |
94 | $this->config->getPath('statistics', 'geoip_database_path') |
95 | ); |
96 | $updater->getDatabase(); |
97 | } else { |
98 | echo 'Skipping GeoIP database' . PHP_EOL; |
99 | flush(); |
100 | } |
101 | |
102 | return new Response('Databases updated' . PHP_EOL); |
103 | } |
104 | } |