2020-12-02 06:54:13 +01:00
< ? php
2021-08-10 19:31:55 +02:00
2020-12-02 06:54:13 +01:00
/*
2021-08-10 19:31:55 +02:00
* StandardMessageGenerator . php
* Copyright ( c ) 2021 james @ firefly - iii . org
2020-12-02 06:54:13 +01:00
*
* 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 .
*
* This program is distributed in the hope that it will be useful ,
* 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 .
*
* 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 />.
*/
2021-08-10 19:31:55 +02:00
declare ( strict_types = 1 );
2020-12-02 06:54:13 +01:00
namespace FireflyIII\Generator\Webhook ;
2022-09-17 16:54:13 +02:00
use FireflyIII\Enums\WebhookResponse ;
use FireflyIII\Enums\WebhookTrigger ;
2020-12-02 06:54:13 +01:00
use FireflyIII\Exceptions\FireflyException ;
use FireflyIII\Models\Transaction ;
use FireflyIII\Models\TransactionGroup ;
use FireflyIII\Models\TransactionJournal ;
use FireflyIII\Models\Webhook ;
use FireflyIII\Models\WebhookMessage ;
use FireflyIII\Transformers\AccountTransformer ;
use FireflyIII\Transformers\TransactionGroupTransformer ;
use FireflyIII\User ;
2020-12-05 06:47:16 +01:00
use Illuminate\Database\Eloquent\Model ;
2020-12-02 06:54:13 +01:00
use Illuminate\Support\Collection ;
2022-12-29 19:41:57 +01:00
use JsonException ;
2020-12-02 06:54:13 +01:00
use Log ;
use Ramsey\Uuid\Uuid ;
use Symfony\Component\HttpFoundation\ParameterBag ;
/**
2020-12-04 20:19:52 +01:00
* Class StandardMessageGenerator
2020-12-02 06:54:13 +01:00
*/
2020-12-04 20:19:52 +01:00
class StandardMessageGenerator implements MessageGeneratorInterface
2020-12-02 06:54:13 +01:00
{
2020-12-05 06:47:16 +01:00
private Collection $objects ;
2020-12-02 06:54:13 +01:00
private int $trigger ;
2021-03-21 09:15:40 +01:00
private User $user ;
private int $version = 0 ;
2020-12-02 06:54:13 +01:00
private Collection $webhooks ;
/**
*
*/
public function generateMessages () : void
{
2021-01-15 21:01:53 +01:00
Log :: debug ( __METHOD__ );
2020-12-05 06:47:16 +01:00
// get the webhooks:
2020-12-02 06:54:13 +01:00
$this -> webhooks = $this -> getWebhooks ();
2020-12-05 06:47:16 +01:00
// do some debugging
Log :: debug (
sprintf ( 'StandardMessageGenerator will generate messages for %d object(s) and %d webhook(s).' , $this -> objects -> count (), $this -> webhooks -> count ())
);
2020-12-02 06:54:13 +01:00
$this -> run ();
}
/**
* @ return Collection
*/
private function getWebhooks () : Collection
{
2021-05-01 20:04:58 +02:00
return $this -> user -> webhooks () -> where ( 'active' , true ) -> where ( 'trigger' , $this -> trigger ) -> get ([ 'webhooks.*' ]);
2020-12-02 06:54:13 +01:00
}
2020-12-04 20:19:52 +01:00
/**
2020-12-05 06:47:16 +01:00
*
2020-12-04 20:19:52 +01:00
*/
2020-12-02 06:54:13 +01:00
private function run () : void
{
2020-12-05 06:47:16 +01:00
Log :: debug ( 'Now in StandardMessageGenerator::run' );
2020-12-02 06:54:13 +01:00
/** @var Webhook $webhook */
foreach ( $this -> webhooks as $webhook ) {
$this -> runWebhook ( $webhook );
}
2021-01-15 21:01:53 +01:00
Log :: debug ( 'Done with StandardMessageGenerator::run' );
2020-12-02 06:54:13 +01:00
}
/**
2022-12-29 19:41:57 +01:00
* @ param Webhook $webhook
2020-12-02 06:54:13 +01:00
*/
private function runWebhook ( Webhook $webhook ) : void
{
2020-12-05 06:47:16 +01:00
Log :: debug ( sprintf ( 'Now in runWebhook(#%d)' , $webhook -> id ));
/** @var Model $object */
foreach ( $this -> objects as $object ) {
$this -> generateMessage ( $webhook , $object );
2020-12-02 06:54:13 +01:00
}
}
/**
2022-12-29 19:41:57 +01:00
* @ param Webhook $webhook
* @ param Model $model
2022-03-29 15:10:05 +02:00
* @ throws FireflyException
2022-12-29 19:41:57 +01:00
* @ throws JsonException
2020-12-02 06:54:13 +01:00
*/
2020-12-05 06:47:16 +01:00
private function generateMessage ( Webhook $webhook , Model $model ) : void
2020-12-02 06:54:13 +01:00
{
2020-12-05 06:47:16 +01:00
$class = get_class ( $model );
2022-12-31 06:57:05 +01:00
// Line is ignored because all of Firefly III's Models have an id property.
Log :: debug ( sprintf ( 'Now in generateMessage(#%d, %s#%d)' , $webhook -> id , $class , $model -> id )); // @phpstan-ignore-line
2020-12-02 06:54:13 +01:00
2020-12-05 06:47:16 +01:00
$uuid = Uuid :: uuid4 ();
$basicMessage = [
2020-12-02 06:54:13 +01:00
'uuid' => $uuid -> toString (),
2020-12-05 06:47:16 +01:00
'user_id' => 0 ,
2022-09-17 16:54:13 +02:00
'trigger' => WebhookTrigger :: from ( $webhook -> trigger ) -> name ,
'response' => WebhookResponse :: from ( $webhook -> response ) -> name ,
2020-12-05 06:47:16 +01:00
'url' => $webhook -> url ,
'version' => sprintf ( 'v%d' , $this -> getVersion ()),
2020-12-02 06:54:13 +01:00
'content' => [],
];
2020-12-05 06:47:16 +01:00
// depends on the model how user_id is set:
switch ( $class ) {
default :
2022-12-31 06:57:05 +01:00
// Line is ignored because all of Firefly III's Models have an id property.
Log :: error ( sprintf ( 'Webhook #%d was given %s#%d to deal with but can\'t extract user ID from it.' , $webhook -> id , $class , $model -> id )); // @phpstan-ignore-line
2020-12-05 06:47:16 +01:00
return ;
case TransactionGroup :: class :
/** @var TransactionGroup $model */
$basicMessage [ 'user_id' ] = $model -> user -> id ;
break ;
}
// then depends on the response what to put in the message:
2020-12-02 06:54:13 +01:00
switch ( $webhook -> response ) {
default :
2020-12-04 20:19:52 +01:00
Log :: error (
sprintf ( 'The response code for webhook #%d is "%d" and the message generator cant handle it. Soft fail.' , $webhook -> id , $webhook -> response )
);
return ;
2022-09-17 16:54:13 +02:00
case WebhookResponse :: NONE -> value :
2020-12-05 06:47:16 +01:00
$basicMessage [ 'content' ] = [];
2020-12-04 20:19:52 +01:00
break ;
2022-09-17 16:54:13 +02:00
case WebhookResponse :: TRANSACTIONS -> value :
2022-10-30 14:24:10 +01:00
$transformer = new TransactionGroupTransformer ();
2020-12-04 20:19:52 +01:00
try {
2020-12-05 06:47:16 +01:00
$basicMessage [ 'content' ] = $transformer -> transformObject ( $model );
2020-12-04 20:19:52 +01:00
} catch ( FireflyException $e ) {
2020-12-05 06:47:16 +01:00
Log :: error (
sprintf ( 'The transformer could not include the requested transaction group for webhook #%d: %s' , $webhook -> id , $e -> getMessage ())
);
return ;
2020-12-04 20:19:52 +01:00
}
2020-12-02 06:54:13 +01:00
break ;
2022-09-17 16:54:13 +02:00
case WebhookResponse :: ACCOUNTS -> value :
2020-12-05 06:47:16 +01:00
$accounts = $this -> collectAccounts ( $model );
2020-12-02 06:54:13 +01:00
foreach ( $accounts as $account ) {
2022-10-30 14:24:10 +01:00
$transformer = new AccountTransformer ();
$transformer -> setParameters ( new ParameterBag ());
2020-12-05 06:47:16 +01:00
$basicMessage [ 'content' ][] = $transformer -> transform ( $account );
2020-12-02 06:54:13 +01:00
}
}
2020-12-05 06:47:16 +01:00
$this -> storeMessage ( $webhook , $basicMessage );
2020-12-02 06:54:13 +01:00
}
2022-03-29 14:58:06 +02:00
/**
* @ inheritDoc
*/
public function getVersion () : int
{
return $this -> version ;
}
2020-12-02 06:54:13 +01:00
/**
2022-12-29 19:41:57 +01:00
* @ param TransactionGroup $transactionGroup
2020-12-02 06:54:13 +01:00
*
* @ return Collection
*/
private function collectAccounts ( TransactionGroup $transactionGroup ) : Collection
{
2022-10-30 14:24:10 +01:00
$accounts = new Collection ();
2020-12-02 06:54:13 +01:00
/** @var TransactionJournal $journal */
foreach ( $transactionGroup -> transactionJournals as $journal ) {
/** @var Transaction $transaction */
foreach ( $journal -> transactions as $transaction ) {
$accounts -> push ( $transaction -> account );
}
}
return $accounts -> unique ();
}
/**
2022-12-29 19:41:57 +01:00
* @ param Webhook $webhook
* @ param array $message
2020-12-04 20:19:52 +01:00
*
2022-03-29 15:10:05 +02:00
* @ return void
2020-12-02 06:54:13 +01:00
*/
2022-03-29 15:10:05 +02:00
private function storeMessage ( Webhook $webhook , array $message ) : void
2020-12-02 06:54:13 +01:00
{
2022-10-30 14:24:10 +01:00
$webhookMessage = new WebhookMessage ();
2020-12-02 06:54:13 +01:00
$webhookMessage -> webhook () -> associate ( $webhook );
$webhookMessage -> sent = false ;
$webhookMessage -> errored = false ;
$webhookMessage -> uuid = $message [ 'uuid' ];
$webhookMessage -> message = $message ;
$webhookMessage -> save ();
2020-12-05 06:47:16 +01:00
Log :: debug ( sprintf ( 'Stored new webhook message #%d' , $webhookMessage -> id ));
2020-12-02 06:54:13 +01:00
}
2022-03-29 14:58:06 +02:00
/**
2022-12-29 19:41:57 +01:00
* @ param Collection $objects
2022-03-29 14:58:06 +02:00
*/
public function setObjects ( Collection $objects ) : void
{
$this -> objects = $objects ;
}
/**
2022-12-29 19:41:57 +01:00
* @ param int $trigger
2022-03-29 14:58:06 +02:00
*/
public function setTrigger ( int $trigger ) : void
{
$this -> trigger = $trigger ;
}
/**
2022-12-29 19:41:57 +01:00
* @ param User $user
2022-03-29 14:58:06 +02:00
*/
public function setUser ( User $user ) : void
{
$this -> user = $user ;
}
2020-12-22 05:35:06 +01:00
}