Created
July 9, 2019 04:04
-
-
Save nutch31/43eba1abb475ea8161901a7f18d47a88 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Command; | |
use Symfony\Component\Console\Command\Command; | |
use Symfony\Component\Console\Input\InputInterface; | |
use Symfony\Component\Console\Output\OutputInterface; | |
use Symfony\Component\Config\Definition\Exception\Exception; | |
use Symfony\Component\Cache\Adapter\AdapterInterface; | |
use Symfony\Component\Cache\CacheItem; | |
use Doctrine\ODM\MongoDB\DocumentManager; | |
use Symfony\Component\Console\Helper\Table; | |
use App\Service\SalesForce; | |
use Carbon\Carbon; | |
class SyncAccount extends Command | |
{ | |
protected static $defaultName = 'app:sync-account'; | |
protected $cache; | |
protected $dm; | |
protected $sfClient; | |
protected $sfObjectName = 'Account'; | |
protected $sfObject; | |
protected $output; | |
protected $input; | |
protected $industries; | |
public function __construct(AdapterInterface $cache, DocumentManager $dm) | |
{ | |
$this->cache = $cache; | |
$this->dm = $dm; | |
parent::__construct(null); | |
} | |
protected function getSalesforceAlphaUserId() | |
{ | |
$username = '@salesforce'; | |
$userId = '5b4dd0a65e01bb67a63e49b2'; | |
return $userId; | |
} | |
protected function fetchUniqueIndustries() | |
{ | |
$querySub = 'SELECT Sub_Industry__c FROM Account GROUP BY Sub_Industry__c'; | |
$queryMain = 'SELECT Industry FROM Account GROUP BY Industry'; | |
try { | |
$resultsSub = $this->sfClient->query($querySub); | |
$resultsMain = $this->sfClient->query($queryMain); | |
} catch (\Excpetion $e) { | |
$this->output->writeln('Error processing sub industries '.$e->getMessage().' '.$e->getCode()); | |
} | |
$businessType = $this->dm->getRepository('App:BusinessType'); | |
$industries = []; | |
foreach ($resultsSub as $row) { | |
$type = $businessType->findBy(['name' => $row['Sub_Industry__c']]); | |
if ($type) { | |
$industries[$row['Sub_Industry__c']] = $type[0]->getId(); | |
continue; | |
} | |
$industries[$row['Sub_Industry__c']] = false; | |
} | |
foreach ($resultsMain as $row) { | |
$type = $businessType->findBy(['name' => $row['Industry']]); | |
if ($type) { | |
$industries[$row['Industry']] = $type[0]->getId(); | |
continue; | |
} | |
$industries[$row['Industry']] = false; | |
} | |
$this->industries = $industries; | |
} | |
private function login() | |
{ | |
try { | |
$this->sfClient->login(); | |
} catch (Exception $e) { | |
throw $e; | |
} | |
return true; | |
} | |
private function initSfClient() | |
{ | |
$this->sfClient = new SalesForce($this->cache); | |
$sfObjectName = $this->sfClient->getObjectName($this->sfObjectName); | |
if (false == $sfObjectName) { | |
throw new Exception('Invalid Object Type', 404); | |
} | |
$this->login(); | |
try { | |
$this->sfObject = $this->sfClient->getInstanceForObject($sfObjectName); | |
} catch (Exception $e) { | |
throw $e; | |
} | |
return; | |
} | |
protected function configure() | |
{ | |
$this->setDescription('Synchronizes Accounts From SalesForce to Alpha') | |
->setHelp('This command synchronizes Account from SalesForce to Alpha.'); | |
} | |
protected function normalizeDates(&$object) | |
{ | |
$sfCreateDate = $object->getSfCreateDate(); | |
$sfLastModifiedDate = $object->getSfLastModifiedDate(); | |
$createDate = $object->getCreateDate(); | |
$LastModifiedDate = $object->getLastModifiedDate(); | |
$sfCreateDate = is_null($sfCreateDate) ? '' : $sfCreateDate->format(DATE_W3C); | |
$sfLastModifiedDate = is_null($sfLastModifiedDate) ? '' : $sfLastModifiedDate->format(DATE_W3C); | |
$createDate = is_null($createDate) ? '' : $createDate->format(DATE_W3C); | |
$LastModifiedDate = is_null($LastModifiedDate) ? '' : $LastModifiedDate->format(DATE_W3C); | |
$object->setSfCreateDate($sfCreateDate); | |
$object->setSfLastModifiedDate($sfLastModifiedDate); | |
$object->setCreateDate($createDate); | |
$object->setlastModifiedDate($LastModifiedDate); | |
} | |
protected function getSfAccountsByDateRange($startDate, $endDate) | |
{ | |
$res = $this->cache->get('sF-CLI'.$this->sfObjectName, function (CacheItem $item) use ($startDate, $endDate) { | |
$item->expiresAfter(900); | |
$sfAccounts = $this->sfObject->getCollectionByDateRange($startDate, $endDate, true, true); | |
$res = []; | |
foreach ($sfAccounts as $sfAccount) { | |
$res[$sfAccount->getId()] = $sfAccount; | |
} | |
return $res; | |
}); | |
return $res; | |
} | |
protected function getAlphaObjects() | |
{ | |
$accountRepository = $this->dm->getRepository('App:'.$this->sfObjectName); | |
$alphaObjects = $accountRepository->findAll(); | |
$alphaObjectWithId = []; | |
foreach ($alphaObjects as $object) { | |
$id = $object->getId(); | |
$this->normalizeDates($object); | |
$alphaObjectWithId[$id] = $object; | |
} | |
return $alphaObjectWithId; | |
} | |
protected function update($alpha, $sf) | |
{ | |
$sf->setLastModifiedDate((new \DateTime())); | |
$sf->setCreateDate($alpha->getLastModifiedDate()); | |
$this->dm->remove($alpha); | |
$this->dm->flush(); | |
$sf->setAccountStatusId('5c1770c1905b811c254d6132'); | |
$this->dm->persist($sf); | |
$this->dm->flush(); | |
} | |
protected function compare($alpha, $sf) | |
{ | |
$aDate = Carbon::parse($alpha->getSfLastModifiedDate()); | |
$sfDate = Carbon::parse($sf->getSfLastModifiedDate())->setTimezone('Asia/Krasnoyarsk'); | |
if (false === $aDate->equalTo($sfDate)) { | |
$this->update($alpha, $sf); | |
return false; | |
} | |
return true; | |
} | |
protected function addObject(&$sfObject) | |
{ | |
$sfObject->setAccountStatusId('5c1770c1905b811c254d6132'); | |
$industry = $sfObject->getIndustry(); | |
$subIndustry = $sfObject->getSubIndustry(); | |
if (isset($this->industries[$industry]) && false != $this->industries[$industry]) { | |
$sfObject->setBusinessTypeId((string) $this->industries[$industry]); | |
} | |
if (isset($this->industries[$subIndustry]) && false != $this->industries[$subIndustry]) { | |
$sfObject->setBusinessSubTypeId((string) $this->industries[$subIndustry]); | |
} | |
$sfObject->setCreatorId((string) $this->getSalesforceAlphaUserId()); | |
$now = (new \DateTime()); | |
$sfObject->setCreateDate($now); | |
$sfObject->setLastModifiedDate($now); | |
$this->normalizeDates($sfObject); | |
$this->dm->persist($sfObject); | |
return; | |
} | |
protected function execute(InputInterface $input, OutputInterface $output) | |
{ | |
$this->input = $input; | |
$this->output = $output; | |
$startDate = null; | |
$endDate = null; | |
$this->initSfClient(); | |
$this->fetchUniqueIndustries(); | |
$sfObjects = $this->getSfAccountsByDateRange($this->sfObjectName, $startDate, $endDate); | |
$alphaObjects = $this->getAlphaObjects(); | |
$section = $output->section(); | |
$table = new Table($section); | |
$table->addRow(['Pos', 'Id', 'Name', 'Ops']); | |
$pos = 1; | |
foreach ($sfObjects as $sfObject) { | |
$sfId = $sfObject->getId(); | |
if (isset($alphaObjects[$sfId])) { | |
$op = $this->compare($alphaObjects[$sfId], $sfObject) ? 'none' : 'Updated'; | |
} else { | |
$this->addObject($sfObject); | |
$op = 'Added'; | |
} | |
$table->addRow([$pos++, $sfId, $sfObject->getAccountCode(), $sfObject->getCustomName(), $op]); | |
} | |
$table->render(); | |
$this->dm->flush(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment