Use DI instead of unnecessary code

This commit is contained in:
Daniel Siepmann 2020-02-24 14:49:31 +01:00
parent 2105b79b8a
commit ab6c7d2967
Signed by: Daniel Siepmann
GPG key ID: 33D6629915560EF4
4 changed files with 85 additions and 34 deletions

View file

@ -22,8 +22,7 @@ namespace DanielSiepmann\Tracking\Dashboard\Widgets;
*/ */
use TYPO3\CMS\Core\Database\Connection; use TYPO3\CMS\Core\Database\Connection;
use TYPO3\CMS\Core\Database\ConnectionPool; use TYPO3\CMS\Core\Database\Query\QueryBuilder;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Dashboard\Widgets\AbstractBarChartWidget; use TYPO3\CMS\Dashboard\Widgets\AbstractBarChartWidget;
class PageViewsBar extends AbstractBarChartWidget class PageViewsBar extends AbstractBarChartWidget
@ -36,6 +35,18 @@ class PageViewsBar extends AbstractBarChartWidget
protected $height = 4; protected $height = 4;
/**
* @var QueryBuilder
*/
protected $queryBuilder;
public function __construct(string $identifier, QueryBuilder $queryBuilder)
{
parent::__construct($identifier);
$this->queryBuilder = $queryBuilder;
$this->identifier = $identifier;
}
protected function prepareChartData(): void protected function prepareChartData(): void
{ {
list($labels, $data) = $this->calculateDataForLastDays(31); list($labels, $data) = $this->calculateDataForLastDays(31);
@ -57,18 +68,15 @@ class PageViewsBar extends AbstractBarChartWidget
protected function getPageViewsInPeriod(int $start, int $end): int protected function getPageViewsInPeriod(int $start, int $end): int
{ {
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class) return (int)$this->queryBuilder
->getQueryBuilderForTable('tx_tracking_pageview');
return (int)$queryBuilder
->count('*') ->count('*')
->from('tx_tracking_pageview') ->from('tx_tracking_pageview')
->where( ->where(
$queryBuilder->expr()->gte('tstamp', $start), $this->queryBuilder->expr()->gte('tstamp', $start),
$queryBuilder->expr()->lte('tstamp', $end), $this->queryBuilder->expr()->lte('tstamp', $end),
$queryBuilder->expr()->notIn( $this->queryBuilder->expr()->notIn(
'tx_tracking_pageview.pid', 'tx_tracking_pageview.pid',
$queryBuilder->createNamedParameter([ $this->queryBuilder->createNamedParameter([
1, 1,
11, 11,
38, 38,

View file

@ -23,9 +23,7 @@ namespace DanielSiepmann\Tracking\Dashboard\Widgets;
use Doctrine\DBAL\ParameterType; use Doctrine\DBAL\ParameterType;
use TYPO3\CMS\Core\Database\Connection; use TYPO3\CMS\Core\Database\Connection;
use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\Database\Query\QueryBuilder; use TYPO3\CMS\Core\Database\Query\QueryBuilder;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Dashboard\Widgets\AbstractDoughnutChartWidget; use TYPO3\CMS\Dashboard\Widgets\AbstractDoughnutChartWidget;
class PageViewsPerPageDoughnut extends AbstractDoughnutChartWidget class PageViewsPerPageDoughnut extends AbstractDoughnutChartWidget
@ -34,6 +32,17 @@ class PageViewsPerPageDoughnut extends AbstractDoughnutChartWidget
protected $description = 'LLL:EXT:tracking/Resources/Private/Language/locallang.xlf:dashboard.widgets.pageViewsPerPageDoughnut.description'; protected $description = 'LLL:EXT:tracking/Resources/Private/Language/locallang.xlf:dashboard.widgets.pageViewsPerPageDoughnut.description';
/**
* @var QueryBuilder
*/
protected $queryBuilder;
public function __construct(string $identifier, QueryBuilder $queryBuilder)
{
parent::__construct($identifier);
$this->queryBuilder = $queryBuilder;
}
protected function prepareChartData(): void protected function prepareChartData(): void
{ {
list($labels, $data) = $this->getPageViewsPerPage(31); list($labels, $data) = $this->getPageViewsPerPage(31);
@ -53,9 +62,8 @@ class PageViewsPerPageDoughnut extends AbstractDoughnutChartWidget
{ {
$labels = []; $labels = [];
$data = []; $data = [];
$queryBuilder = $this->getQueryBuilder();
$result = $queryBuilder $result = $this->queryBuilder
->selectLiteral('count(tx_tracking_pageview.pid) as total') ->selectLiteral('count(tx_tracking_pageview.pid) as total')
->addSelect('pages.title', 'pages.uid') ->addSelect('pages.title', 'pages.uid')
->from('tx_tracking_pageview') ->from('tx_tracking_pageview')
@ -63,12 +71,15 @@ class PageViewsPerPageDoughnut extends AbstractDoughnutChartWidget
'tx_tracking_pageview', 'tx_tracking_pageview',
'pages', 'pages',
'pages', 'pages',
$queryBuilder->expr()->eq('tx_tracking_pageview.pid', $queryBuilder->quoteIdentifier('pages.uid')) $this->queryBuilder->expr()->eq(
'tx_tracking_pageview.pid',
$this->queryBuilder->quoteIdentifier('pages.uid')
)
) )
->where( ->where(
$queryBuilder->expr()->notIn( $this->queryBuilder->expr()->notIn(
'tx_tracking_pageview.pid', 'tx_tracking_pageview.pid',
$queryBuilder->createNamedParameter([ $this->queryBuilder->createNamedParameter([
1, 1,
11, 11,
38, 38,
@ -91,9 +102,4 @@ class PageViewsPerPageDoughnut extends AbstractDoughnutChartWidget
$data, $data,
]; ];
} }
private function getQueryBuilder(): QueryBuilder
{
return GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('tx_tracking_pageview');
}
} }

View file

@ -7,17 +7,27 @@ services:
DanielSiepmann\Tracking\: DanielSiepmann\Tracking\:
resource: '../Classes/*' resource: '../Classes/*'
DanielSiepmann\DI\DatabaseConnection\Pageview: # Virtual services
DanielSiepmann\Tracking\DI\DatabaseConnection\Pageview:
factory: factory:
- '@TYPO3\CMS\Core\Database\ConnectionPool' - '@TYPO3\CMS\Core\Database\ConnectionPool'
- 'getConnectionForTable' - 'getConnectionForTable'
arguments: arguments:
- 'tx_tracking_pageview' - 'tx_tracking_pageview'
DanielSiepmann\Tracking\DI\QueryBuilder\PageView:
factory:
- '@TYPO3\CMS\Core\Database\ConnectionPool'
- 'getQueryBuilderForTable'
arguments:
- 'tx_tracking_pageview'
# Existing classes
DanielSiepmann\Tracking\Domain\Repository\Pageview: DanielSiepmann\Tracking\Domain\Repository\Pageview:
public: true public: true
arguments: arguments:
- '@DanielSiepmann\DI\DatabaseConnection\Pageview' - '@DanielSiepmann\Tracking\DI\DatabaseConnection\Pageview'
DanielSiepmann\Tracking\Middleware\Pageview: DanielSiepmann\Tracking\Middleware\Pageview:
public: true public: true
@ -30,17 +40,21 @@ services:
# Dashboard Widgets # Dashboard Widgets
DanielSiepmann\Tracking\Dashboard\Widgets\PageViewsBar: DanielSiepmann\Tracking\Dashboard\Widgets\PageViewsBar:
class: DanielSiepmann\Tracking\Dashboard\Widgets\PageViewsBar class: 'DanielSiepmann\Tracking\Dashboard\Widgets\PageViewsBar'
arguments: [pageViewsBar] arguments:
- 'pageViewsBar'
- '@DanielSiepmann\Tracking\DI\QueryBuilder\PageView'
tags: tags:
- name: dashboard.widget - name: 'dashboard.widget'
identifier: pageViewsBar identifier: 'pageViewsBar'
widgetGroups: tracking widgetGroups: 'tracking'
DanielSiepmann\Tracking\Dashboard\Widgets\PageViewsPerPageDoughnut: DanielSiepmann\Tracking\Dashboard\Widgets\PageViewsPerPageDoughnut:
class: DanielSiepmann\Tracking\Dashboard\Widgets\PageViewsPerPageDoughnut class: 'DanielSiepmann\Tracking\Dashboard\Widgets\PageViewsPerPageDoughnut'
arguments: [pageViewsPerPageDoughnut] arguments:
- 'pageViewsPerPageDoughnut'
- '@DanielSiepmann\Tracking\DI\QueryBuilder\PageView'
tags: tags:
- name: dashboard.widget - name: 'dashboard.widget'
identifier: pageViewsPerPageDoughnut identifier: 'pageViewsPerPageDoughnut'
widgetGroups: tracking widgetGroups: 'tracking'

23
readme.rst Normal file
View file

@ -0,0 +1,23 @@
Todos
=====
#. Add command that will iterate over all DB entries and remove ones matching the black list rule.
E.g. if rule is adjusted in meanwhile.
#. Add further widgets.
#. Top 404 requests (Collect them to show them).
#. Grouped by user agents.
#. Move bot detection to another rule.
#. Keep indexing those requests, but mark them as bot and separate them in widgets.
#. Provide an overview of crawls as widgets. E.g. to allow fine grained robots.txt.
#. Add information to Admin Panel.
#. Add operating System
#. Another Symfony Expression which returns the OS ("Ubuntu", "Macintosh", "Android", "iPhone", "Windows")