Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
* Rector result with cgl applied
* Fix constructor multi line by hand
* Migrate away from prophecy.
  • Loading branch information
DanielSiepmann committed Feb 7, 2024
1 parent 6320129 commit 413afa1
Show file tree
Hide file tree
Showing 56 changed files with 1,252 additions and 1,489 deletions.
418 changes: 360 additions & 58 deletions .php-cs-fixer.dist.php

Large diffs are not rendered by default.

12 changes: 3 additions & 9 deletions Classes/Command/UpdateDataCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,9 @@

class UpdateDataCommand extends Command
{
/**
* @var Pageview
*/
private $repository;

public function __construct(Pageview $repository)
{
$this->repository = $repository;

public function __construct(
private readonly Pageview $repository
) {
parent::__construct();
}

Expand Down
41 changes: 12 additions & 29 deletions Classes/Dashboard/Provider/NewestPageviews.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,42 +23,22 @@

namespace DanielSiepmann\Tracking\Dashboard\Provider;

use Exception;
use TYPO3\CMS\Core\Database\Connection;
use TYPO3\CMS\Core\Database\Query\QueryBuilder;
use TYPO3\CMS\Dashboard\Widgets\ListDataProviderInterface;

class NewestPageviews implements ListDataProviderInterface
{
/**
* @var QueryBuilder
* @param int[] $languageLimitation
*/
private $queryBuilder;

/**
* @var int
*/
private $maxResults;

/**
* @var array
*/
private $pagesToExclude;

/**
* @var array<int>
*/
private $languageLimitation;

public function __construct(
QueryBuilder $queryBuilder,
int $maxResults = 6,
array $pagesToExclude = [],
array $languageLimitation = []
private readonly QueryBuilder $queryBuilder,
private readonly int $maxResults = 6,
private readonly array $pagesToExclude = [],
private readonly array $languageLimitation = []
) {
$this->queryBuilder = $queryBuilder;
$this->maxResults = $maxResults;
$this->pagesToExclude = $pagesToExclude;
$this->languageLimitation = $languageLimitation;
}

public function getItems(): array
Expand Down Expand Up @@ -98,10 +78,13 @@ public function getItems(): array
$this->queryBuilder->where(...$constraints);
}

$items = $this->queryBuilder->execute()->fetchAll();
$items = $this->queryBuilder->executeQuery()->fetchAllAssociative();
foreach ($items as $item) {
if (is_array($item) === false) {
continue;
if (is_string($item['url']) === false) {
throw new Exception('url of item was not string: ' . var_export($item['url'], true), 1707327319);
}
if (is_string($item['user_agent']) === false) {
throw new Exception('user_agent of item was not string: ' . var_export($item['user_agent'], true), 1707327344);
}

$preparedItems[] = sprintf(
Expand Down
62 changes: 18 additions & 44 deletions Classes/Dashboard/Provider/PageviewsPerDay.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
namespace DanielSiepmann\Tracking\Dashboard\Provider;

use DanielSiepmann\Tracking\Extension;
use Doctrine\DBAL\Platforms\SqlitePlatform;
use TYPO3\CMS\Core\Database\Connection;
use TYPO3\CMS\Core\Database\Query\QueryBuilder;
use TYPO3\CMS\Core\Localization\LanguageService;
Expand All @@ -32,49 +33,20 @@

class PageviewsPerDay implements ChartDataProviderInterface
{
/**
* @var LanguageService
*/
private $languageService;

/**
* @var QueryBuilder
*/
private $queryBuilder;

/**
* @var int
*/
private $days;
private LanguageService $languageService;

/**
* @var array<int>
* @param int[] $pagesToExclude
* @param int[] $languageLimitation
*/
private $pagesToExclude;

/**
* @var string
*/
private $dateFormat;

/**
* @var array<int>
*/
private $languageLimitation;

public function __construct(
QueryBuilder $queryBuilder,
int $days = 31,
array $pagesToExclude = [],
array $languageLimitation = [],
string $dateFormat = 'Y-m-d'
private readonly QueryBuilder $queryBuilder,
private readonly int $days = 31,
private readonly array $pagesToExclude = [],
private readonly array $languageLimitation = [],
private readonly string $dateFormat = 'Y-m-d'
) {
$this->languageService = $GLOBALS['LANG'];
$this->queryBuilder = $queryBuilder;
$this->days = $days;
$this->pagesToExclude = $pagesToExclude;
$this->languageLimitation = $languageLimitation;
$this->dateFormat = $dateFormat;
}

public function getChartData(): array
Expand Down Expand Up @@ -102,16 +74,16 @@ private function calculateData(): array
$data = [];

for ($daysBefore = $this->days; $daysBefore >= 0; $daysBefore--) {
$label = date($this->dateFormat, (int)strtotime('-' . $daysBefore . ' day'));
$label = date($this->dateFormat, (int) strtotime('-' . $daysBefore . ' day'));
$labels[$label] = $label;
$data[$label] = 0;
}

$start = (int)strtotime('-' . $this->days . ' day 0:00:00');
$end = (int)strtotime('tomorrow midnight');
$start = (int) strtotime('-' . $this->days . ' day 0:00:00');
$end = (int) strtotime('tomorrow midnight');

foreach ($this->getPageviewsInPeriod($start, $end) as $day) {
$data[$day['label']] = (int)$day['count'];
$data[$day['label']] = (int) $day['count'];
}

return [
Expand Down Expand Up @@ -154,13 +126,15 @@ private function getPageviewsInPeriod(int $start, int $end): array
->groupBy('label')
->orderBy('label', 'ASC')
;

if ($this->queryBuilder->getConnection()->getDatabasePlatform()->getName() === 'sqlite') {
if (
(class_exists(SqlitePlatform::class) && $this->queryBuilder->getConnection()->getDatabasePlatform() instanceof SqlitePlatform)
|| (method_exists($this->queryBuilder->getConnection()->getDatabasePlatform(), 'getName') && $this->queryBuilder->getConnection()->getDatabasePlatform()->getName() === 'sqlite')
) {
$this->queryBuilder->addSelectLiteral('date(crdate, "unixepoch") as "label"');
} else {
$this->queryBuilder->addSelectLiteral('FROM_UNIXTIME(crdate, "%Y-%m-%d") as "label"');
}

return $this->queryBuilder->execute()->fetchAll();
return $this->queryBuilder->executeQuery()->fetchAllAssociative();
}
}
40 changes: 10 additions & 30 deletions Classes/Dashboard/Provider/PageviewsPerOperatingSystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

namespace DanielSiepmann\Tracking\Dashboard\Provider;

use Exception;
use TYPO3\CMS\Core\Database\Connection;
use TYPO3\CMS\Core\Database\Query\QueryBuilder;
use TYPO3\CMS\Dashboard\WidgetApi;
Expand All @@ -31,35 +32,14 @@
class PageviewsPerOperatingSystem implements ChartDataProviderInterface
{
/**
* @var QueryBuilder
* @param int[] $languageLimitation
*/
private $queryBuilder;

/**
* @var int
*/
private $days;

/**
* @var int
*/
private $maxResults;

/**
* @var array<int>
*/
private $languageLimitation;

public function __construct(
QueryBuilder $queryBuilder,
int $days = 31,
int $maxResults = 6,
array $languageLimitation = []
private readonly QueryBuilder $queryBuilder,
private readonly int $days = 31,
private readonly int $maxResults = 6,
private readonly array $languageLimitation = []
) {
$this->queryBuilder = $queryBuilder;
$this->days = $days;
$this->maxResults = $maxResults;
$this->languageLimitation = $languageLimitation;
}

public function getChartData(): array
Expand Down Expand Up @@ -112,13 +92,13 @@ private function getPageViewsPerPage(): array
->orderBy('total', 'desc')
->addOrderBy('operating_system', 'asc')
->setMaxResults($this->maxResults)
->execute()
->fetchAll()
->executeQuery()
->fetchAllAssociative()
;

foreach ($result as $row) {
if (is_array($row) === false) {
continue;
if (is_string($row['operating_system']) === false) {
throw new Exception('operating_system of row was not string: ' . var_export($row['operating_system'], true), 1707326866);
}

$labels[] = mb_strimwidth($row['operating_system'], 0, 50, '');
Expand Down
66 changes: 19 additions & 47 deletions Classes/Dashboard/Provider/PageviewsPerPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

namespace DanielSiepmann\Tracking\Dashboard\Provider;

use Exception;
use TYPO3\CMS\Backend\Utility\BackendUtility;
use TYPO3\CMS\Core\Database\Connection;
use TYPO3\CMS\Core\Database\Query\QueryBuilder;
Expand All @@ -33,49 +34,17 @@
class PageviewsPerPage implements ChartDataProviderInterface
{
/**
* @var QueryBuilder
* @param int[] $pagesToExclude
* @param int[] $languageLimitation
*/
private $queryBuilder;

/**
* @var PageRepository
*/
private $pageRepository;

/**
* @var int
*/
private $days;

/**
* @var int
*/
private $maxResults;

/**
* @var array<int>
*/
private $pagesToExclude;

/**
* @var array<int>
*/
private $languageLimitation;

public function __construct(
QueryBuilder $queryBuilder,
PageRepository $pageRepository,
int $days = 31,
int $maxResults = 6,
array $pagesToExclude = [],
array $languageLimitation = []
private readonly QueryBuilder $queryBuilder,
private readonly PageRepository $pageRepository,
private readonly int $days = 31,
private readonly int $maxResults = 6,
private readonly array $pagesToExclude = [],
private readonly array $languageLimitation = []
) {
$this->queryBuilder = $queryBuilder;
$this->pageRepository = $pageRepository;
$this->days = $days;
$this->maxResults = $maxResults;
$this->pagesToExclude = $pagesToExclude;
$this->languageLimitation = $languageLimitation;
}

public function getChartData(): array
Expand Down Expand Up @@ -136,16 +105,16 @@ private function getPageviewsPerPage(): array
->orderBy('total', 'desc')
->addOrderBy('latest', 'desc')
->setMaxResults($this->maxResults)
->execute()
->fetchAll()
->executeQuery()
->fetchAllAssociative()
;

foreach ($result as $row) {
if (is_array($row) === false) {
continue;
if (is_numeric($row['pid']) === false) {
throw new Exception('PID of row was not numeric: ' . var_export($row['pid'], true), 1707326783);
}

$labels[] = $this->getRecordTitle((int)$row['pid']);
$labels[] = $this->getRecordTitle((int) $row['pid']);
$data[] = $row['total'];
}

Expand All @@ -159,13 +128,16 @@ private function getRecordTitle(int $uid): string
{
$record = BackendUtility::getRecord('pages', $uid);
if (count($this->languageLimitation) === 1 && $record !== null) {
$record = $this->pageRepository->getRecordOverlay('pages', $record, $this->languageLimitation[0]);
$record = $this->pageRepository->getPageOverlay(
$record,
$this->languageLimitation[0]
);
}

if (is_array($record) === false) {
return 'Unkown';
}

return strip_tags(BackendUtility::getRecordTitle('pages', $record, true));
return strip_tags((string) BackendUtility::getRecordTitle('pages', $record, true));
}
}
Loading

0 comments on commit 413afa1

Please sign in to comment.