Completed
Pull Request — master (#89)
by
unknown
62:49
created

HistoryManager::getHistory()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 8
ccs 0
cts 6
cp 0
rs 9.4285
cc 1
eloc 5
nc 1
nop 1
crap 2
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\Translation;
13
14
use ONGR\ElasticsearchBundle\Result\DocumentIterator;
15
use ONGR\ElasticsearchDSL\Query\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
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 message history.
41
     *
42
     * @param string $id
43
     *
44
     * @return DocumentIterator
45
     */
46
    public function getHistory($id)
47
    {
48
        $search = $this->repository->createSearch();
49
        $search->addFilter(new TermQuery('translation', $id));
50
        $search->addSort(new FieldSort('created_at', FieldSort::DESC));
51
52
        return $this->repository->findDocuments($search);
53
    }
54
55
    /**
56
     * @param $id
57
     * @return array
58
     */
59
    public function getOrderedHistory($id)
60
    {
61
        $ordered = [];
62
        $histories = $this->getHistory($id);
63
64
        /** @var History $history */
65
        foreach ($histories as $history) {
66
            $ordered[$history->getLocale()][] = $history;
67
        }
68
69
        return $ordered;
70
    }
71
72
    /**
73
     * @param Message $message
74
     * @param $id
75
     * @param $locale
76
     */
77
    public function addHistory(Message $message, $id, $locale)
78
    {
79
        $history = new History();
80
        $history->setLocale($locale);
81
        $history->setTranslation($id);
82
        $history->setMessage($message->getMessage());
83
        $history->setId(sha1($id . $message->getMessage()));
84
        $history->setUpdatedAt($message->getUpdatedAt());
85
86
        $this->repository->getManager()->persist($history);
87
    }
88
}
89