middleware( function ($request, $next) { /** @var User $admin */ $admin = auth()->user(); /** @var CurrencyRepositoryInterface repository */ $this->repository = app(CurrencyRepositoryInterface::class); $this->userRepository = app(UserRepositoryInterface::class); $this->repository->setUser($admin); return $next($request); } ); } /** * Disable a currency. * * @param TransactionCurrency $currency * * @return JsonResponse * @codeCoverageIgnore */ public function disable(TransactionCurrency $currency): JsonResponse { // must be unused. if ($this->repository->currencyInUse($currency)) { return response()->json([], 409); } $this->repository->disable($currency); $manager = $this->getManager(); $defaultCurrency = app('amount')->getDefaultCurrencyByUser(auth()->user()); $this->parameters->set('defaultCurrency', $defaultCurrency); /** @var CurrencyTransformer $transformer */ $transformer = app(CurrencyTransformer::class); $transformer->setParameters($this->parameters); $resource = new Item($currency, $transformer, 'currencies'); return response()->json($manager->createData($resource)->toArray())->header('Content-Type', self::CONTENT_TYPE); } /** * Enable a currency. * * @param TransactionCurrency $currency * * @return JsonResponse * @codeCoverageIgnore */ public function enable(TransactionCurrency $currency): JsonResponse { $this->repository->enable($currency); $manager = $this->getManager(); $defaultCurrency = app('amount')->getDefaultCurrencyByUser(auth()->user()); $this->parameters->set('defaultCurrency', $defaultCurrency); /** @var CurrencyTransformer $transformer */ $transformer = app(CurrencyTransformer::class); $transformer->setParameters($this->parameters); $resource = new Item($currency, $transformer, 'currencies'); return response()->json($manager->createData($resource)->toArray())->header('Content-Type', self::CONTENT_TYPE); } /** * Make the currency a default currency. * * @param TransactionCurrency $currency * * @return JsonResponse * @codeCoverageIgnore */ public function makeDefault(TransactionCurrency $currency): JsonResponse { $this->repository->enable($currency); app('preferences')->set('currencyPreference', $currency->code); app('preferences')->mark(); $manager = $this->getManager(); $this->parameters->set('defaultCurrency', $currency); /** @var CurrencyTransformer $transformer */ $transformer = app(CurrencyTransformer::class); $transformer->setParameters($this->parameters); $resource = new Item($currency, $transformer, 'currencies'); return response()->json($manager->createData($resource)->toArray())->header('Content-Type', self::CONTENT_TYPE); } /** * Update a currency. * * @param UpdateRequest $request * @param TransactionCurrency $currency * * @return JsonResponse */ public function update(UpdateRequest $request, TransactionCurrency $currency): JsonResponse { $data = $request->getAll(); $currency = $this->repository->update($currency, $data); if (true === $request->boolean('default')) { app('preferences')->set('currencyPreference', $currency->code); app('preferences')->mark(); } $manager = $this->getManager(); $defaultCurrency = app('amount')->getDefaultCurrencyByUser(auth()->user()); $this->parameters->set('defaultCurrency', $defaultCurrency); /** @var CurrencyTransformer $transformer */ $transformer = app(CurrencyTransformer::class); $transformer->setParameters($this->parameters); $resource = new Item($currency, $transformer, 'currencies'); return response()->json($manager->createData($resource)->toArray())->header('Content-Type', self::CONTENT_TYPE); } }