Last active
August 29, 2015 14:14
-
-
Save fesor/ba2892f24e63204eac24 to your computer and use it in GitHub Desktop.
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
<?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