Skip to content

Instantly share code, notes, and snippets.

@fesor
Last active August 29, 2015 14:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fesor/ba2892f24e63204eac24 to your computer and use it in GitHub Desktop.
Save fesor/ba2892f24e63204eac24 to your computer and use it in GitHub Desktop.
<?php
namespace WT\ServiceBundle\Service;
use Doctrine\ORM\EntityManager;
use WT\CommonBundle\DateRange;
use WT\BackendBundle\Entity\Service\AccountSource;
use WT\ServiceBundle\AccountContextFactory;
use WT\ServiceBundle\Entity\Transaction;
use WT\ServiceBundle\Transactions\Collection\AbstractTransactionsCollection;
use WT\ServiceBundle\Transactions\Collection\TransactionsCollection;
class SourceUpdater
{
/**
* @var SourceFactory
*/
private $sourceFactory;
/**
* @var AccountContextFactory
*/
private $accountContextFactory;
/**
* @var TransactionRepository
*/
private $transactionsRepository;
public function __construct(TransactionRepository $transactionsRepository, SourceFactory $sourceFactory, AccountContextFactory $accountContextFactory)
{
$this->transactionsRepository = $transactionsRepository;
$this->sourceFactory = $sourceFactory;
$this->accountContextFactory = $accountContextFactory;
}
/**
* @param DateRange $range
* @param AccountSource $accountSource
* @return TransactionsCollection
*/
private function loadTransactions(DateRange $range, AccountSource $accountSource)
{
$context = $this->accountContextFactory->getContext($accountSource->getAccount());
$sourceObject = $this->sourceFactory->createSource($accountSource->getSourceType());
$sourceParams = $accountSource->getParams();
//$sourceParams = $this->container->get('wt.extractor_params')->extract($sourceParams);
$transactions = $sourceObject->loadTransactions($range, $context, $sourceParams);
foreach ($transactions as $transaction) {
/** @var Transaction $transaction */
$transaction->setSource($accountSource);
}
$this->accountContextFactory->saveContext($context);
return $transactions;
}
public function updateTransactions(DateRange $range, AccountSource $accountSource)
{
$transactions = $this->loadTransactions($range, $accountSource);
$updatedTransactions = $this->insertTransactions($transactions, $range, $accountSource);
$this->transactionsRepository->save($updatedTransaction);
}
private function insertTransactions(TransactionsCollection $newTransactions, DateRange $range, AccountSource $source)
{
$oldTransactions = $this->transactionsRepository->getBySource($source, $range, false);
$insertCollection = $newTransactions->merge($oldTransactions);
if ($accountSource->getSourceType()->isStatic()) {
return $insertCollection;
}
$newKeys = $newTransactions->getKeys();
$oldKeys = $oldTransactions->getKeys();
// Delete
$deleteKeys = array_diff($oldKeys, $newKeys);
foreach ($deleteKeys as $key) {
$item = $oldTransactions->get($key);
$this->transactionsRepository->removeTransaction($item);
}
return $insertCollection;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment