<commit_before><?php
namespace Dizda\BankManager\CoreBundle\EventListener;

use Dizda\BankManager\CoreBundle\Event\AccountEvent;
use Doctrine\ODM\MongoDB\DocumentManager;
use JMS\DiExtraBundle\Annotation\Service;
use JMS\DiExtraBundle\Annotation\Observe;
use JMS\DiExtraBundle\Annotation\Tag;
use JMS\DiExtraBundle\Annotation as DI;
use Dizda\BankManager\CoreBundle\Document\BalanceHistory;

use Dizda\BankManager\CoreBundle\Document\Transaction;
use Dizda\BankManager\UserBundle\Document\User;

/**
 * @Service("dizda.bank.listener.account");
 */
class AccountListener
{
    protected $dm;
    protected $statsFetched = [];
    private $push_user;
    private $push_token;

    /**
     * @DI\InjectParams({
     *     "dm"         = @DI\Inject("doctrine.odm.mongodb.document_manager"),
     *     "push_user"    = @DI\Inject("%pushover_user%"),
     *     "push_token"   = @DI\Inject("%pushover_token%")
     * })
     */
    public function __construct(DocumentManager $dm, $push_user, $push_token)
    {
        $this->dm = $dm;
        $this->push_user  = $push_user;
        $this->push_token = $push_token;
    }

    
    /**
     * Save accounts array into MongoDB
     * @Observe("dizda.bank.account.update")
     */
    public function onAccountEvent(AccountEvent $event)
    {
        foreach($event->getAccounts() as $account)
        {
            $exist = $this->dm->find('DizdaBankManagerCoreBundle:Account', $account->getIban());

            if($exist)
            {
                $balance = $exist->getBalanceHistory()->last();

                if(!$balance)
                {
                    $balance = new BalanceHistory();
                    $balance->setBalance($account->getBalance());

                    $exist->addBalanceHistory($balance);
                    $this->dm->persist($exist);
                }else{

                    if($balance->getBalance() != $account->getBalance())
                    {
                        $balance = new BalanceHistory();
                        $balance->setBalance($account->getBalance());

                        $exist->setBalance($account->getBalance());
                        $exist->addBalanceHistory($balance);
                        $this->dm->persist($exist);
                    }

                }

            }else{
                $this->dm->persist($account);
            }


        }
        $this->dm->flush();
    }
    
    /**
     * @Observe("dizda.bank.transaction.add")
     */
    public function onTransactionAddEvent(AccountEvent $event)
    {
        foreach($event->getTransactions() as $key => $account)
        {
            $accountEntity = $this->dm->find('Dizda\BankManager\CoreBundle\Document\Account', $key);
            $this->statsFetched[$key]['count'] = 0;
            $this->statsFetched[$key]['added'] = 0;
            
            foreach($account as $transaction)
            {
                
                /* If the transaction is already created, we dont overwrite it */
                if( !$this->dm->find('Dizda\BankManager\CoreBundle\Document\Transaction', $transaction->generateId()) )
                {
                    $transaction->setAccount($accountEntity);
                    //$this->dm->persist($transaction); // PLEASE UNCOMMENT, FOR DEV PURPOSE
                    $this->statsFetched[$key]['added']++;
                    $this->sendAlertEmail($transaction, $event->getUser());
                }
                
                $this->statsFetched[$key]['count']++;
                
            }
        }
        $this->dm->flush();
        <commit_msg>persist uncommented<commit_after><?php
namespace Dizda\BankManager\CoreBundle\EventListener;

use Dizda\BankManager\CoreBundle\Event\AccountEvent;
use Doctrine\ODM\MongoDB\DocumentManager;
use JMS\DiExtraBundle\Annotation\Service;
use JMS\DiExtraBundle\Annotation\Observe;
use JMS\DiExtraBundle\Annotation\Tag;
use JMS\DiExtraBundle\Annotation as DI;
use Dizda\BankManager\CoreBundle\Document\BalanceHistory;

use Dizda\BankManager\CoreBundle\Document\Transaction;
use Dizda\BankManager\UserBundle\Document\User;

/**
 * @Service("dizda.bank.listener.account");
 */
class AccountListener
{
    protected $dm;
    protected $statsFetched = [];
    private $push_user;
    private $push_token;

    /**
     * @DI\InjectParams({
     *     "dm"         = @DI\Inject("doctrine.odm.mongodb.document_manager"),
     *     "push_user"    = @DI\Inject("%pushover_user%"),
     *     "push_token"   = @DI\Inject("%pushover_token%")
     * })
     */
    public function __construct(DocumentManager $dm, $push_user, $push_token)
    {
        $this->dm = $dm;
        $this->push_user  = $push_user;
        $this->push_token = $push_token;
    }

    
    /**
     * Save accounts array into MongoDB
     * @Observe("dizda.bank.account.update")
     */
    public function onAccountEvent(AccountEvent $event)
    {
        foreach($event->getAccounts() as $account)
        {
            $exist = $this->dm->find('DizdaBankManagerCoreBundle:Account', $account->getIban());

            if($exist)
            {
                $balance = $exist->getBalanceHistory()->last();

                if(!$balance)
                {
                    $balance = new BalanceHistory();
                    $balance->setBalance($account->getBalance());

                    $exist->addBalanceHistory($balance);
                    $this->dm->persist($exist);
                }else{

                    if($balance->getBalance() != $account->getBalance())
                    {
                        $balance = new BalanceHistory();
                        $balance->setBalance($account->getBalance());

                        $exist->setBalance($account->getBalance());
                        $exist->addBalanceHistory($balance);
                        $this->dm->persist($exist);
                    }

                }

            }else{
                $this->dm->persist($account);
            }


        }
        $this->dm->flush();
    }
    
    /**
     * @Observe("dizda.bank.transaction.add")
     */
    public function onTransactionAddEvent(AccountEvent $event)
    {
        foreach($event->getTransactions() as $key => $account)
        {
            $accountEntity = $this->dm->find('Dizda\BankManager\CoreBundle\Document\Account', $key);
            $this->statsFetched[$key]['count'] = 0;
            $this->statsFetched[$key]['added'] = 0;
            
            foreach($account as $transaction)
            {
                
                /* If the transaction is already created, we dont overwrite it */
                if( !$this->dm->find('Dizda\BankManager\CoreBundle\Document\Transaction', $transaction->generateId()) )
                {
                    $transaction->setAccount($accountEntity);
                    $this->dm->persist($transaction); // PLEASE UNCOMMENT, FOR DEV PURPOSE
                    $this->statsFetched[$key]['added']++;
                    $this->sendAlertEmail($transaction, $event->getUser());
                }
                
                $this->statsFetched[$key]['count']++;
                
            }
        }
        $this->dm->flush();
        
{'fraction_non_alphanumeric': 0.1420659097202991, 'fraction_numerical': 0.0005538631957906397, 'mean_word_length': 1.8105058365758755, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 93, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2769479', 'n_tokens_mistral': 2034, 'n_tokens_neox': 1975, 'n_words': 273}