コード例 #1
0
ファイル: Socnet.php プロジェクト: OnlineBidder/onlineBidder
 public function saveAds(array $ads)
 {
     foreach ($ads as $ad) {
         $adsEntity = new VkAds();
         foreach ($ad as $key => $value) {
             $adsEntity->set($key, $value);
         }
         $this->getObjectManager()->persist($adsEntity);
         $this->getObjectManager()->flush();
     }
 }
コード例 #2
0
 public function adsCheckerForceAction()
 {
     ini_set('max_execution_time', 200);
     /**@var $campaign VkCampaigns*/
     /**@var $vkAd VkAds*/
     /**@var $vkAccount VkAccounts*/
     /**@var $cabinet Cabinets*/
     $vkAccountId = (int) $this->params()->fromRoute('vk_account_id', 0);
     $cabinetId = (int) $this->params()->fromRoute('cabinet_id', 0);
     $vkClientId = (int) $this->params()->fromRoute('client_id', 0);
     $vkCampaignId = (int) $this->params()->fromRoute('campaign_id', 0);
     if (!$cabinetId) {
         return true;
     }
     $cabinet = $this->cabinetsRepository->findBy(['vk_account_id' => $vkAccountId, 'account_id' => $cabinetId]);
     $cabinet = reset($cabinet);
     $vkUserId = $cabinet->getVkUserId();
     $vkAccount = $this->vkAccountsRepository->findBy(['vk_user_id' => $vkUserId]);
     $vkAccount = reset($vkAccount);
     $vkApi = Vk\Core::getInstance()->apiVersion('5.5')->setToken($vkAccount->getAccessKey());
     $params = ['account_id' => $cabinet->getAccountId(), 'include_deleted' => 1];
     if ($vkClientId) {
         $params['client_id'] = $vkClientId;
     }
     if ($vkCampaignId) {
         $params['campaign_ids'] = json_encode((array) $vkCampaignId);
     }
     $vkCampaigns = $vkApi->request('ads.getCampaigns', $params)->getResponse();
     foreach ($vkCampaigns as $vkCampaign) {
         $campaignForSave = $this->campaignsRepository->findBy(['id' => $vkCampaign->id]);
         $campaignForSave = reset($campaignForSave);
         if ($campaignForSave) {
             foreach ($vkCampaign as $key => $value) {
                 $campaignForSave->set($key, $value);
             }
             if ($vkCampaign->status == 2) {
                 $campaignForSave->setBidderControl(0);
             }
             $this->getObjectManager()->persist($campaignForSave);
         } else {
             $campaignEntity = new VkCampaigns();
             $campaignEntity->setCabinetId($cabinetId);
             $campaignEntity->setVkClientId($vkClientId);
             $campaignEntity->setVkAccountId($vkAccountId);
             foreach ($vkCampaign as $key => $value) {
                 $campaignEntity->set($key, $value);
             }
             $this->getObjectManager()->persist($campaignEntity);
         }
         $params = ['account_id' => $cabinet->getAccountId(), 'campaign_ids' => json_encode((array) $vkCampaign->id), 'include_deleted' => 0];
         if ($vkClientId) {
             $params['client_id'] = $vkClientId;
         }
         $adInVk = $vkApi->request('ads.getAds', $params)->getResponse();
         $vkAds = $this->vkAdsRepository->findAll();
         foreach ($adInVk as $ad) {
             $skip = false;
             foreach ($vkAds as $vkAd) {
                 if ($vkAd->getId() == $ad->id) {
                     foreach ($ad as $key => $value) {
                         $vkAd->set($key, $value);
                     }
                     $this->getObjectManager()->persist($vkAd);
                     $skip = true;
                     break;
                 }
             }
             if (!$skip) {
                 $adsEntity = new VkAds();
                 $adsEntity->setVkClientId($vkClientId);
                 foreach ($ad as $key => $value) {
                     $adsEntity->set($key, $value);
                 }
                 $this->getObjectManager()->persist($adsEntity);
             }
         }
         $this->getObjectManager()->flush();
     }
     echo '1';
     exit;
     return true;
 }
