/** * Get account data or create new account if customer has no account for the requested asset. * * @param Request\Get $request * * @return Response\Get */ public function get(Request\Get $request) { $result = new Response\Get(); $this->_logger->info("'Get account' operation is called."); $accountId = $request->getAccountId(); $customerId = $request->getCustomerId(); $assetTypeId = $request->getAssetTypeId(); $assetTypeCode = $request->getAssetTypeCode(); $createNewAccIfMissed = $request->getCreateNewAccountIfMissed(); /* accountId has the highest priority */ if ($accountId) { $data = $this->_repoAccount->getById($accountId); } else { /* try to look up by customer id & asset type id */ if (!$assetTypeId) { /* get asset type ID by asset code */ $assetTypeId = $this->_repoTypeAsset->getIdByCode($assetTypeCode); } /* get account by customerId & assetTypeId */ $data = $this->_repoAccount->getByCustomerId($customerId, $assetTypeId); } /* analyze found data */ if ($data) { $result->setData($data); $result->markSucceed(); } else { if ($createNewAccIfMissed) { /* not found - add new account */ $data = [Account::ATTR_CUST_ID => $customerId, Account::ATTR_ASSET_TYPE_ID => $assetTypeId, Account::ATTR_BALANCE => 0]; $accId = $this->_repoAccount->create($data); $data[Account::ATTR_ID] = $accId; $result->setData($data); $result->markSucceed(); $this->_logger->info("There is no account for customer #{$customerId} and asset type #{$assetTypeId}. New account #{$accId} is created."); } } $this->_logger->info("'Get account' operation is completed."); return $result; }