Skip to content

Commit

Permalink
Add matchfunding transactions listener
Browse files Browse the repository at this point in the history
  • Loading branch information
subiabre committed Dec 19, 2024
1 parent 54b45b3 commit ae098d7
Showing 1 changed file with 56 additions and 0 deletions.
56 changes: 56 additions & 0 deletions src/EventListener/MatchfundingTransactionsListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace App\EventListener;

use App\Entity\Accounting\Transaction;
use App\Entity\Matchfunding\MatchSubmissionStatus;
use App\Entity\Project\Project;
use App\Matchfunding\MatchStrategy\MatchStrategyLocator;
use Doctrine\Bundle\DoctrineBundle\Attribute\AsEntityListener;
use Doctrine\ORM\Event\PostPersistEventArgs;
use Doctrine\ORM\Events;

#[AsEntityListener(
event: Events::postPersist,
method: 'processTransaction',
entity: Transaction::class
)]
final class MatchfundingTransactionsListener
{
public function __construct(
private MatchStrategyLocator $matchStrategyLocator
) {}

/**
* Generates an income statement for User-received Transactions.
*/
public function processTransaction(
Transaction $transaction,
PostPersistEventArgs $event,
) {
$target = $transaction->getTarget()->getOwner();

if (!$target instanceof Project) {
return;
}

$submissions = $target->getMatchSubmissions();

foreach ($submissions as $submission) {
if ($submission->getStatus() !== MatchSubmissionStatus::Accepted) {
continue;
}

$call = $submission->getMatchCall();
$strategy = $this->matchStrategyLocator->getForCall($call);
$match = $strategy->match($transaction);

if ($match->getId() !== null) {
return;
}

$event->getObjectManager()->persist($match);
$event->getObjectManager()->flush();
}
}
}

0 comments on commit ae098d7

Please sign in to comment.