Skip to content

Commit

Permalink
Merge pull request #6995 from laboro/fix/BAP-9317_1.9
Browse files Browse the repository at this point in the history
BAP- 9317 Can't delete 100r emails
  • Loading branch information
Denis Voronin committed Mar 4, 2016
2 parents cdd4a27 + f5d31d8 commit cdedfeb
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 11 deletions.
40 changes: 30 additions & 10 deletions src/Oro/Bundle/ImapBundle/Manager/ImapClearManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Oro\Bundle\EmailBundle\Entity\EmailUser;
use Oro\Bundle\ImapBundle\Entity\ImapEmailFolder;
use Oro\Bundle\ImapBundle\Entity\UserEmailOrigin;
use Oro\Bundle\PlatformBundle\EventListener\OptionalListenerInterface;

/**
* Class ImapClearManager
Expand All @@ -27,14 +28,23 @@ class ImapClearManager implements LoggerAwareInterface
*/
protected $em;

/** @var OptionalListenerInterface */
protected $listener;

/**
* Constructor.
*
* @param EntityManager $em
* @param EntityManager $em
* @param OptionalListenerInterface $listener
*/
public function __construct(EntityManager $em)
public function __construct(EntityManager $em, OptionalListenerInterface $listener)
{
$this->em = $em;
/**
* @info This listener should be disabled because it does unnecessary work during removing.
* It updates date in entities which will be deleted in method clearFolder().
*/
$this->listener = $listener;
}

/**
Expand Down Expand Up @@ -93,7 +103,9 @@ protected function getOriginsToClear($originId)
*/
protected function clearOrigin($origin)
{
$folders = $origin->getFolders();
$this->listener->setEnabled(false);

$folders = $origin->getFolders();
$folderRepository = $this->em->getRepository('OroImapBundle:ImapEmailFolder');

foreach ($folders as $folder) {
Expand All @@ -120,6 +132,8 @@ protected function clearOrigin($origin)
}

$this->em->flush();

$this->listener->setEnabled(true);
}

/**
Expand All @@ -128,12 +142,12 @@ protected function clearOrigin($origin)
protected function clearFolder($imapFolder)
{
$folder = $imapFolder->getFolder();
$limit = self::BATCH_SIZE;
$limit = self::BATCH_SIZE;
$offset = 0;
$i = 0;
while ($result =
$this->em->getRepository('OroEmailBundle:EmailUser')
->getEmailUserByFolder($folder, $limit, $offset)->getQuery()->getResult()
$i = 0;

while ($result = $this->em->getRepository('OroEmailBundle:EmailUser')
->getEmailUserByFolder($folder, $limit, $offset)->getQuery()->getResult()
) {
foreach ($result as $emailUser) {
/** @var EmailUser $emailUser */
Expand All @@ -144,7 +158,7 @@ protected function clearFolder($imapFolder)
}

$imapEmails = $this->em->getRepository('OroImapBundle:ImapEmail')->findBy([
'email' => $email,
'email' => $email,
'imapFolder' => $imapFolder
]);
foreach ($imapEmails as $imapEmail) {
Expand All @@ -154,7 +168,6 @@ protected function clearFolder($imapFolder)
}
$this->em->flush();
$this->cleanUp();

}
if ($i > 0) {
$this->logger->info(
Expand All @@ -181,6 +194,13 @@ protected function entitiesToClear()
'Oro\Bundle\EmailBundle\Entity\EmailRecipient',
'Oro\Bundle\ImapBundle\Entity\ImapEmail',
'Oro\Bundle\EmailBundle\Entity\EmailBody',
'Oro\Bundle\ActivityListBundle\Entity\ActivityList',
'Oro\Bundle\EmailBundle\Entity\EmailThread',
'Oro\Bundle\WorkflowBundle\Entity\ProcessTrigger',
'Oro\Bundle\WorkflowBundle\Entity\ProcessDefinition',
'Oro\Bundle\UserBundle\Entity\User',
'Oro\Bundle\OrganizationBundle\Entity\Organization',
'OroEntityProxy\OroEmailBundle\EmailAddressProxy',
];
}

Expand Down
1 change: 1 addition & 0 deletions src/Oro/Bundle/ImapBundle/Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ services:
class: %oro_imap.imap_clear_manager.class%
arguments:
- @doctrine.orm.entity_manager
- @oro_entity.event_listener.entity_modify_created_updated_properties_listener
calls:
- [setLogger, [@logger]]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ protected function setUp()
->disableOriginalConstructor()
->getMock();

$this->manager = new ImapClearManager($this->em);
$listener = $this->getMock('Oro\Bundle\PlatformBundle\EventListener\OptionalListenerInterface');

$this->manager = new ImapClearManager($this->em, $listener);
$this->manager->setLogger($this->logger);
}

Expand Down

0 comments on commit cdedfeb

Please sign in to comment.