Created
July 10, 2019 03:46
-
-
Save nutch31/45c21053ddacaacd4d05d6ccbba50357 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 Symfony\Component\Console\Input\InputArgument; | |
use App\Service\SalesForce; | |
use Carbon\Carbon; | |
class SyncOpportunityLine extends Command | |
{ | |
protected static $defaultName = 'app:sync-opportunityline'; | |
protected $cache; | |
protected $dm; | |
protected $sfClient; | |
protected $sfObjectName = 'OpportunityLineItem'; | |
protected $sfObject; | |
protected $output; | |
protected $input; | |
protected $industries; | |
protected $forceUpdate; | |
public function __construct(AdapterInterface $cache, DocumentManager $dm) | |
{ | |
$this->cache = $cache; | |
$this->dm = $dm; | |
parent::__construct(null); | |
} | |
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 getSalesforceAlphaUserId() | |
{ | |
$username = '@salesforce'; | |
$userId = '5b4dd0a65e01bb67a63e49b2'; | |
return $userId; | |
} | |
protected function configure() | |
{ | |
$this->setDescription('Synchronizes Opportunities From SalesForce to Alpha') | |
->addArgument('force-update', InputArgument::OPTIONAL, 'Forces to update all fields (f.e. when an existing SF field is needed in Alpha)') | |
->setHelp('This command synchronizes Opportunities 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 getSfObjectsByDateRange($startDate, $endDate) | |
{ | |
$res = $this->cache->get('sF-CLI'.$this->sfObjectName, function (CacheItem $item) use ($startDate, $endDate) { | |
$item->expiresAfter(900); | |
$sfObjects = $this->sfObject->getCollectionByDateRange($startDate, $endDate, true, true); | |
$res = []; | |
foreach ($sfObjects as $sfObject) { | |
$res[$sfObject->getId()] = $sfObject; | |
} | |
return $res; | |
}); | |
return $res; | |
} | |
protected function getAlphaObjects() | |
{ | |
$repository = $this->dm->getRepository('App:'.$this->sfObjectName); | |
$alphaObjects = $repository->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(); | |
$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 ($this->forceUpdate || false === $aDate->equalTo($sfDate)) { | |
$this->update($alpha, $sf); | |
return false; | |
} | |
return true; | |
} | |
protected function addObject($sfObject) | |
{ | |
$sfObject->setCreatorId((string) $this->getSalesforceAlphaUserId()); | |
$now = new \DateTime(); | |
$sfObject->setCreateDate($now); | |
$sfObject->setLastModifiedDate($now); | |
$this->dm->persist($sfObject); | |
return; | |
} | |
protected function execute(InputInterface $input, OutputInterface $output) | |
{ | |
$this->forceUpdate = $input->getArgument('force-update') == null ? false : true; | |
$this->input = $input; | |
$this->output = $output; | |
$startDate = null; | |
$endDate = null; | |
$this->initSfClient(); | |
$sfObjects = $this->getSfObjectsByDateRange($this->sfObjectName, $startDate, $endDate); | |
$alphaObjects = $this->getAlphaObjects(); | |
$section = $output->section(); | |
$table = new Table($section); | |
$table->addRow(['Pos', 'Id', 'OpportunityId', 'Name', 'Code', '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->getOpportunityId(), $sfObject->getProductCode(), $sfObject->getName(), $op]); | |
} | |
$table->render(); | |
$this->dm->flush(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment