nstack-io /
laravel-sdk
| 1 | <?php |
||
| 2 | |||
| 3 | namespace NStack\Translation; |
||
| 4 | |||
| 5 | use Carbon\Carbon; |
||
| 6 | use Illuminate\Filesystem\Filesystem; |
||
| 7 | use Illuminate\Support\Collection; |
||
| 8 | use Illuminate\Translation\FileLoader; |
||
| 9 | use NStack\Clients\LocalizeClient; |
||
| 10 | use NStack\Models\Resource; |
||
| 11 | use NStack\NStack; |
||
| 12 | |||
| 13 | /** |
||
| 14 | * NStackLoader for translations |
||
| 15 | * |
||
| 16 | * @author Pawel Wilk <[email protected]> |
||
| 17 | */ |
||
| 18 | class NStackLoader extends FileLoader |
||
| 19 | { |
||
| 20 | /** |
||
| 21 | * @var NStack |
||
| 22 | */ |
||
| 23 | protected $nstack; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @var LocalizeClient|null |
||
| 27 | */ |
||
| 28 | protected $client; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @var string |
||
| 32 | */ |
||
| 33 | protected $platform; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var int |
||
| 37 | */ |
||
| 38 | protected $cacheTime; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * To avoid spamming the service, only retry the service if retry is above this count or sec below. |
||
| 42 | * |
||
| 43 | * @var int |
||
| 44 | */ |
||
| 45 | protected $maxNetworkRetries; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * To avoid spamming the service, only retry the service if retry is above the count above or this sec. |
||
| 49 | * |
||
| 50 | * @var int |
||
| 51 | */ |
||
| 52 | protected $retryNetworkAfterSec; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @var \Illuminate\Database\Eloquent\Collection |
||
| 56 | */ |
||
| 57 | protected $failedCarbons; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Constructor |
||
| 61 | * |
||
| 62 | * @param Filesystem $files |
||
| 63 | * @param string $path |
||
| 64 | * @param NStack $nstack |
||
| 65 | */ |
||
| 66 | public function __construct(Filesystem $files, $path, NStack $nstack) |
||
| 67 | { |
||
| 68 | parent::__construct($files, $path); |
||
| 69 | |||
| 70 | $this->nstack = $nstack; |
||
| 71 | $this->failedCarbons = new Collection(); |
||
| 72 | |||
| 73 | $this->platform = config('nstack.platform'); |
||
| 74 | $this->cacheTime = config('nstack.cacheTime', 600); |
||
| 75 | $this->maxNetworkRetries = config('nstack.maxNetworkRetries', 3); |
||
| 76 | $this->retryNetworkAfterSec = config('nstack.retryNetworkAfterSec', 10); |
||
| 77 | } |
||
| 78 | |||
| 79 | /** |
||
| 80 | * {@inheritDoc} |
||
| 81 | * @see \Illuminate\Translation\LoaderInterface::load() |
||
| 82 | */ |
||
| 83 | public function load($locale, $group, $namespace = null) |
||
| 84 | { |
||
| 85 | if (!is_null($namespace) && ($namespace != '*')) { |
||
| 86 | return $this->loadNamespaced($locale, $group, $namespace); |
||
| 87 | } |
||
| 88 | |||
| 89 | if ($resource = $this->findResource($locale)) { |
||
| 90 | $data = $this->loadTranslations($resource); |
||
| 91 | |||
| 92 | if (isset($data[$group])) { |
||
| 93 | return $data[$group]; |
||
| 94 | } |
||
| 95 | } |
||
| 96 | |||
| 97 | return parent::load($locale, $group); |
||
| 98 | } |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Download and cache translations |
||
| 102 | * |
||
| 103 | * @param Resource $resource |
||
| 104 | * @param bool $refresh |
||
| 105 | * @return array |
||
| 106 | */ |
||
| 107 | protected function loadTranslations(Resource $resource, $refresh = true) |
||
| 108 | { |
||
| 109 | $cacheKey = sprintf('nstack.resource.%d', $resource->getId()); |
||
| 110 | |||
| 111 | if (($data = \Cache::get($cacheKey)) && !$refresh) { |
||
| 112 | return $data; |
||
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
| 113 | } |
||
| 114 | |||
| 115 | $response = $this->request(function () use ($resource) { |
||
| 116 | return $this->getClient()->showResource($resource->getUrl()); |
||
| 117 | }); |
||
| 118 | |||
| 119 | if (empty($response['data'])) { |
||
| 120 | return []; |
||
| 121 | } |
||
| 122 | |||
| 123 | \Cache::put($cacheKey, $response['data'], $this->cacheTime); |
||
| 124 | |||
| 125 | return $response['data']; |
||
| 126 | } |
||
| 127 | |||
| 128 | protected function request(\Closure $closure) |
||
| 129 | { |
||
| 130 | try { |
||
| 131 | return $closure(); |
||
| 132 | } catch (\GuzzleHttp\Exception\GuzzleException $e) { |
||
| 133 | if ($e->getCode() == 403) { |
||
| 134 | throw new \Exception('Invalid credentials'); |
||
| 135 | } |
||
| 136 | |||
| 137 | $this->performFail(); |
||
| 138 | } |
||
| 139 | |||
| 140 | if ($this->shouldTryAgain()) { |
||
| 141 | sleep(1); |
||
| 142 | |||
| 143 | return $this->request($closure); |
||
| 144 | } else { |
||
| 145 | throw new \Exception('Maximum amount retries reached'); |
||
| 146 | } |
||
| 147 | } |
||
| 148 | |||
| 149 | /** |
||
| 150 | * Find resource corresponding to locale |
||
| 151 | * |
||
| 152 | * @param string $locale |
||
| 153 | * @return \NStack\Models\Resource|boolean |
||
| 154 | */ |
||
| 155 | protected function findResource($locale) |
||
| 156 | { |
||
| 157 | foreach ($this->getResources() as $resource) { |
||
| 158 | /* @var $resource \NStack\Models\Resource */ |
||
| 159 | if (locale_filter_matches($resource->getLanguage()->getLocale(), $locale)) { |
||
| 160 | return $resource; |
||
| 161 | } |
||
| 162 | } |
||
| 163 | |||
| 164 | return false; |
||
| 165 | } |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Return nstack localize client |
||
| 169 | * |
||
| 170 | * @return \NStack\Clients\LocalizeClient |
||
| 171 | */ |
||
| 172 | protected function getClient() |
||
| 173 | { |
||
| 174 | if (is_null($this->client)) { |
||
| 175 | $this->client = new LocalizeClient($this->nstack->getConfig()); |
||
| 176 | } |
||
| 177 | |||
| 178 | return $this->client; |
||
| 179 | } |
||
| 180 | |||
| 181 | /** |
||
| 182 | * getResources |
||
| 183 | * |
||
| 184 | * @param bool $force |
||
| 185 | * @return array|mixed |
||
| 186 | * @throws \Exception |
||
| 187 | * @author Casper Rasmussen <[email protected]> |
||
| 188 | */ |
||
| 189 | protected function getResources($force = false) |
||
| 190 | { |
||
| 191 | $cacheKey = 'nstack.availableLocales'; |
||
| 192 | |||
| 193 | if (($data = \Cache::get($cacheKey)) && !$force) { |
||
| 194 | return $data; |
||
| 195 | } |
||
| 196 | |||
| 197 | $response = $this->request(function () { |
||
| 198 | return $this->getClient()->indexResources($this->platform); |
||
| 199 | }); |
||
| 200 | |||
| 201 | if (empty($response)) { |
||
| 202 | return []; |
||
| 203 | } |
||
| 204 | |||
| 205 | \Cache::put($cacheKey, $response, $this->cacheTime); |
||
| 206 | |||
| 207 | return $response; |
||
| 208 | } |
||
| 209 | |||
| 210 | /** |
||
| 211 | * shouldTryAgain. |
||
| 212 | * |
||
| 213 | * @return bool |
||
| 214 | * @author Casper Rasmussen <[email protected]> |
||
| 215 | */ |
||
| 216 | private function shouldTryAgain() |
||
| 217 | { |
||
| 218 | if ($this->failedCarbons->count() < $this->maxNetworkRetries) { |
||
| 219 | return true; |
||
| 220 | } |
||
| 221 | |||
| 222 | /** @var Carbon $carbon */ |
||
| 223 | $carbon = $this->failedCarbons->first(); |
||
| 224 | if ($carbon->diffInSeconds(Carbon::now()) >= $this->retryNetworkAfterSec) { |
||
| 225 | return true; |
||
| 226 | } |
||
| 227 | |||
| 228 | return false; |
||
| 229 | } |
||
| 230 | |||
| 231 | /** |
||
| 232 | * performFail. |
||
| 233 | * |
||
| 234 | * @return void |
||
| 235 | * @author Casper Rasmussen <[email protected]> |
||
| 236 | */ |
||
| 237 | private function performFail() |
||
| 238 | { |
||
| 239 | $this->failedCarbons->prepend(new Carbon()); |
||
| 240 | if ($this->failedCarbons->count() > 3) { |
||
| 241 | $this->failedCarbons->pop(); |
||
| 242 | } |
||
| 243 | } |
||
| 244 | } |
||
| 245 |