Completed
Pull Request — master (#80)
by
unknown
91:48 queued 26:46
created

RequestHandler::setTranslation()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
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 Symfony\Component\HttpFoundation\Request;
15
use ONGR\TranslationsBundle\Document\Translation;
16
17
/**
18
* Remakes requests to be compatible with TranslationManager.
19
*/
20
class RequestHandler
21
{
22
    /**
23
     * Translation that is modified by the request
24
     *
25
     * @var Translation
26
     */
27
    private $translation;
28
29
    /**
30
     * @return Translation
31
     */
32
    public function getTranslation()
33
    {
34
        return $this->translation;
35
    }
36
37
    /**
38
     * @param Translation $translation
39
     */
40
    public function setTranslation(Translation $translation)
41
    {
42
        $this->translation = $translation;
43
    }
44
45
    /**
46
     * Remakes a request to have json content
47
     * of a single object. If there is a number of
48
     * locales associated with a request it returns an
49
     * array of new requests
50
     *
51
     * @param Request $request
52
     *
53
     * @return mixed
54
     */
55
    public function remakeRequest(Request $request)
56
    {
57
        $content = [];
58
        $content['name'] = $request->request->get('name');
59
        $content['properties'] = $request->request->get('properties');
60
        $content['id'] = $request->request->get('id');
61
        $content['findBy'] = $request->request->get('findBy');
62
        if ($request->request->has('locales')) {
63
            return $this->turnToArray($request, $content);
64
        }
65
        $content = json_encode($content);
66
        return new Request([], [], [], [], [], [], $content);
67
    }
68
69
    /**
70
     * Turns a request to an array of requests with json content
71
     *
72
     * @param Request $request
73
     * @param array $content
74
     *
75
     * @return array
76
     */
77
    private function turnToArray(Request $request, array $content)
78
    {
79
        $requests = [];
80
        $locales = $request->request->get('locales');
81
        $messages = $request->request->get('messages');
82
        $findBy = $request->request->get('findBy');
83
        foreach ($locales as $locale) {
84
            if (
85
                $messages[$locale] == '' ||
86
                $this->sameMessageExists($locale, $messages[$locale])
87
            ) {
88
                continue;
89
            }
90
            $content['properties']['locale'] = $locale;
91
            $content['properties']['message'] = $messages[$locale];
92
            $content['properties']['status'] = 'dirty';
93
            $content['findBy'] = $findBy[$locale];
94
            $requests[] = new Request([], [], [], [], [], [], json_encode($content));
95
        }
96
        return $requests;
97
    }
98
99
    /**
100
     * Checks if the given message matches the
101
     * message set in the given locale of the translation
102
     *
103
     * @param string $locale
104
     * @param string $message
105
     *
106
     * @return bool
107
     */
108
    private function sameMessageExists($locale, $message)
109
    {
110
        if (!isset($this->translation)) {
111
            throw new \InvalidArgumentException('translation is not set in RequestHandler.php');
112
        }
113
        $return = false;
114
        foreach ($this->translation->getMessages() as $translationMessage) {
115
            if (
116
                $translationMessage->getLocale() == $locale &&
117
                $translationMessage->getMessage() == $message
118
            ) {
119
                $return = true;
120
            }
121
        }
122
        return $return;
123
    }
124
}
125