Completed
Push — master ( 92ccbf...b2415b )
by Simonas
63:39
created

HistoryManager::get()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 16
rs 9.4285
cc 2
eloc 10
nc 2
nop 1
1
<?php
2
3
/*
4
 * This file is part of the ONGR package.
5
 *
6
 * (c) NFQ Technologies UAB <[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 ONGR\TranslationsBundle\Service;
13
14
use ONGR\ElasticsearchDSL\Query\TermQuery;
15
use ONGR\ElasticsearchDSL\Sort\FieldSort;
16
use ONGR\ElasticsearchBundle\Service\Repository;
17
use ONGR\TranslationsBundle\Document\History;
18
use ONGR\TranslationsBundle\Document\Message;
19
use ONGR\TranslationsBundle\Document\Translation;
20
21
/**
22
 * History handler.
23
 */
24
class HistoryManager
25
{
26
    /**
27
     * @var Repository
28
     */
29
    private $repository;
30
31
    /**
32
     * @param Repository $repository
33
     */
34
    public function __construct(Repository $repository)
35
    {
36
        $this->repository = $repository;
37
    }
38
39
    /**
40
     * Returns an array of history objects grouped by locales
41
     *
42
     * @param Translation $translation
43
     *
44
     * @return array
45
     */
46
    public function get(Translation $translation)
47
    {
48
        $ordered = [];
49
        $search = $this->repository->createSearch();
50
        $search->addFilter(new TermQuery('key', $translation->getKey()));
51
        $search->addFilter(new TermQuery('domain', $translation->getDomain()));
52
        $search->addSort(new FieldSort('created_at', FieldSort::DESC));
53
        $histories = $this->repository->findDocuments($search);
54
55
        /** @var History $history */
56
        foreach ($histories as $history) {
57
            $ordered[$history->getLocale()][] = $history;
58
        }
59
60
        return $ordered;
61
    }
62
63
    /**
64
     * @param Message $message
65
     * @param Translation $translation
66
     */
67
    public function add(Message $message, Translation $translation)
68
    {
69
        $history = new History();
70
        $history->setLocale($message->getLocale());
71
        $history->setKey($translation->getKey());
72
        $history->setDomain($translation->getDomain());
73
        $history->setMessage($message->getMessage());
74
        $history->setUpdatedAt($message->getUpdatedAt());
75
76
        $this->repository->getManager()->persist($history);
77
    }
78
}
79