fork download
  1. <?php
  2.  
  3. namespace WT\ServiceBundle\Service;
  4.  
  5.  
  6. use Doctrine\ORM\EntityManager;
  7. use WT\CommonBundle\DateRange;
  8. use WT\BackendBundle\Entity\Service\AccountSource;
  9. use WT\ServiceBundle\AccountContextFactory;
  10. use WT\ServiceBundle\Entity\Transaction;
  11. use WT\ServiceBundle\Transactions\Collection\AbstractTransactionsCollection;
  12. use WT\ServiceBundle\Transactions\Collection\TransactionsCollection;
  13.  
  14. class SourceUpdater
  15. {
  16.  
  17.  
  18. private $entityManager;
  19.  
  20. /**
  21.   * @var SourceFactory
  22.   */
  23. private $sourceFactory;
  24.  
  25. /**
  26.   * @var AccountContextFactory
  27.   */
  28. private $accountContextFactory;
  29.  
  30. public function __construct(EntityManager $em, SourceFactory $sourceFactory, AccountContextFactory $accountContextFactory)
  31. {
  32. $this->entityManager = $em;
  33.  
  34. $this->sourceFactory = $sourceFactory;
  35. $this->accountContextFactory = $accountContextFactory;
  36.  
  37. }
  38.  
  39. /**
  40.   * @param DateRange $range
  41.   * @param AccountSource $accountSource
  42.   * @return TransactionsCollection
  43.   */
  44. private function loadTransactions(DateRange $range, AccountSource $accountSource)
  45. {
  46. $context = $this->accountContextFactory->getContext($accountSource->getAccount());
  47. $sourceObject = $this->sourceFactory->createSource($accountSource->getSourceType());
  48.  
  49. $sourceParams = $accountSource->getParams();
  50. //$sourceParams = $this->container->get('wt.extractor_params')->extract($sourceParams);
  51.  
  52. $transactions = $sourceObject->loadTransactions($range, $context, $sourceParams);
  53. foreach ($transactions as $transaction) {
  54. /** @var Transaction $transaction */
  55. $transaction->setSource($accountSource);
  56. }
  57.  
  58. $this->accountContextFactory->saveContext($context);
  59.  
  60. return $transactions;
  61. }
  62.  
  63. public function updateTransactions(DateRange $range, AccountSource $accountSource)
  64. {
  65. $transactions = $this->loadTransactions($range, $accountSource);
  66.  
  67. //dump($transactions);
  68.  
  69. $sourceType = $accountSource->getSourceType();
  70. if ($sourceType->isStatistic()) {
  71. $updatedTransactions = $this->insertStatisticTransactions($transactions, $range, $accountSource);
  72. } else {
  73. $updatedTransactions = $this->insertTransactions($transactions, $range, $accountSource);
  74. }
  75.  
  76. // set rules
  77. //dump($updatedTransactions);
  78.  
  79.  
  80.  
  81. }
  82.  
  83.  
  84. private function mergeTransactions(TransactionsCollection $newTransactions, TransactionsCollection $oldTransactions)
  85. {
  86. $transactionsRepository = $this->entityManager->getRepository('WTServiceBundle:Transaction');
  87.  
  88. $newKeys = $newTransactions->getKeys();
  89. $oldKeys = $oldTransactions->getKeys();
  90.  
  91. // Insert
  92. $insertKeys = array_diff($newKeys, $oldKeys);
  93. $insertCollection = $newTransactions->extract($insertKeys);
  94. $transactionsRepository->insertCollection($insertCollection);
  95.  
  96. // Update
  97. $updateKeys = array_intersect($newKeys, $oldKeys);
  98. foreach ($updateKeys as $key) {
  99. $oldItem = $oldTransactions->get($key);
  100. $newItem = $newTransactions->get($key);
  101.  
  102. if ($oldItem->hash() != $newItem->hash()) {
  103. // update item
  104. $oldItem->load($newItem);
  105. $insertCollection->insert($oldItem);
  106. }
  107. }
  108.  
  109. return $insertCollection;
  110. }
  111.  
  112.  
  113. private function insertTransactions(TransactionsCollection $newTransactions, DateRange $range, AccountSource $source)
  114. {
  115. $transactionsRepository = $this->entityManager->getRepository('WTServiceBundle:Transaction');
  116. $oldTransactions = $transactionsRepository->getBySource($source, $range, false);
  117.  
  118.  
  119. $insertCollection = $this->mergeTransactions($newTransactions, $oldTransactions);
  120.  
  121. $newKeys = $newTransactions->getKeys();
  122. $oldKeys = $oldTransactions->getKeys();
  123.  
  124. // Delete
  125. $deleteKeys = array_diff($oldKeys, $newKeys);
  126. foreach ($deleteKeys as $key) {
  127. $item = $oldTransactions->get($key);
  128. $this->entityManager->remove($item);
  129. }
  130.  
  131. $this->entityManager->flush();
  132.  
  133. return $insertCollection;
  134. }
  135.  
  136. private function insertStatisticTransactions(TransactionsCollection $newTransactions, DateRange $range, AccountSource $source)
  137. {
  138. $transactionsRepository = $this->entityManager->getRepository('WTServiceBundle:Transaction');
  139. $oldTransactions = $transactionsRepository->getBySource($source, $range);
  140.  
  141. $insertCollection = $this->mergeTransactions($newTransactions, $oldTransactions);
  142.  
  143. $this->entityManager->flush();
  144.  
  145. return $insertCollection;
  146. }
  147.  
  148.  
  149. }
Success #stdin #stdout 0.02s 24400KB
stdin
Standard input is empty
stdout
Standard output is empty