コード例 #3
0
ファイル: VkLogic.php プロジェクト: OnlineBidder/onlineBidder
 public function collateQueue($onlyPriority = false)
 {
     echo ' - get all queue' . PHP_EOL;
     $isPriority = true;
     $queue = $this->getVkPriorityQueue();
     if (!$onlyPriority && !$queue) {
         $isPriority = false;
         $queue = $this->getVkDataUpdateQueue();
     }
     /**@var $task VkDataUpdateQueue*/
     /**@var $vkAd VkAds*/
     /**@var $adObject VkAds*/
     foreach ($queue as $countIteration => $task) {
         echo '  -- queue number ' . $countIteration . PHP_EOL;
         echo '  -- queue task ' . $task->getMethod() . PHP_EOL;
         $start = microtime(true);
         if (!$isPriority && $this->issetPriorityQueue()) {
             break;
         }
         $this->getObjectManager()->remove($task);
         $this->getObjectManager()->flush();
         $vkApi = Vk\Core::getInstance()->apiVersion('5.5')->setToken($task->getAccessToken());
         $data = unserialize($task->getData());
         $res = $vkApi->request($task->getMethod(), $data);
         $res = $res->getResponse();
         if ($res) {
             $clientId = 0;
             if (isset($data['client_id'])) {
                 $clientId = $data['client_id'];
             }
             switch ($task->getMethod()) {
                 case 'ads.getStatistics':
                     $this->getServiceLocator()->get('StatsModule\\Logic\\StatsLogger')->saveVkData($res);
                     break;
                 case 'ads.getAds':
                     $resultAdsArray = [];
                     $vkAds = $this->getObjectManager()->getRepository('\\Socnet\\Entity\\VkAds')->findBy(['vk_client_id' => $clientId]);
                     foreach ($vkAds as $adObject) {
                         $resultAdsArray[$adObject->getId()] = $adObject;
                     }
                     unset($vkAds);
                     if (0 && count($res) === 2000) {
                         $taskData = unserialize($task->getData());
                         $taskData['offset'] = isset($taskData['offset']) ? $taskData['offset'] + 1999 : 1999;
                         $this->pushIntoQueue('ads.getAds', $task->getAccessToken(), serialize($taskData));
                     }
                     foreach ($res as $ad) {
                         $needUpdate = false;
                         if (isset($resultAdsArray[$ad->id])) {
                             $vkAd = $resultAdsArray[$ad->id];
                             foreach ($ad as $key => $value) {
                                 if ($vkAd->get($key) !== false && $vkAd->get($key) != $value) {
                                     $needUpdate = true;
                                     $vkAd->set($key, $value);
                                 }
                                 if ($ad->status != self::AD_STATUS_DELETE) {
                                     $adRejectReason = $this->getObjectManager()->getRepository('\\Socnet\\Entity\\VkAdsRejectionReasons')->findBy(['id' => $ad->id]);
                                     if ($ad->approved == self::AD_APPROVED_REJECT) {
                                         if (!$adRejectReason) {
                                             $this->pushIntoQueue('ads.getRejectionReason', $task->getAccessToken(), serialize(['account_id' => $data['account_id'], 'ad_id' => $ad->id]));
                                             $this->getObjectManager()->flush();
                                         }
                                     } elseif ($adRejectReason) {
                                         $this->getObjectManager()->remove(reset($adRejectReason));
                                         $this->getObjectManager()->flush();
                                     }
                                 } elseif ($vkAd->getBidderControl()) {
                                     $vkAd->setBidderControl(0);
                                     $needUpdate = true;
                                 }
                             }
                             if ($needUpdate) {
                                 $this->getObjectManager()->persist($vkAd);
                             }
                         } else {
                             $adsEntity = new VkAds();
                             if (isset($data['client_id'])) {
                                 $adsEntity->setVkClientId($data['client_id']);
                             }
                             foreach ($ad as $key => $value) {
                                 $adsEntity->set($key, $value);
                             }
                             $this->getObjectManager()->persist($adsEntity);
                         }
                         unset($ad);
                     }
                     $this->getObjectManager()->flush();
                     break;
                 case 'ads.getCampaigns':
                     foreach ($res as $vkCampaign) {
                         $campaignForSave = $this->getObjectManager()->getRepository('\\Socnet\\Entity\\VkCampaigns')->findBy(['id' => $vkCampaign->id]);
                         $campaignForSave = reset($campaignForSave);
                         if ($campaignForSave) {
                             foreach ($vkCampaign as $key => $value) {
                                 $campaignForSave->set($key, $value);
                             }
                             if ($vkCampaign->status == 2) {
                                 $campaignForSave->setBidderControl(0);
                             }
                             $this->getObjectManager()->persist($campaignForSave);
                         } else {
                             $additionalData = $task->getAdditionalInfo();
                             $campaignEntity = new VkCampaigns();
                             $campaignEntity->setCabinetId($data['account_id']);
                             if (isset($data['client_id'])) {
                                 $campaignEntity->setVkClientId($data['client_id']);
                             }
                             if ($additionalData) {
                                 $additionalData = unserialize($additionalData);
                                 $campaignEntity->setVkAccountId($additionalData['vk_account_id']);
                             }
                             foreach ($vkCampaign as $key => $value) {
                                 $campaignEntity->set($key, $value);
                             }
                             $this->getObjectManager()->persist($campaignEntity);
                         }
                         unset($vkCampaign);
                     }
                     $this->getObjectManager()->flush();
                     break;
                 case 'ads.getAdsLayout':
                     $filter = [];
                     $vkAdsTargeting = $this->getObjectManager()->getRepository('\\Socnet\\Entity\\VkAds')->findBy(['vk_client_id' => $clientId]);
                     /**@var $ad VkAds*/
                     foreach ($vkAdsTargeting as $ad) {
                         $filter[] = $ad->getId();
                     }
                     unset($vkAdsTargeting);
                     $vkAdsLayout = $this->getObjectManager()->getRepository('\\Socnet\\Entity\\VkAdsLayout')->findBy(['id' => $filter]);
                     foreach ($res as $adLayout) {
                         $skip = false;
                         $needUpdate = false;
                         foreach ($vkAdsLayout as $vkAd) {
                             if ($vkAd->getId() == $adLayout->id) {
                                 foreach ($adLayout as $key => $value) {
                                     if ($vkAd->get($key) !== false && $vkAd->get($key) != $value) {
                                         $needUpdate = true;
                                         $vkAd->set($key, $value);
                                     }
                                 }
                                 if ($needUpdate) {
                                     $this->getObjectManager()->persist($vkAd);
                                 }
                                 $skip = true;
                                 break;
                             }
                         }
                         if (!$skip) {
                             $adsLayoutEntity = new VkAdsLayout();
                             foreach ($adLayout as $key => $value) {
                                 $adsLayoutEntity->set($key, $value);
                             }
                             $this->getObjectManager()->persist($adsLayoutEntity);
                         }
                         unset($adLayout);
                     }
                     $this->getObjectManager()->flush();
                     break;
                 case 'ads.getRejectionReason':
                     $adRejectReason = new VkAdsRejectionReasons();
                     $adRejectReason->setId($data['ad_id']);
                     if (isset($res->comment)) {
                         $adRejectReason->setComment($res->comment);
                     }
                     if (isset($res->rules)) {
                         $adRejectReason->setRules(serialize($res->rules));
                     }
                     $this->getObjectManager()->persist($adRejectReason);
                     $this->getObjectManager()->flush();
                     break;
                 case 'ads.getAdsTargeting':
                     $filter = [];
                     $vkAds = $this->getObjectManager()->getRepository('\\Socnet\\Entity\\VkAds')->findBy(['vk_client_id' => $clientId]);
                     /**@var $ad VkAds*/
                     foreach ($vkAds as $ad) {
                         $filter[] = $ad->getId();
                     }
                     unset($vkAds);
                     /**@var $vkAdTargeting VkAdsTargeting*/
                     $vkAdsTargeting = $this->getObjectManager()->getRepository('\\Socnet\\Entity\\VkAdsTargeting')->findBy(['id' => $filter]);
                     $resultTargetingArray = [];
                     foreach ($vkAdsTargeting as $vkAdTargeting) {
                         $resultTargetingArray[$vkAdTargeting->getId()] = $vkAdTargeting;
                     }
                     unset($vkAdsTargeting);
                     foreach ($res as $targetingFromVk) {
                         if (isset($resultTargetingArray[$targetingFromVk->id])) {
                             $vkAdTargeting = $resultTargetingArray[$targetingFromVk->id];
                             if ($vkAdTargeting->getHash() != md5(serialize((array) $targetingFromVk))) {
                                 $vkAdTargeting->setHash(md5(serialize((array) $targetingFromVk)));
                                 $vkAdTargeting->setData(serialize((array) $targetingFromVk));
                                 $this->getObjectManager()->persist($vkAdTargeting);
                             }
                         } else {
                             $newVkTarget = new VkAdsTargeting();
                             $newVkTarget->setId($targetingFromVk->id);
                             $newVkTarget->setHash(md5(serialize((array) $targetingFromVk)));
                             $newVkTarget->setData(serialize((array) $targetingFromVk));
                             $this->getObjectManager()->persist($newVkTarget);
                         }
                     }
                     $this->getObjectManager()->flush();
                     break;
                 case 'ads.getSuggestions':
                     foreach ($res as $vkCountry) {
                         $country = new VkCountries();
                         $country->setId($vkCountry->id);
                         $country->setName($vkCountry->name);
                         $this->getObjectManager()->persist($country);
                     }
                     $this->getObjectManager()->flush();
                     break;
                 case 'ads.updateAds':
                     $adsForUpdate = json_decode($data['data']);
                     foreach ($adsForUpdate as $adForUpdate) {
                         foreach ($res as $adRes) {
                             if (isset($adRes->id) && !isset($adRes->error_code) && $adRes->id == $adForUpdate->ad_id) {
                                 $vkAd = $this->getObjectManager()->getRepository('\\Socnet\\Entity\\VkAds')->findBy(['id' => $adRes->id]);
                                 if ($vkAd && ($vkAd = reset($vkAd))) {
                                     if (isset($adForUpdate->status)) {
                                         // TODO!!!!
                                         $vkAd->setStatus($adForUpdate->status);
                                     }
                                     $this->getObjectManager()->persist($vkAd);
                                     break;
                                 }
                             }
                         }
                     }
                     $this->getObjectManager()->flush();
                     break;
                 case 'ads.deleteAds':
                     $adsForDelete = json_decode($data['ids']);
                     foreach ($adsForDelete as $key => $adForDelete) {
                         if (isset($res[$key]) && $res[$key] == 0) {
                             $vkAd = $this->getObjectManager()->getRepository('\\Socnet\\Entity\\VkAds')->findBy(['id' => $adForDelete]);
                             if ($vkAd && ($vkAd = reset($vkAd))) {
                                 $vkAd->setStatus(self::AD_STATUS_DELETE);
                                 $this->getObjectManager()->persist($vkAd);
                             }
                         }
                     }
                     $this->getObjectManager()->flush();
                     break;
                 case 'ads.createAds':
                     $adsForDelete = json_decode($data['ids']);
                     foreach ($adsForDelete as $key => $adForDelete) {
                         if (isset($res[$key]) && $res[$key] == 0) {
                             $vkAd = $this->getObjectManager()->getRepository('\\Socnet\\Entity\\VkAds')->findBy(['id' => $adForDelete]);
                             if ($vkAd && ($vkAd = reset($vkAd))) {
                                 $vkAd->setStatus(self::AD_STATUS_DELETE);
                                 $this->getObjectManager()->persist($vkAd);
                             }
                         }
                     }
                     $this->getObjectManager()->flush();
                     break;
             }
         } else {
         }
         unset($task);
         unset($res);
         echo '  -- task time ' . ($taskTime = microtime(true) - $start) . ' sec' . PHP_EOL;
     }
 }
コード例 #4
0
 private function saveAds(array $ads, $vkClientId = 0)
 {
     foreach ($ads as $ad) {
         $adsEntity = new VkAds();
         $adsEntity->setVkClientId($vkClientId);
         foreach ($ad as $key => $value) {
             $adsEntity->set($key, $value);
         }
         $this->getObjectManager()->persist($adsEntity);
     }
     $this->getObjectManager()->flush();
 }