aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/Wallabag/ImportBundle/Component/AMPQ/EntryConsumer.php
blob: 7775f01c585d36a82eb97dce46dc0b2d80d9491b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<?php

namespace Wallabag\ImportBundle\Component\AMPQ;

use Doctrine\ORM\EntityManager;
use OldSound\RabbitMqBundle\RabbitMq\ConsumerInterface;
use PhpAmqpLib\Message\AMQPMessage;
use Wallabag\CoreBundle\Helper\ContentProxy;
use Wallabag\CoreBundle\Repository\EntryRepository;

class EntryConsumer implements ConsumerInterface
{
    private $em;
    private $contentProxy;
    private $entryRepository;

    public function __construct(EntityManager $em, EntryRepository $entryRepository, ContentProxy $contentProxy)
    {
        $this->em = $em;
        $this->entryRepository = $entryRepository;
        $this->contentProxy = $contentProxy;
    }

    /**
     * {@inheritdoc}
     */
    public function execute(AMQPMessage $msg)
    {
        $storedEntry = unserialize($msg->body);
        $entry = $this->entryRepository->findByUrlAndUserId($storedEntry['url'], $storedEntry['userId']);
        if ($entry) {
            $entry = $this->contentProxy->updateEntry($entry, $entry->getUrl());
            if ($entry) {
                $this->em->persist($entry);
                $this->em->flush();
            }
        }
    }
}