Completed
Pull Request — master (#97)
by Simonas
116:39 queued 51:32
created

HistoryManager::getUnorderedHistory()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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