Ejemplo n.º 1
0
 /**
  * Add new customer to downline and new entry to change log.
  *
  * @param Request\Add $request
  *
  * @return Response\Add
  */
 public function add(Request\Add $request)
 {
     $result = new Response\Add();
     $customerId = $request->getCustomerId();
     $parentId = $request->getParentId();
     $humanReference = $request->getReference();
     $countryCode = $request->getCountryCode();
     $date = $request->getDate();
     $this->_logger->info("Add new customer #{$customerId} with parent #{$parentId} to downline tree.");
     $def = $this->_manTrans->begin();
     try {
         /* define referred parent */
         $parentId = $this->_subReferral->getReferredParentId($customerId, $parentId);
         if ($customerId == $parentId) {
             /* add root node */
             $this->_logger->info("This is root node (customer id is equal to parent id).");
             $path = Cfg::DTPS;
             $depth = Cfg::INIT_DEPTH;
         } else {
             /* get parent data by parent Mage id */
             $data = $this->_repoCustomer->getById($parentId);
             $parentPath = $data->getPath();
             $parentDepth = $data->getDepth();
             $path = $parentPath . $parentId . Cfg::DTPS;
             $depth = $parentDepth + 1;
         }
         /* add customer to downline */
         $toAdd = [Customer::ATTR_CUSTOMER_ID => $customerId, Customer::ATTR_PARENT_ID => $parentId, Customer::ATTR_DEPTH => $depth, Customer::ATTR_PATH => $path, Customer::ATTR_REFERRAL_CODE => $customerId];
         if (isset($humanReference)) {
             $toAdd[Customer::ATTR_HUMAN_REF] = $humanReference;
         }
         if (isset($countryCode)) {
             $toAdd[Customer::ATTR_COUNTRY_CODE] = $countryCode;
         } else {
             $toAdd[Customer::ATTR_COUNTRY_CODE] = $this->_subReferral->getDefaultCountryCode();
         }
         $this->_repoCustomer->create($toAdd);
         /* save log record to changes registry */
         $formatted = $date;
         $toLog = [Change::ATTR_CUSTOMER_ID => $customerId, Change::ATTR_PARENT_ID => $parentId, Change::ATTR_DATE_CHANGED => $formatted];
         $idLog = $this->_repoChange->create($toLog);
         if ($idLog) {
             $this->_logger->debug("Downline changes are logged in registry with date: {$formatted}.");
             $this->_logger->debug("New change log record #{$idLog} is inserted (customer: {$customerId}, parent: {$parentId}, date: {$formatted}).");
             $result->setData($toAdd);
             $result->markSucceed();
             $this->_manTrans->commit($def);
             $this->_logger->info("New customer #{$customerId} with parent #{$parentId} is added to downline tree.");
         }
     } finally {
         $this->_manTrans->end($def);
     }
     return $result;
 }