Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php namespace App\Console\Commands;
- use Illuminate\Console\Command;
- use Illuminate\Foundation\Inspiring;
- use ET_Client;
- use ET_DataExtension;
- use ET_DataExtension_Row;
- use App\MasterPreference;
- use App\LineLevelPreference;
- use Crypt;
- use Exception;
- use PhpSpec\Exception\Example\ExampleException;
- class migrateDownDE extends Command {
- protected $name = 'migrateDownDE:slnc';
- protected $description = 'This command migrates data from the Exact Target Data extensions for Preference Center to this local Laravel Instance';
- /**
- * Create a new command instance.
- */
- public function __construct()
- {
- //
- parent::__construct();
- }
- /**
- * calls in the EtApi
- *
- * @return mixed
- */
- public function etConnect()
- {
- return \App::make('App\EtApi');
- }
- public function updateCreateDelete($data, $whereColumn=null, $model)
- {
- $values = array();
- $all = $model->all();
- //data within the Laravel app
- $localSearchArray = array();
- //the data coming from Exact Target
- $remoteSearchArray = array();
- foreach ($all as $k => $v)
- {
- $localSearchArray[] = $v->$whereColumn;
- }
- foreach ($data->results as $v)
- {
- foreach ($v->Properties->Property as $val)
- {
- if ($val->Name == $whereColumn)
- {
- $remoteSearchArray[] = $val->Value;
- }
- $values[$val->Name] = $val->Value;
- }
- //check the current preferences table for this record
- //if it exists update it -- if not insert it
- $custName = $values[$whereColumn];
- //run a select where query and set boolean if it exists
- $model->updateOrCreate([$whereColumn => $custName], $values);
- }
- //persist deletions to local databaase
- $deletions = array_diff($localSearchArray, $remoteSearchArray);
- if ($deletions)
- {
- foreach ($deletions as $k => $v)
- {
- $model->where($whereColumn, '=', $v)->delete();
- }
- }
- return 'successful update no deletions';
- }
- /**
- * Execute the command.
- *
- * @return void
- */
- public function handle(MasterPreference $mPref, LineLevelPreference $lPref)
- {
- //array of data extension names to get rows for
- $DataExtensions = [
- 'MasterPreferences',
- 'SubPreferences'
- ];
- //fire off main method -- update, create, or persist deletions
- foreach ($DataExtensions as $k => $v)
- {
- $data = $this->etConnect()->getRows($v);
- if ($k == 0)
- {
- $this->updateCreateDelete($data, 'customer_id', $mPref);
- }
- else if ($k == 1)
- {
- $this->updateCreateDelete($data, 'phone_number', $lPref);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement