firefly-iii/app/Transformers/RuleTransformer.php

162 lines
5.2 KiB
PHP
Raw Normal View History

2018-06-23 23:51:22 -05:00
<?php
/**
* RuleTransformer.php
2020-02-16 06:57:18 -06:00
* Copyright (c) 2019 james@firefly-iii.org
2018-06-23 23:51:22 -05:00
*
* This file is part of Firefly III (https://github.com/firefly-iii).
2018-06-23 23:51:22 -05:00
*
* 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.
2018-06-23 23:51:22 -05:00
*
* This program is distributed in the hope that it will be useful,
2018-06-23 23:51:22 -05: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.
2018-06-23 23:51:22 -05: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/>.
2018-06-23 23:51:22 -05:00
*/
declare(strict_types=1);
namespace FireflyIII\Transformers;
use FireflyIII\Exceptions\FireflyException;
2018-06-23 23:51:22 -05:00
use FireflyIII\Models\Rule;
2018-12-07 08:36:04 -06:00
use FireflyIII\Models\RuleAction;
use FireflyIII\Models\RuleTrigger;
2018-12-20 13:50:05 -06:00
use FireflyIII\Repositories\Rule\RuleRepositoryInterface;
2018-06-23 23:51:22 -05:00
/**
* Class RuleTransformer
*/
class RuleTransformer extends AbstractTransformer
2018-06-23 23:51:22 -05:00
{
2018-12-20 13:50:05 -06:00
/** @var RuleRepositoryInterface */
private $ruleRepository;
2018-06-23 23:51:22 -05:00
/**
* CurrencyTransformer constructor.
*
* @codeCoverageIgnore
*/
public function __construct()
2018-06-23 23:51:22 -05:00
{
2018-12-20 13:50:05 -06:00
$this->ruleRepository = app(RuleRepositoryInterface::class);
2018-06-23 23:51:22 -05:00
}
/**
* Transform the rule.
*
* @param Rule $rule
*
* @return array
2019-08-25 00:40:14 -05:00
* @throws FireflyException
2018-06-23 23:51:22 -05:00
*/
public function transform(Rule $rule): array
{
2018-12-20 13:50:05 -06:00
$this->ruleRepository->setUser($rule->user);
2020-10-23 12:11:25 -05:00
return [
2021-03-11 23:11:34 -06:00
'id' => (string)$rule->id,
2018-06-23 23:51:22 -05:00
'created_at' => $rule->created_at->toAtomString(),
2018-12-12 13:30:25 -06:00
'updated_at' => $rule->updated_at->toAtomString(),
2021-03-21 03:15:40 -05:00
'rule_group_id' => (string)$rule->rule_group_id,
2018-06-23 23:51:22 -05:00
'title' => $rule->title,
2018-12-12 13:30:25 -06:00
'description' => $rule->description,
2018-06-23 23:51:22 -05:00
'order' => (int)$rule->order,
'active' => $rule->active,
'strict' => $rule->strict,
2018-12-12 13:30:25 -06:00
'stop_processing' => $rule->stop_processing,
2019-08-29 10:53:25 -05:00
'trigger' => $this->getRuleTrigger($rule),
2018-12-07 08:36:04 -06:00
'triggers' => $this->triggers($rule),
'actions' => $this->actions($rule),
2018-06-23 23:51:22 -05:00
'links' => [
[
'rel' => 'self',
'uri' => '/rules/' . $rule->id,
],
],
];
}
2018-12-07 08:36:04 -06:00
/**
* @param Rule $rule
*
* @return string
* @throws FireflyException
*/
2019-08-25 00:40:14 -05:00
private function getRuleTrigger(Rule $rule): string
{
$moment = null;
$triggers = $this->ruleRepository->getRuleTriggers($rule);
/** @var RuleTrigger $ruleTrigger */
foreach ($triggers as $ruleTrigger) {
if ('user_action' === $ruleTrigger->trigger_type) {
$moment = $ruleTrigger->trigger_value;
}
}
if (null === $moment) {
throw new FireflyException(sprintf('Rule #%d has no valid trigger moment. Edit it in the Firefly III user interface to correct this.', $rule->id));
}
return $moment;
}
2018-12-07 08:36:04 -06:00
/**
* @param Rule $rule
*
* @return array
*/
private function triggers(Rule $rule): array
{
$result = [];
2018-12-20 13:50:05 -06:00
$triggers = $this->ruleRepository->getRuleTriggers($rule);
2018-12-07 08:36:04 -06:00
/** @var RuleTrigger $ruleTrigger */
foreach ($triggers as $ruleTrigger) {
if ('user_action' === $ruleTrigger->trigger_type) {
continue;
}
2018-12-07 08:36:04 -06:00
$result[] = [
2021-03-06 05:45:49 -06:00
'id' => (string)$ruleTrigger->id,
2018-12-07 08:36:04 -06:00
'created_at' => $ruleTrigger->created_at->toAtomString(),
2018-12-12 13:30:25 -06:00
'updated_at' => $ruleTrigger->updated_at->toAtomString(),
2018-12-07 08:36:04 -06:00
'type' => $ruleTrigger->trigger_type,
'value' => $ruleTrigger->trigger_value,
'order' => $ruleTrigger->order,
'active' => $ruleTrigger->active,
'stop_processing' => $ruleTrigger->stop_processing,
];
}
return $result;
}
2021-03-21 03:15:40 -05:00
/**
* @param Rule $rule
*
* @return array
*/
private function actions(Rule $rule): array
{
$result = [];
$actions = $this->ruleRepository->getRuleActions($rule);
/** @var RuleAction $ruleAction */
foreach ($actions as $ruleAction) {
$result[] = [
'id' => (string)$ruleAction->id,
'created_at' => $ruleAction->created_at->toAtomString(),
'updated_at' => $ruleAction->updated_at->toAtomString(),
'type' => $ruleAction->action_type,
'value' => $ruleAction->action_value,
'order' => $ruleAction->order,
'active' => $ruleAction->active,
'stop_processing' => $ruleAction->stop_processing,
];
}
return $result;
}
}