TranslationManager::findTranslationBy()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
ccs 2
cts 2
cp 1
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
/*
4
 * This file is part of the AsmTranslationLoaderBundle package.
5
 *
6
 * (c) Marc Aschmann <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Asm\TranslationLoaderBundle\Doctrine;
13
14
use Asm\TranslationLoaderBundle\Event\TranslationEvent;
15
use Asm\TranslationLoaderBundle\Model\TranslationInterface;
16
use Asm\TranslationLoaderBundle\Model\TranslationManager as BaseTranslationManager;
17
use Doctrine\Common\Persistence\ObjectManager;
18
use Doctrine\Common\Persistence\ObjectRepository;
19
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
20
21
/**
22
 * TranslationManager implementation supporting Doctrine.
23
 *
24
 * @author Christian Flothmann <[email protected]>
25
 */
26
class TranslationManager extends BaseTranslationManager
27
{
28
    /**
29
     * @var ObjectManager
30
     */
31
    private $objectManager;
32
33
    /**
34
     * @var ObjectRepository
35
     */
36
    private $repository;
37
38
    /**
39
     * @param ObjectManager $objectManager Object manager for translation entities
40
     * @param string $class Translation model class name
41
     * @param EventDispatcherInterface $eventDispatcher Event dispatcher used to propagate new, modified
42
     *                                                  and removed translations
43
     */
44 10
    public function __construct(ObjectManager $objectManager, $class, EventDispatcherInterface $eventDispatcher)
45
    {
46 10
        parent::__construct($class, $eventDispatcher);
47
48 10
        $this->objectManager = $objectManager;
49 10
        $this->repository    = $objectManager->getRepository($class);
50 10
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55 1
    public function findTranslationBy(array $criteria)
56
    {
57 1
        return $this->repository->findOneBy($criteria);
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63 3
    public function findTranslationsBy(array $criteria)
64
    {
65 3
        return $this->repository->findBy($criteria);
66
    }
67
68
    /**
69
     * {@inheritdoc}
70
     */
71 3
    public function updateTranslation(TranslationInterface $translation)
72
    {
73 3
        $translation->setDateUpdated(new \DateTime());
74
75 3
        if ($this->objectManager->contains($translation)) {
76 1
            $eventName = TranslationEvent::POST_UPDATE;
77 1
        } else {
78 2
            $eventName = TranslationEvent::POST_PERSIST;
79
        }
80
81 3
        $this->objectManager->persist($translation);
82 3
        $this->objectManager->flush();
83
84 3
        $this->eventDispatcher->dispatch($eventName, new TranslationEvent($translation));
85 3
    }
86
87
    /**
88
     * {@inheritdoc}
89
     */
90 2
    public function removeTranslation(TranslationInterface $translation)
91
    {
92 2
        $this->objectManager->remove($translation);
93 2
        $this->objectManager->flush();
94
95 2
        $this->eventDispatcher->dispatch(TranslationEvent::POST_REMOVE, new TranslationEvent($translation));
96 2
    }
97
98
    /**
99
     * {@inheritdoc}
100
     */
101
    public function findTranslationFreshness($timestamp)
102
    {
103
        return $this->repository->findTranslationFreshness($timestamp);
104
    }
105
106
    /**
107
     * {@inheritdoc}
108
     */
109
    public function getTranslationList(array $criteria)
110
    {
111
        return $this->repository->getTranslationList($criteria);
112
    }
113
}
114