Created
September 17, 2019 20:50
-
-
Save Jlaird/c89e768066869ff0ca60e58e8e4690bf to your computer and use it in GitHub Desktop.
This file contains 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 Cygnus\ApplicationBundle\Command; | |
use Cygnus\ModlrBundle\Api\ModlrApi; | |
use Cygnus\ApplicationBundle\Utils\Helpers\ModelHierarchy; | |
use Cygnus\PlatformBundle\Kernel\PlatformKernel; | |
use Cygnus\ModlrBundle\Component\Utility; | |
use Aws\CloudSearchDomain\CloudSearchDomainClient; | |
use Algolia\AlgoliaSearch\SearchClient as SearchClient; | |
use Cygnus\ApplicationBundle\Utils\Helpers\Content as ContentHelper; | |
use \DateTime; | |
/** | |
* | |
* Post import cleanup commands to tidy up data | |
* | |
*/ | |
class CloudSearchPopulate extends AbstractCommand | |
{ | |
/** | |
* @var ConfigValueLoader | |
*/ | |
private $config; | |
/** | |
* {@inheritdoc} | |
*/ | |
protected function executeStart() | |
{ | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
protected function executeEnd() | |
{ | |
} | |
/** | |
* Class constructor. This must be called in extending classes and must call parent::__construct() | |
* | |
* @param ModlrApi $modlrApi The Modlr Api | |
* @param PlatformKernel $kernel The PlatformKernel | |
*/ | |
public function __construct(ModlrApi $modlrApi, PlatformKernel $kernel) | |
{ | |
$this->api = $modlrApi; | |
$this->kernel = $kernel; | |
parent::__construct(); | |
$config = $this->kernel->getContext()->getConfig()->getValues(); | |
$this->host = $config->get('display.media_domain'); | |
$this->algoliaClient = SearchClient::create( | |
); | |
} | |
/** | |
* Post import cleanup commands to tidy up data | |
* | |
* Arguments: SubCommand, Imported, Total, Type, ID (all optional) | |
*/ | |
protected function configure() | |
{ | |
parent::configure(); | |
$this | |
->setName('cygnus_application:cloud_search_populate') | |
->setDescription('Populate CloudSearch') | |
; | |
} | |
/** | |
* Post import cleanup commands to tidy up data (nfl/nfn) | |
*/ | |
public function doCommandCloudSearchPopulate() | |
{ | |
$this->writeln('Start Processing'); | |
$this->indent(); | |
$counter = function ($this){ | |
return $this->getPersistence('Platform\Content')->count([]); | |
}; | |
$retriever = function ($this, $limit, $skip) { | |
return $this->getPersistence('Platform\Content')->query([], [], [], $limit, $skip); | |
}; | |
// $counter = function ($this){ | |
// return $this->getPersistence('Platform\Content')->count(['_id' => 10037796]); | |
// }; | |
// $retriever = function ($this, $limit, $skip) { | |
// return $this->getPersistence('Platform\Content')->query(['_id' => 10037796], [], [], $limit, $skip); | |
// }; | |
$modifier = function ($this, &$item, &$formatted) { | |
// Delete the old item | |
// $formatted['delete'][] = ['type' => 'delete', 'id' => strval($item->getId())]; | |
// Proccess each item | |
$formatted['add'][] = $this->proccessItem($item); | |
}; | |
$persister = function ($this, $formatted) { | |
$this->writeln('Save'); | |
//dvar_dump($this->batchUpdateCloudSearch($formatted['delete'])); | |
//var_dump($formatted); | |
var_dump($this->batchUpdateAlgolia($formatted['add'])); | |
// $this->getModelManager()->flush(); | |
$this->getModelManager()->clear(); | |
}; | |
$this->loop($counter, $retriever, $modifier, $persister, 'Content', 500); | |
$this->writeln(sprintf('Ending %s Migration', 'Content')); | |
$this->outdent(); | |
} | |
/** | |
* Batch Update Cloud Search | |
* @param object $item | |
*/ | |
public function batchUpdateAlgolia($formatted) | |
{ | |
$respone = $this->algoliaClient->multipleBatch($formatted); | |
return $respone; | |
} | |
/** | |
* Responsible for getting the Image size and setting it. | |
* @param object $item | |
*/ | |
public function proccessItem($item) | |
{ | |
// $this->writeln(sprintf('Processing %s', $item->getId())); | |
return $this->format($item); | |
} | |
private function format($item) | |
{ | |
$data = []; | |
$data['action'] = 'addObject'; | |
$data['indexName'] = 'prod_base_content'; | |
$fields = []; | |
$fields['objectID'] = $item->getId(); | |
$fields['url'] = ContentHelper::getWebsiteUrl($item); | |
$fields['Sites'] = $this->host; | |
$fields['domain'] = sprintf('https://www.%s', $this->host); | |
if (null !== $item->getStatus()) | |
$fields['status'] = $item->getStatus(); | |
if (null !== $item->getNameWebsite()) | |
$fields['name'] = $item->getNameWebsite(); | |
if (null !== $item->getTeaserWebsite()) | |
$fields['teaser'] = $item->getTeaserWebsite(); | |
if (null !== strip_tags($item->getbodyWebsite())) { | |
$fields['body'] = strip_tags($item->getbodyWebsite()); | |
$fields['body'] = preg_replace('/[^A-Za-z0-9\-\'";. ]/', '', $fields['body']); | |
} | |
$modified = $item->getUpdated() | |
? $item->getUpdated() | |
: $item->getCreated(); | |
$modified = ($modified->getTimestamp() < 0) | |
? new DateTime("1970-01-01T00:00:00.000Z") | |
: $modified; | |
$fields['modified'] = $modified->getTimestamp(); | |
if (null !== $item->getCreated()) | |
$fields['created'] = $item->getCreated()->getTimestamp(); | |
if (null !== $item->getPublished()) | |
$fields['published'] = $item->getPublished()->getTimestamp(); | |
$fields = $this->getWebsiteSchedules($item, $fields); | |
$fields = $this->getMagazineSchedules($item, $fields); | |
$fields = $this->proccessImage($item, $fields); | |
$fields['Type'] = $item->getType(); | |
// $fields = $this->getCategoies($item, $fields); | |
// die(); | |
$data['body'] = $fields; | |
return $data; | |
} | |
/** | |
* Loop over featured schudles | |
*/ | |
private function getWebsiteSchedules($item, $fields) | |
{ | |
$schedules = $item->getSchedulesWebsite(); | |
$fields['Topics'] = []; | |
foreach ($schedules as $schedule) { | |
$sections = ModelHierarchy::getModelHierarchyModels($schedule->getSection()); | |
$sectionObject = []; | |
$counter = count($sections); | |
foreach ($sections as $section) { | |
$counter--; | |
$key = sprintf('lev%s', $counter); | |
if (!array_key_exists($key, $fields['Topics'])) { | |
$fields['Topics'][$key] = []; | |
} | |
if(!in_array($section->getFullName(), $fields['Topics'][$key])) { | |
$fields['Topics'][$key][] = $section->getFullName(); | |
} | |
} | |
} | |
return $fields; | |
} | |
private function getMagazineSchedules($item, $fields) | |
{ | |
$fields['Publications'] = []; | |
foreach($item->getSchedulesMagazine() as $schedule) { | |
if (!in_array($schedule->getProduct()->getName(), $fields['Publications'])) { | |
$fields['Publications'][] = $schedule->getProduct()->getName(); | |
} | |
} | |
return $fields; | |
} | |
public function proccessImage($item, $fields) | |
{ | |
if (NULL !== $item->getPrimaryImage()) { | |
$fields['primaryimage'] = sprintf('https://base.imgix.net/%s/%s', | |
$item->getPrimaryImage()->getFilePath(), | |
$item->getPrimaryImage()->getFileName()); | |
} | |
return $fields; | |
} | |
/** | |
* Figures out where the source file exists and returns the file name. | |
* @param object $item | |
* @return boolean returns fales if it can't find the file and the name if it does. | |
*/ | |
public function getSourceImage($item) | |
{ | |
$filePath = sprintf('%s/original/%s', $item->getFilePath(), $item->getSource()->getName()); | |
$sourceFile = $this->s3Client->doesObjectExist('media.cygnus.com', $filePath); | |
if ($sourceFile) { | |
return true; | |
} | |
$name = substr($item->getFileName(), 0, strrpos($item->getFileName(), '.')); | |
$ext = pathinfo($item->getSource()->getName(), PATHINFO_EXTENSION); | |
$filePath = sprintf('%s/original/%s.%s', $item->getFilePath(), $name, $ext); | |
$sourceFile = $this->s3Client->doesObjectExist('media.cygnus.com', $filePath); | |
if ($sourceFile) { | |
$item->getSource()->setName(sprintf('%s.%s', $name, $ext)); | |
return true; | |
} | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment