Skip to content

Commit

Permalink
Make Database trait a singleton (#222)
Browse files Browse the repository at this point in the history
fixes #219
  • Loading branch information
yhabteab authored Nov 16, 2023
2 parents b1bfd82 + d3bfeac commit 7676092
Show file tree
Hide file tree
Showing 20 changed files with 49 additions and 55 deletions.
3 changes: 2 additions & 1 deletion application/clicommands/DownloadCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Icinga\Exception\NotFoundError;
use Icinga\Module\Pdfexport\ProvidedHook\Pdfexport;
use Icinga\Module\Reporting\Cli\Command;
use Icinga\Module\Reporting\Database;
use Icinga\Module\Reporting\Model;
use Icinga\Module\Reporting\Report;
use InvalidArgumentException;
Expand Down Expand Up @@ -48,7 +49,7 @@ public function defaultAction()
}

/** @var Model\Report $report */
$report = Model\Report::on($this->getDb())
$report = Model\Report::on(Database::get())
->with('timeframe')
->filter(Filter::equal('id', $id))
->first();
Expand Down
3 changes: 2 additions & 1 deletion application/clicommands/ListCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Icinga\Module\Reporting\Clicommands;

use Icinga\Module\Reporting\Cli\Command;
use Icinga\Module\Reporting\Database;
use Icinga\Module\Reporting\Model;
use InvalidArgumentException;
use ipl\Stdlib\Filter;
Expand Down Expand Up @@ -56,7 +57,7 @@ public function indexAction()

$direction = $this->params->get('direction', 'ASC');

