Files
firefly-iii/app/Generator/Report/ReportGeneratorFactory.php

65 lines
2.0 KiB
PHP
Raw Normal View History

<?php
2022-12-29 19:41:57 +01:00
/**
* ReportGeneratorFactory.php
2020-01-28 08:45:28 +01:00
* Copyright (c) 2019 james@firefly-iii.org
*
* This file is part of Firefly III (https://github.com/firefly-iii).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
2017-10-21 08:40:00 +02:00
*
* This program is distributed in the hope that it will be useful,
2017-10-21 08:40:00 +02:00
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
2017-10-21 08:40:00 +02:00
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace FireflyIII\Generator\Report;
use Carbon\Carbon;
use FireflyIII\Exceptions\FireflyException;
/**
2017-11-15 12:25:49 +01:00
* Class ReportGeneratorFactory.
*/
class ReportGeneratorFactory
{
/**
* Static report generator class.
*
2021-03-21 09:15:40 +01:00
* @throws FireflyException
*/
public static function reportGenerator(string $type, Carbon $start, Carbon $end): ReportGeneratorInterface
{
$period = 'Month';
// more than two months date difference means year report.
2024-03-17 12:00:28 +01:00
if ($start->diffInMonths($end, true) > 1) {
$period = 'Year';
}
2024-03-17 12:00:28 +01:00
// more than one year date difference means multi-year report.
if ($start->diffInMonths($end, true) > 12) {
2016-11-09 21:46:32 +01:00
$period = 'MultiYear';
}
$class = sprintf('FireflyIII\Generator\Report\%s\%sReportGenerator', $type, $period);
if (class_exists($class)) {
/** @var ReportGeneratorInterface $obj */
2017-03-05 18:15:38 +01:00
$obj = app($class);
$obj->setStartDate($start);
$obj->setEndDate($end);
return $obj;
}
2023-12-20 19:35:52 +01:00
throw new FireflyException(sprintf('Cannot generate report. There is no "%s"-report for period "%s".', $type, $period));
}
}