Completed
Push — master ( d7e919...298f7d )
by Simonas
64:16
created

HistoryManager::getHistory()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 12
rs 9.4285
cc 2
eloc 6
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\ElasticsearchBundle\Result\DocumentIterator;
15
use ONGR\ElasticsearchDSL\Query\TermLevel\TermQuery;
16
use ONGR\ElasticsearchDSL\Sort\FieldSort;
17
use ONGR\ElasticsearchBundle\Service\Repository;
18
use ONGR\TranslationsBundle\Document\History;
19
use ONGR\TranslationsBundle\Document\Message;
20
use ONGR\TranslationsBundle\Document\Translation;
21
22
/**
23
 * History handler.
24
 */
25
class HistoryManager
26
{
27
    /**
28
     * @var Repository
29
     */
30
    private $repository;
31
32
    /**
33
     * @param Repository $repository
34
     */
35
    public function __construct(Repository $repository)
36
    {
37
        $this->repository = $repository;
38
    }
39
40
    /**
41
     * Returns an array of history objects grouped by locales
42
     *
43
     * @param Translation $translation
44
     *
45
     * @return array
46
     */
47
    public function getHistory(Translation $translation)
48
    {
49
        $ordered = [];
50
        $histories = $this->getUnorderedHistory($translation);
51
52
        /** @var History $history */
53
        foreach ($histories as $history) {
54
            $ordered[$history->getLocale()][] = $history;
55
        }
56
57
        return $ordered;
58
    }
59
60
    /**
61
     * @param Message $message
62
     * @param Translation $translation
63
     */
64
    public function addHistory(Message $message, Translation $translation)
65
    {
66
        $history = new History();
67
        $history->setLocale($message->getLocale());
68
        $history->setKey($translation->getKey());
69
        $history->setDomain($translation->getDomain());
70
        $history->setMessage($message->getMessage());
71
        $history->setUpdatedAt($message->getUpdatedAt());
72
73
        $this->repository->getManager()->persist($history);
74
    }
75
76
    /**
77
     * Returns message history.
78
     *
79
     * @param Translation $translation
80
     *
81
     * @return DocumentIterator
82
     */
83
    private function getUnorderedHistory(Translation $translation)
84
    {
85
        $search = $this->repository->createSearch();
86
        $search->addQuery(new TermQuery('key', $translation->getKey()));
87
        $search->addQuery(new TermQuery('domain', $translation->getDomain()));
88
        $search->addSort(new FieldSort('created_at', FieldSort::DESC));
89
90
        return $this->repository->findDocuments($search);
91
    }
92
}
93