Completed
Push — master ( 9d729c...78ea8b )
by Valentyn
03:06
created

TranslatedResponseTrait::translateEntities()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 5.0113

Importance

Changes 0
Metric Value
dl 0
loc 24
ccs 12
cts 13
cp 0.9231
rs 9.2248
c 0
b 0
f 0
cc 5
nc 5
nop 2
crap 5.0113
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Translation;
6
7
use Symfony\Component\HttpFoundation\RequestStack;
8
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
9
use Symfony\Component\Serializer\Serializer;
10
11
/**
12
 * Trait TranslatedResponseTrait.
13
 *
14
 * @method get($id)
15
 * @method has($id):bool
16
 */
17
trait TranslatedResponseTrait
18
{
19 18
    public function translateResponse($response, array $context): array
20
    {
21
        /** @var $normalizer Serializer */
22 18
        $normalizer = $this->get('serializer');
23 18
        $response = $normalizer->normalize($response, null, $context);
24 18
        $response = $this->translateEntities(is_array($response) ? $response : [$response]);
25
26 18
        return $response;
27
    }
28
29 18
    protected function translateEntities(array $data, $recursive = true): array
30
    {
31 18
        $translatedData = [];
32
33 18
        foreach ($data as $key => $value) {
34 18
            if ($key === 'translations') {
35 12
                $translatedData = array_merge($translatedData, $this->getEntityTranslation($value));
36
37 12
                if ($recursive === true) {
38 12
                    continue;
39
                }
40
                break;
41
            }
42
43 18
            if (is_array($value)) {
44 18
                $data[$key] = $this->translateEntities($value, $recursive);
45
            }
46
        }
47
48 18
        unset($data['translations']);
49 18
        $data = array_merge($data, $translatedData);
50
51 18
        return $data;
52
    }
53
54 12
    private function getEntityTranslation(array $translations)
55
    {
56 12
        $userLocale = $this->getUserPreferredLocale(array_keys($translations));
57
58 12
        return $translations[$userLocale];
59
    }
60
61
    /**
62
     * @param array $locales
63
     *
64
     * @return null|string
65
     */
66 12
    private function getUserPreferredLocale(array $locales = [])
67
    {
68 12
        if (!isset($locales[0])) {
69
            // there's no translations for this entity
70
            throw new NotFoundHttpException();
71
        }
72
73
        /** @var $requestStack RequestStack */
74 12
        $requestStack = $this->get('request_stack');
75 12
        $request = $requestStack->getCurrentRequest();
76
77 12
        $preferredLocale = $request->getPreferredLanguage($locales);
78
79 12
        $locale = $request->getLocale(); // can be set by query param (?language=ru) or by symfony
80 12
        if ($locale !== $request->getDefaultLocale() && in_array($locale, $locales, true) === true) {
81 2
            $preferredLocale = $locale;
82
        }
83
84 12
        return $preferredLocale;
85
    }
86
}
87