/** * @param SymfonyStyle|null $io */ public function updateCode(SymfonyStyle $io = null) { $io->title('CampaignChain Data Update'); if (empty($this->versions)) { $io->warning('No code updater Service found, maybe you didn\'t enable a bundle?'); return; } $io->comment('The following data versions will be updated'); $migratedVersions = array_map(function (DataUpdateVersion $version) { return $version->getVersion(); }, $this->em->getRepository('CampaignChainUpdateBundle:DataUpdateVersion')->findAll()); $updated = false; foreach ($this->versions as $version => $class) { if (in_array($version, $migratedVersions)) { continue; } $io->section('Version ' . $class->getVersion()); $io->listing($class->getDescription()); $io->text('Begin data update'); $result = $class->execute($io); if ($result) { $dbVersion = new DataUpdateVersion(); $dbVersion->setVersion($version); $this->em->persist($dbVersion); $this->em->flush(); $io->text('Data update finished'); } $updated = true; } if (!$updated) { $io->success('All data is up to date.'); } else { $io->success('Every data version has been updated.'); } }
public function execute(SymfonyStyle $io = null) { $currentProfiles = $this->em->getRepository('CampaignChainLocationGoogleAnalyticsBundle:Profile')->findAll(); if (empty($currentProfiles)) { $io->text('There is no Profile entity to update'); return true; } foreach ($currentProfiles as $profile) { if (substr($profile->getProfileId(), 0, 2) != 'UA') { continue; } $profile->setPropertyId($profile->getProfileId()); $gaProfileUrl = $profile->getLocation()->getUrl(); $google_base_url = 'https:\\/\\/www.google.com\\/analytics\\/web\\/#report\\/visitors-overview\\/a' . $profile->getAccountId() . 'w\\d+p'; $pattern = '/' . $google_base_url . '(.*)/'; preg_match($pattern, $gaProfileUrl, $matches); if (!empty($matches) && count($matches) == 2) { $profile->setProfileId($matches[1]); $profile->setIdentifier($profile->getProfileId()); } $this->em->persist($profile); } $this->em->flush(); return true; }
/** * @param Token $newToken * @param bool $sameToken * @return bool|string */ public function setToken(Token $newToken, $sameToken = false) { if ($newToken->getLocation()) { $this->em->persist($newToken); $this->em->flush(); return true; } // Check whether the token already exists in relation to a channel. $repository = $this->em->getRepository('CampaignChainSecurityAuthenticationClientOAuthBundle:Token'); $query = $repository->createQueryBuilder('token')->where('token.application = :application')->andWhere('token.accessToken = :accessToken')->andWhere('token.location IS NOT NULL')->setParameter('application', $newToken->getApplication())->setParameter('accessToken', $newToken->getAccessToken())->setMaxResults(1)->getQuery(); $oldToken = $query->getOneOrNullResult(); if (!$oldToken) { // So, there's no token related to a specific channel, but perhaps there is one // that has been persisted at a previous attempt to connect to the channel? // TODO: Implement what to do if token was persisted previously without channel relationship? $this->token = $newToken; $this->em->persist($this->token); $this->em->flush(); return self::STATUS_NEW_TOKEN; } // Has a scope been set? if (!$newToken->getScope() && !$sameToken) { return self::STATUS_NO_CHANGE; } //With LinkedIn if a user connects multiple times, he will get the same access token //And even though they are same, it has to be saved as a new token if ($sameToken) { $this->token = $newToken; $this->em->persist($this->token); $this->em->flush(); return self::STATUS_NEW_TOKEN; } $newScope = $newToken->getScope(); $newAccessToken = $newToken->getAccessToken(); $existingScope = $oldToken->getScope(); $existingAccessToken = $oldToken->getAccessToken(); // If the channel has the same access token and the same scope, // or no scope has been defined, then we're done. if ($existingScope === $newScope) { return self::STATUS_SAME_SCOPE; } // Is the scope different for the same profile? if ($existingAccessToken !== $newAccessToken) { // If the channel has a different scope and access token, // then create a new token entry for the existing profile. // This takes care of how Google handles scopes for its APIs. $this->token = $newToken; $status = self::STATUS_NEW_TOKEN; } else { // If the channel has the same access token, but a different scope, // then just update the scope for the token. // This takes care of how Facebook deals with scope changes. $this->token = $oldToken; $this->token->setScope($newScope); $status = self::STATUS_NEW_SCOPE; } $this->em->persist($this->token); $this->em->flush(); return $status; }
public function updateBitlyAccessToken($access_token) { $activeSystem = $this->getActiveSystem(); $activeSystem->setBitlyAccessToken($access_token); $this->em->persist($activeSystem); $this->em->flush(); }
/** * @param Operation $oldOperation * @param Operation $newOperation */ public function cloneOperation(Operation $oldOperation, Operation $newOperation) { $newsItem = $this->getNewsItemByOperation($oldOperation); $clonedNewsItem = clone $newsItem; $clonedNewsItem->setOperation($newOperation); $this->em->persist($clonedNewsItem); $this->em->flush(); }
/** * @param Operation $operation * @return \Symfony\Component\HttpFoundation\Response * @throws \Exception */ public function readAction(Operation $operation) { $newsItem = $this->contentService->getNewsItemByOperation($operation); $activity = $operation->getActivity(); $locationModuleIdentifier = $activity->getLocation()->getLocationModule()->getIdentifier(); $isCompanyPageShare = 'campaignchain-linkedin-page' == $locationModuleIdentifier; $isLive = true; if (!$newsItem->getLinkedinData()) { $connection = $this->restClient->getConnectionByActivity($activity); /* * Below calls to the Linkedin REST API will throw errors if the * Linkedin App has not been approved by Linkedin as part of their * partner program. */ try { if ($isCompanyPageShare) { $response = $connection->getCompanyUpdate($activity, $newsItem); } else { $response = $connection->getUserUpdate($activity, $newsItem); } if (!is_null($response)) { $newsItem->setLinkedinData($response); $this->em->persist($newsItem); $this->em->flush(); } else { $isLive = false; } } catch (\Exception $e) { $isLive = true; } } return $this->templating->renderResponse('CampaignChainOperationLinkedInBundle::read.html.twig', array('page_title' => $activity->getName(), 'news_item' => $newsItem, 'activity' => $activity, 'is_live' => $isLive, 'is_company' => $isCompanyPageShare)); }
protected function prepareReportLocationJobs() { /** @var SchedulerReportLocation[] $scheduledReports */ $scheduledReports = $this->em->getRepository('CampaignChainCoreBundle:SchedulerReportLocation')->getScheduledReportJobsForSchedulerCommand($this->scheduler->getPeriodStart(), $this->scheduler->getPeriodEnd()); if (empty($scheduledReports)) { $this->io->text('No scheduled Location reports.'); $this->logger->info('No scheduled Location reports.'); return; } // Queue the scheduled reports. $this->io->text('Processing scheduled Location reports.'); $this->logger->info('Processing scheduled Location reports.'); foreach ($scheduledReports as $scheduledReport) { $txt = 'Report ID: ' . $scheduledReport->getId(); $this->io->section($txt); $this->logger->info($txt); $this->queueReportJob($scheduledReport); /* * Update next run. */ // Are we within the regular scheduled period? if ($scheduledReport->getEndDate() > $this->now && $scheduledReport->getInterval() != null) { $interval = \DateInterval::createFromDateString($scheduledReport->getInterval()); $nextRun = clone $scheduledReport->getNextRun(); $scheduledReport->setNextRun($nextRun->add($interval)); $txt = 'Regular period. Next run is in ' . $scheduledReport->getInterval(); $this->io->text($txt); $this->logger->info($txt); } $this->em->persist($scheduledReport); } //update the next run dates for the scheduler $this->em->flush(); }
public function schedule($operation, $facts = null) { $scheduler = new SchedulerReportOperation(); $scheduler->setOperation($operation); $scheduler->setInterval('1 hour'); $scheduler->setEndAction($operation->getActivity()->getCampaign()); $this->em->persist($scheduler); // Add initial data to report. $this->newsitem = $this->em->getRepository('CampaignChainOperationLinkedInBundle:NewsItem')->findOneByOperation($operation); if (!$this->newsitem) { throw new \Exception('No Linkedin news item found for an operation with ID: ' . $operation->getId()); } $facts[self::METRIC_LIKES] = 0; $facts[self::METRIC_COMMENTS] = 0; $this->factService->addFacts('activity', self::BUNDLE_NAME, $operation, $facts); }
/** * In case of an exception in scheduler console command * the message should be saved into the scheduler entity * * @param ConsoleExceptionEvent $event */ public function onConsoleException(ConsoleExceptionEvent $event) { /** @var SchedulerCommand $command */ $command = $event->getCommand(); if ($command->getName() != 'campaignchain:scheduler') { return; } // if scheduler is null exception happened in early stage // maybe email should be sent if (!$command->getScheduler()) { return; } /** @var Scheduler $scheduler */ $scheduler = $command->getScheduler(); $scheduler->setMessage($event->getException()->getMessage()); $scheduler->setStatus(Scheduler::STATUS_ERROR); $scheduler->setExecutionEnd(new \DateTime()); $this->em->persist($scheduler); $this->em->flush(); $command->getIo()->error($scheduler->getMessage()); $this->logger->critical($scheduler->getMessage()); }
/** * @param array $files * @param bool $doDrop */ public function load(array $files, $doDrop = true) { try { $this->em->getConnection()->beginTransaction(); $userProcessor = new UserProcessor(realpath(SystemUtil::getRootDir() . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR), $this->userService, $this->mimeTypeGuesser, $this->extensionGuesser); // Create Alice manager and fixture set $this->fixtureManager->addProcessor($userProcessor); $set = $this->fixtureManager->createFixtureSet(); // Add the fixture files foreach ($files as $file) { $set->addFile($file, 'yaml'); } $set->setDoDrop($doDrop); $set->setDoPersist(true); $set->setSeed(1337 + 42); // TODO Keep Module data intact $bundles = $this->em->getRepository("CampaignChain\\CoreBundle\\Entity\\Bundle")->findAll(); $modules = $this->em->getRepository("CampaignChain\\CoreBundle\\Entity\\Module")->findAll(); if ($this->fixtureManager->load($set)) { // TODO: Restore modules data foreach ($bundles as $bundle) { $this->em->persist($bundle); } foreach ($modules as $module) { $this->em->persist($module); } $this->em->flush(); $this->em->getConnection()->commit(); return true; } return false; } catch (\Exception $e) { $this->em->getConnection()->rollback(); $this->setException($e); return false; } }
/** * Store a module's system parameters. */ private function registerModuleSystemParams() { if (!count($this->systemParams)) { return; } /* * If a system entry already exists, then update it. Otherwise, * create a new one. */ $system = $this->systemService->getActiveSystem(); if (!$system) { $system = new System(); $system->setNavigation([]); $this->em->persist($system); } if (!is_array($system->getNavigation())) { $system->setNavigation([]); } foreach ($this->systemParams as $moduleParams) { foreach ($moduleParams as $key => $params) { switch ($key) { case 'navigation': // Does the app override the modules' navigation? if (isset($this->appComposerJson['extra']) && isset($this->appComposerJson['extra']['campaignchain']) && isset($this->appComposerJson['extra']['campaignchain']['navigation'])) { $system->setNavigation($this->appComposerJson['extra']['campaignchain']['navigation']); } else { // Merge existing navigations with new modules' navigation. $navigation = array_merge_recursive($system->getNavigation(), $params); $system->setNavigation($navigation); } break; } } } $this->em->flush(); }
/** * Global implementation of Interfaces\Persistent */ protected function persist() { Registry::persist($this); }