Completed
Pull Request — master (#80)
by
unknown
64:19
created

RequestHandler   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 131
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 3
Bugs 0 Features 2
Metric Value
wmc 19
c 3
b 0
f 2
lcom 1
cbo 5
dl 0
loc 131
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getTranslation() 0 4 1
A setTranslation() 0 4 1
A remakeRequest() 0 19 4
A turnToArray() 0 21 4
B sameMessageExists() 0 16 5
A sameTagExists() 0 12 4
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 Elasticsearch\Common\Exceptions\InvalidArgumentException;
15
use Symfony\Component\HttpFoundation\Request;
16
use ONGR\TranslationsBundle\Document\Translation;
17
18
/**
19
* Remakes requests to be compatible with TranslationManager.
20
*/
21
class RequestHandler
22
{
23
    /**
24
     * Translation that is modified by the request
25
     *
26
     * @var Translation
27
     */
28
    private $translation;
29
30
    /**
31
     * @return Translation
32
     */
33
    public function getTranslation()
34
    {
35
        return $this->translation;
36
    }
37
38
    /**
39
     * @param Translation $translation
40
     */
41
    public function setTranslation(Translation $translation)
42
    {
43
        $this->translation = $translation;
44
    }
45
46
    /**
47
     * Remakes a request to have json content
48
     * of a single object. If there is a number of
49
     * locales associated with a request it returns an
50
     * array of new requests
51
     *
52
     * @param Request $request
53
     *
54
     * @return mixed
55
     */
56
    public function remakeRequest(Request $request)
57
    {
58
        $content = [];
59
        $content['name'] = $request->request->get('name');
60
        $content['properties'] = $request->request->get('properties');
61
        $content['id'] = $request->request->get('id');
62
        $content['findBy'] = $request->request->get('findBy');
63
        if ($request->request->has('locales')) {
64
            return $this->turnToArray($request, $content);
65
        }
66
        if (
67
            $content['name'] == 'tags' &&
68
            $this->sameTagExists($content['properties']['name'])
69
        ) {
70
            throw new \InvalidArgumentException('Tag already set');
71
        }
72
        $content = json_encode($content);
73
        return new Request([], [], [], [], [], [], $content);
74
    }
75
76
    /**
77
     * Turns a request to an array of requests with json content
78
     *
79
     * @param Request $request
80
     * @param array $content
81
     *
82
     * @return array
83
     */
84
    private function turnToArray(Request $request, array $content)
85
    {
86
        $requests = [];
87
        $locales = $request->request->get('locales');
88
        $messages = $request->request->get('messages');
89
        $findBy = $request->request->get('findBy');
90
        foreach ($locales as $locale) {
91
            if (
92
                $messages[$locale] == '' ||
93
                $this->sameMessageExists($locale, $messages[$locale])
94
            ) {
95
                continue;
96
            }
97
            $content['properties']['locale'] = $locale;
98
            $content['properties']['message'] = $messages[$locale];
99
            $content['properties']['status'] = 'dirty';
100
            $content['findBy'] = $findBy[$locale];
101
            $requests[] = new Request([], [], [], [], [], [], json_encode($content));
102
        }
103
        return $requests;
104
    }
105
106
    /**
107
     * Checks if the given message matches the
108
     * message set in the given locale of the translation
109
     *
110
     * @param string $locale
111
     * @param string $message
112
     *
113
     * @return bool
114
     */
115
    private function sameMessageExists($locale, $message)
116
    {
117
        if (!isset($this->translation)) {
118
            throw new \InvalidArgumentException('translation is not set in RequestHandler.php');
119
        }
120
        $return = false;
121
        foreach ($this->translation->getMessages() as $translationMessage) {
122
            if (
123
                $translationMessage->getLocale() == $locale &&
124
                $translationMessage->getMessage() == $message
125
            ) {
126
                $return = true;
127
            }
128
        }
129
        return $return;
130
    }
131
132
    /**
133
     * Checks if the same tag exists
134
     *
135
     *  @param string $name
136
     *
137
     * @return bool
138
     */
139
    private function sameTagExists($name)
140
    {
141
        if (!isset($this->translation)) {
142
            throw new \InvalidArgumentException('translation is not set in RequestHandler.php');
143
        }
144
        foreach ($this->translation->getTags() as $tag) {
145
            if ($tag->getName() == $name) {
146
                return true;
147
            }
148
        }
149
        return false;
150
    }
151
}
152