Пример #1
0
 /**
  * @param Request\GetRepresentative $request
  *
  * @return Response\GetRepresentative
  */
 public function getRepresentative(Request\GetRepresentative $request)
 {
     $result = new Response\GetRepresentative();
     $typeId = $request->getAssetTypeId();
     $typeCode = $request->getAssetTypeCode();
     $this->_logger->info("'Get representative account' operation is called.");
     if (is_null($typeId)) {
         $typeId = $this->_repoTypeAsset->getIdByCode($typeCode);
     }
     if (!is_null($typeId)) {
         if (isset($this->_cachedRepresentAccs[$typeId])) {
             $result->setData($this->_cachedRepresentAccs[$typeId]);
             $result->markSucceed();
         } else {
             /* there is no cached data yet */
             /* get representative customer ID */
             $customerId = $this->_repoAccount->getRepresentativeCustomerId();
             /* get all accounts for the representative customer */
             $accounts = $this->_repoAccount->getAllByCustomerId($customerId);
             if ($accounts) {
                 $mapped = [];
                 foreach ($accounts as $one) {
                     $mapped[$one->getAssetTypeId()] = $one;
                 }
                 $this->_cachedRepresentAccs = $mapped;
             }
             /* check selected accounts */
             if (isset($this->_cachedRepresentAccs[$typeId])) {
                 $result->setData($this->_cachedRepresentAccs[$typeId]);
                 $result->markSucceed();
             } else {
                 /* there is no accounts yet */
                 $req = new Request\Get();
                 $req->setCustomerId($customerId);
                 $req->setAssetTypeId($typeId);
                 $req->setCreateNewAccountIfMissed();
                 $resp = $this->get($req);
                 $accData = $resp->getData();
                 $this->_cachedRepresentAccs[$accData[Account::ATTR_ASSET_TYPE_ID]] = new Account($accData);
                 $result->setData($accData);
                 $result->markSucceed();
             }
         }
     } else {
         $this->_logger->error("Asset type is not defined (asset code: {$typeCode}).");
     }
     if ($result->isSucceed()) {
         $repAccId = $result->getId();
         $this->_logger->info("Representative account #{$repAccId} is found.");
     }
     $this->_logger->info("'Get representative account' operation is completed.");
     return $result;
 }