-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add matchfunding transactions listener
- Loading branch information
Showing
1 changed file
with
56 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |