JsLocalizationController::getConfigJson()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
namespace JsLocalization\Http\Controllers;
4
5
use Illuminate\Routing\Controller;
6
use JsLocalization\Facades\ConfigCachingService;
7
use JsLocalization\Facades\MessageCachingService;
8
use JsLocalization\Http\Responses\StaticFileResponse;
9
10
class JsLocalizationController extends Controller
11
{
12
13
    /**
14
     * Create the JS-Response for all configured translation messages
15
     *
16
     * @return \Illuminate\Http\Response
17
     */
18 2
    public function createJsMessages()
19
    {
20 2
        $contents = $this->getMessagesJson();
21
22 2
        return response($contents)
0 ignored issues
show
Bug introduced by
The method header() does not exist on Symfony\Component\HttpFoundation\Response. Did you maybe mean sendHeaders()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
23 2
            ->header('Content-Type', 'text/javascript')
24 2
            ->setLastModified(MessageCachingService::getLastRefreshTimestamp());
25
    }
26
27
    /**
28
     * @return \Illuminate\Http\Response
29
     */
30 2
    public function createJsConfig()
31
    {
32 2
        $contents = $this->getConfigJson();
33
34
        /** @var \Illuminate\Http\Response $response */
35 2
        $response = response($contents);
36 2
        $response->header('Content-Type', 'text/javascript');
37
38 2
        if (ConfigCachingService::isDisabled()) {
39 1
            $response->setEtag(md5($contents));
40 1
        } else {
41 1
            $response->setLastModified(ConfigCachingService::getLastRefreshTimestamp());
42
        }
43
44 2
        return $response;
45
    }
46
47
    /**
48
     * Deliver the Framework for getting the translation in JS
49
     *
50
     * @return \Illuminate\Http\Response
51
     */
52 1
    public function deliverLocalizationJS()
53
    {
54 1
        $response = new StaticFileResponse( __DIR__."/../../../public/js/localization.min.js" );
55 1
        $response->setPublic();
56 1
        $response->header('Content-Type', 'application/javascript');
57
58 1
        return $response;
59
    }
60
61
    /**
62
     * Deliver one file that combines messages and framework.
63
     * Saves one additional HTTP-Request
64
     *
65
     * @return \Illuminate\Http\Response
66
     */
67 1
    public function deliverAllInOne()
68
    {
69 1
        $contents = file_get_contents( __DIR__."/../../../public/js/localization.min.js" );
70 1
        $contents .= "\n";
71 1
        $contents .= $this->getMessagesJson();
72 1
        $contents .= $this->getConfigJson();
73
74
        /** @var \Illuminate\Http\Response $response */
75 1
        $response = response($contents);
76 1
        $response->header('Content-Type', 'text/javascript');
77
78 1
        if (ConfigCachingService::isDisabled()) {
79
            $response->setEtag(md5($contents));
80
        } else {
81 1
            $response->setLastModified(MessageCachingService::getLastRefreshTimestamp());
82
        }
83
84 1
        return $response;
85
    }
86
87
    /**
88
     * Transforms the cached data to stay compatible to old versions of the package.
89
     *
90
     * @param string $messages
91
     * @return string
92
     */
93 3
    protected function ensureBackwardsCompatibility($messages)
94
    {
95 3
        if (preg_match('/^\\{"[a-z]{2}":/', $messages)) {
96 2
            return $messages;
97
        } else {
98 1
            return '{"' . app()->getLocale() . '":' . $messages . '}';
99
        }
100
    }
101
102
    /**
103
     * Get the configured messages from the translation files
104
     *
105
     * @return string
106
     */
107 3
    protected function getMessagesJson()
108
    {
109 3
        $messages = MessageCachingService::getMessagesJson();
110 3
        $messages = $this->ensureBackwardsCompatibility($messages);
111
112 3
        $contents  = 'Lang.addMessages(' . $messages . ');';
113
114 3
        return $contents;
115
    }
116
117
    /**
118
     * Get the JSON-encoded config properties that shall be passed to the client.
119
     *
120
     * @return string
121
     */
122 3
    protected function getConfigJson()
123
    {
124 3
        $config = ConfigCachingService::getConfigJson();
125
126 3
        $contents = 'Config.addConfig(' . $config . ');';
127
128 3
        return $contents;
129
    }
130
131
}
132