$reports = Model\Report::on($this->getDb());
$reports = Model\Report::on(Database::get());
$reports
->with(['reportlets'])
->orderBy($sort, $direction);
Expand Down
3 changes: 2 additions & 1 deletion application/clicommands/ScheduleCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Icinga\Application\Logger;
use Icinga\Data\ResourceFactory;
use Icinga\Module\Reporting\Cli\Command;
use Icinga\Module\Reporting\Database;
use Icinga\Module\Reporting\Model;
use Icinga\Module\Reporting\Report;
use Icinga\Module\Reporting\Schedule;
Expand Down Expand Up @@ -103,7 +104,7 @@ public function runAction()
protected function fetchSchedules(): array
{
$schedules = [];
$query = Model\Schedule::on($this->getDb())->with(['report.timeframe', 'report']);
$query = Model\Schedule::on(Database::get())->with(['report.timeframe', 'report']);

foreach ($query as $schedule) {
$schedule = Schedule::fromModel($schedule, Report::fromModel($schedule->report));
Expand Down
4 changes: 1 addition & 3 deletions application/controllers/ReportController.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@

class ReportController extends Controller
{
use Database;

/** @var Report */
protected $report;

Expand All @@ -36,7 +34,7 @@ public function init()
$reportId = $this->params->getRequired('id');

/** @var Model\Report $report */
$report = Model\Report::on($this->getDb())
$report = Model\Report::on(Database::get())
->with(['timeframe'])
->filter(Filter::equal('id', $reportId))
->first();
Expand Down
3 changes: 1 addition & 2 deletions application/controllers/ReportsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

class ReportsController extends Controller
{
use Database;
use ReportsTimeframesAndTemplatesTabs;

public function indexAction()
Expand All @@ -37,7 +36,7 @@ public function indexAction()

$tableRows = [];

$reports = Report::on($this->getDb())
$reports = Report::on(Database::get())
->withColumns(['report.timeframe.name']);

$sortControl = $this->createSortControl(
Expand Down
4 changes: 1 addition & 3 deletions application/controllers/TemplateController.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@

class TemplateController extends Controller
{
use Database;

/** @var Model\Template */
protected $template;

Expand All @@ -32,7 +30,7 @@ public function init()
parent::init();

/** @var Model\Template $template */
$template = Model\Template::on($this->getDb())
$template = Model\Template::on(Database::get())
->filter(Filter::equal('id', $this->params->getRequired('id')))
->first();

Expand Down
3 changes: 1 addition & 2 deletions application/controllers/TemplatesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

class TemplatesController extends Controller
{
use Database;
use ReportsTimeframesAndTemplatesTabs;

public function indexAction()
Expand All @@ -36,7 +35,7 @@ public function indexAction()
);
}

$templates = Model\Template::on($this->getDb());
$templates = Model\Template::on(Database::get());

$sortControl = $this->createSortControl(
$templates,
Expand Down
4 changes: 1 addition & 3 deletions application/controllers/TimeframeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,13 @@

class TimeframeController extends Controller
{
use Database;

/** @var Timeframe */
protected $timeframe;

public function init()
{
/** @var Model\Timeframe $timeframe */
$timeframe = Model\Timeframe::on($this->getDb())
$timeframe = Model\Timeframe::on(Database::get())
->filter(Filter::equal('id', $this->params->getRequired('id')))
->first();

Expand Down
3 changes: 1 addition & 2 deletions application/controllers/TimeframesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

class TimeframesController extends Controller
{
use Database;
use ReportsTimeframesAndTemplatesTabs;

public function indexAction()
Expand All @@ -38,7 +37,7 @@ public function indexAction()

$tableRows = [];

$timeframes = Model\Timeframe::on($this->getDb());
$timeframes = Model\Timeframe::on(Database::get());

$sortControl = $this->createSortControl(
$timeframes,
Expand Down
3 changes: 0 additions & 3 deletions library/Reporting/Cli/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,9 @@

use Icinga\Application\Icinga;
use Icinga\Application\Version;
use Icinga\Module\Reporting\Database;

class Command extends \Icinga\Cli\Command
{
use Database;

// Fix Web 2 issue where $configs is not properly initialized
protected $configs = [];

Expand Down
37 changes: 29 additions & 8 deletions library/Reporting/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,30 @@
use PDO;
use stdClass;

trait Database
final class Database
{
protected function getDb(): RetryConnection
/** @var RetryConnection Database connection */
private static $instance;

private function __construct()
{
}

/**
* Get the database connection
*
* @return RetryConnection
*/
public static function get(): RetryConnection
{
if (self::$instance === null) {
self::$instance = self::getDb();
}

return self::$instance;
}

private static function getDb(): RetryConnection
{
$config = new Sql\Config(
ResourceFactory::getResourceConfig(
Expand All @@ -34,9 +55,9 @@ protected function getDb(): RetryConnection
*
* @return array<int, string>
*/
protected function listTimeframes(): array
public static function listTimeframes(): array
{
return $this->list(
return self::list(
(new Sql\Select())
->from('timeframe')
->columns(['id', 'name'])
Expand All @@ -48,9 +69,9 @@ protected function listTimeframes(): array
*
* @return array<int, string>
*/
protected function listTemplates(): array
public static function listTemplates(): array
{
return $this->list(
return self::list(
(new Sql\Select())
->from('template')
->columns(['id', 'name'])
Expand All @@ -64,11 +85,11 @@ protected function listTemplates(): array
*
* @return array<int, string>
*/
private function list(Sql\Select $select): array
private static function list(Sql\Select $select): array
{
$result = [];
/** @var stdClass $row */
foreach ($this->getDb()->select($select) as $row) {
foreach (self::get()->select($select) as $row) {
/** @var int $id */
$id = $row->id;
/** @var string $name */
Expand Down
6 changes: 1 addition & 5 deletions library/Reporting/ProvidedHook/DbMigration.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@

class DbMigration extends DbMigrationHook
{
use Database {
getDb as private getReportingDb;
}

public function getName(): string
{
return $this->translate('Icinga Reporting');
Expand All @@ -40,7 +36,7 @@ protected function getSchemaQuery(): Query

public function getDb(): Connection
{
return $this->getReportingDb();
return Database::get();
}

public function getVersion(): string
Expand Down
2 changes: 0 additions & 2 deletions library/Reporting/Report.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@

class Report
{
use Database;

/** @var int */
protected $id;

Expand Down
2 changes: 0 additions & 2 deletions library/Reporting/Timeframe.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@

class Timeframe
{
use Database;

/** @var int */
protected $id;

Expand Down
7 changes: 3 additions & 4 deletions library/Reporting/Web/Forms/ReportForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

class ReportForm extends CompatForm
{
use Database;
use ProvidedReports;

protected $id;
Expand Down Expand Up @@ -125,15 +124,15 @@ protected function assemble()
'required' => true,
'class' => 'autosubmit',
'label' => $this->translate('Timeframe'),
'options' => [null => $this->translate('Please choose')] + $this->listTimeframes(),
'options' => [null => $this->translate('Please choose')] + Database::listTimeframes(),
'description' => $this->translate(
'Specifies the time frame in which this report is to be generated'
)
]);

$this->addElement('select', 'template', [
'label' => $this->translate('Template'),
'options' => [null => $this->translate('Please choose')] + $this->listTemplates(),
'options' => [null => $this->translate('Please choose')] + Database::listTemplates(),
'description' => $this->translate(
'Specifies the template to use when exporting this report to pdf. (Default Icinga template)'
)
Expand Down Expand Up @@ -193,7 +192,7 @@ protected function assemble()

public function onSuccess()
{
$db = $this->getDb();
$db = Database::get();

if ($this->getPopulatedValue('remove')) {
$db->delete('report', ['id = ?' => $this->id]);
Expand Down
3 changes: 1 addition & 2 deletions library/Reporting/Web/Forms/ScheduleForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@

class ScheduleForm extends CompatForm
{
use Database;
use ProvidedActions;

/** @var Report */
Expand Down Expand Up @@ -144,7 +143,7 @@ protected function assemble()

public function onSuccess()
{
$db = $this->getDb();
$db = Database::get();
$schedule = $this->report->getSchedule();
if ($this->getPopulatedValue('remove')) {
$db->delete('schedule', ['id = ?' => $schedule->getId()]);
Expand Down
2 changes: 0 additions & 2 deletions library/Reporting/Web/Forms/SendForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,12 @@
namespace Icinga\Module\Reporting\Web\Forms;

use Icinga\Module\Reporting\Actions\SendMail;
use Icinga\Module\Reporting\Database;
use Icinga\Module\Reporting\ProvidedReports;
use Icinga\Module\Reporting\Report;
use ipl\Web\Compat\CompatForm;

class SendForm extends CompatForm
{
use Database;
use ProvidedReports;

/** @var Report */
Expand Down
6 changes: 2 additions & 4 deletions library/Reporting/Web/Forms/TemplateForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@

class TemplateForm extends CompatForm
{
use Database;

protected $template;

public function getTemplate()
Expand Down Expand Up @@ -158,7 +156,7 @@ protected function assemble()
public function onSuccess()
{
if ($this->getPopulatedValue('remove')) {
$this->getDb()->delete('template', ['id = ?' => $this->template->id]);
Database::get()->delete('template', ['id = ?' => $this->template->id]);

return;
}
Expand All @@ -178,7 +176,7 @@ public function onSuccess()
}
}

$db = $this->getDb();
$db = Database::get();

$now = time() * 1000;

Expand Down
4 changes: 1 addition & 3 deletions library/Reporting/Web/Forms/TimeframeForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@

class TimeframeForm extends CompatForm
{
use Database;

/** @var int */
protected $id;

Expand Down Expand Up @@ -184,7 +182,7 @@ protected function assemble()

public function onSuccess()
{
$db = $this->getDb();
$db = Database::get();

if ($this->getPopulatedValue('remove')) {
$db->delete('timeframe', ['id = ?' => $this->id]);
Expand Down
Loading

0 comments on commit 7676092

Please sign in to comment.