Total Complexity | 40 |
Total Lines | 276 |
Duplicated Lines | 0 % |
Changes | 3 | ||
Bugs | 0 | Features | 0 |
Complex classes like Translation often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Translation, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
14 | class Translation |
||
15 | { |
||
16 | /** |
||
17 | * Holds the app configuration. |
||
18 | * |
||
19 | * @access protected |
||
20 | * @var Illuminate\Config\Repository |
||
|
|||
21 | */ |
||
22 | public $config; |
||
23 | |||
24 | /** |
||
25 | * Holds the current request. |
||
26 | * |
||
27 | * @access protected |
||
28 | * @var Illuminate\Http\Request |
||
29 | */ |
||
30 | protected $request; |
||
31 | |||
32 | /** |
||
33 | * Represents the translation model |
||
34 | * |
||
35 | * @access protected |
||
36 | * @var JosephNC\Translation\Models\Translation |
||
37 | */ |
||
38 | protected $translationModel; |
||
39 | |||
40 | /** |
||
41 | * Translation constructor |
||
42 | * |
||
43 | * @access public |
||
44 | * @var Illuminate\Contracts\Foundation\Application |
||
45 | */ |
||
46 | public function __construct( Application $app ) |
||
47 | { |
||
48 | $this->config = $app->make('config'); |
||
49 | $this->request = app( \Illuminate\Http\Request::class ); |
||
50 | $this->translationModel = $app->make($this->getTranslationModel()); |
||
51 | } |
||
52 | |||
53 | /** |
||
54 | * Returns the API Key |
||
55 | * |
||
56 | * @return string |
||
57 | */ |
||
58 | public function getApiKey() : string |
||
59 | { |
||
60 | return (string) $this->config->get( 'translation.key', '' ); |
||
61 | } |
||
62 | |||
63 | /** |
||
64 | * Returns the locale |
||
65 | * |
||
66 | * @return string |
||
67 | */ |
||
68 | public function getLocale() : string |
||
73 | } |
||
74 | |||
75 | /** |
||
76 | * Returns the array of configuration locales. |
||
77 | * |
||
78 | * @return array |
||
79 | */ |
||
80 | protected function getLocales() : array |
||
83 | } |
||
84 | |||
85 | /** |
||
86 | * Returns the default locale from the configuration. |
||
87 | * |
||
88 | * @access protected |
||
89 | * |
||
90 | * @return string |
||
91 | */ |
||
92 | protected function getDefaultLocale() : string |
||
93 | { |
||
94 | $locale = $this->config->get( 'app.locale', $this->config->get('app.fallback_locale') ); |
||
95 | |||
96 | $locale = $this->localeExist( $locale ) ? $locale : 'en'; |
||
97 | |||
98 | return (string) $locale; |
||
99 | } |
||
100 | |||
101 | /** |
||
102 | * Returns the translation model from the configuration. |
||
103 | * |
||
104 | * @access protected |
||
105 | * |
||
106 | * @return string |
||
107 | */ |
||
108 | protected function getTranslationModel() : string |
||
109 | { |
||
110 | return (string) $this->config->get('translation.models.translation', Models\Translation::class); |
||
111 | } |
||
112 | |||
113 | /** |
||
114 | * Returns the current route translation prefix |
||
115 | * using the srequest segment set in the config |
||
116 | * |
||
117 | * @return string |
||
118 | */ |
||
119 | public function getRoutePrefix() : string |
||
120 | { |
||
121 | $locale = (string) $this->request->segment( $this->getRequestSegment() ); |
||
122 | |||
123 | return $this->localeExist( $locale ) ? $locale : ''; |
||
124 | } |
||
125 | |||
126 | /** |
||
127 | * Returns the request segment to retrieve the locale from. |
||
128 | * |
||
129 | * @return int |
||
130 | */ |
||
131 | protected function getRequestSegment() : int |
||
134 | } |
||
135 | |||
136 | /** |
||
137 | * Sets the locale |
||
138 | * |
||
139 | * @param string $locale The locale to use. Defaults to 'en'. |
||
140 | * @throws InvalidArgumentException|Exception |
||
141 | */ |
||
142 | public function setLocale( string $locale = 'en' ) |
||
143 | { |
||
144 | if ( ! empty( $locale ) && ! $this->localeExist( $locale ) ) { |
||
145 | $message = 'Invalid Argument! Locale passed does not exist.'; |
||
146 | |||
147 | throw new InvalidArgumentException( $message ); |
||
148 | } |
||
149 | |||
150 | $_SESSION['locale'] = $locale; |
||
151 | } |
||
152 | |||
153 | /** |
||
154 | * Checks if the locale passed is exist |
||
155 | * |
||
156 | * @param string $locale The locale to check |
||
157 | * |
||
158 | * @return bool |
||
159 | */ |
||
160 | public function localeExist( string $locale = 'en' ) : bool |
||
163 | } |
||
164 | |||
165 | /** |
||
166 | * Translate the text |
||
167 | * |
||
168 | * @access public |
||
169 | * |
||
170 | * @param string $text |
||
171 | * @param array $replacements |
||
172 | * @param string $to_locale |
||
173 | * |
||
174 | * @throws InvalidArgumentException |
||
175 | * |
||
176 | * @return string The translated text. |
||
177 | */ |
||
178 | public function translate( string $text, array $replacements = [], string $to_locale = '' ) : string |
||
230 | } |
||
231 | |||
232 | /** |
||
233 | * Replaces placeholders with its real value |
||
234 | * |
||
235 | * @access private |
||
236 | * @param string $text The text having the placeholder |
||
237 | * @param array $replacements The replacement values in array |
||
238 | * |
||
239 | * @return string The replaced text |
||
240 | */ |
||
241 | private function makeReplacements( string $text, array $replacements ) : string |
||
242 | { |
||
243 | $keys = strtolower( '__' . join( '__,__', array_keys( $replacements ) ) . '__' ); |
||
244 | $search = explode( ',', $keys ); |
||
245 | $replace = array_values( $replacements ); |
||
246 | |||
247 | return (string) str_replace( $search, $replace, $text ); |
||
248 | } |
||
249 | |||
250 | /** |
||
251 | * Runs on page shutdown |
||
252 | * |
||
253 | * @access public |
||
254 | */ |
||
255 | public function shutdown() |
||
290 | } |
||
291 | } |
||
292 |