Advertisement
Guest User

Untitled

a guest
Jul 7th, 2015
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.03 KB | None | 0 0
  1. <?php namespace App\Console\Commands;
  2.  
  3. use Illuminate\Console\Command;
  4. use Illuminate\Foundation\Inspiring;
  5. use ET_Client;
  6. use ET_DataExtension;
  7. use ET_DataExtension_Row;
  8. use App\MasterPreference;
  9. use App\LineLevelPreference;
  10. use Crypt;
  11. use Exception;
  12. use PhpSpec\Exception\Example\ExampleException;
  13.  
  14. class migrateDownDE extends Command {
  15.  
  16.     protected $name = 'migrateDownDE:slnc';
  17.  
  18.     protected $description = 'This command migrates data from the Exact Target Data extensions for Preference Center to this local Laravel Instance';
  19.  
  20.     /**
  21.      * Create a new command instance.
  22.      */
  23.     public function __construct()
  24.     {
  25.         //
  26.         parent::__construct();
  27.  
  28.     }
  29.  
  30.     /**
  31.      * calls in the EtApi
  32.      *
  33.      * @return mixed
  34.      */
  35.     public function etConnect()
  36.     {
  37.         return \App::make('App\EtApi');
  38.     }
  39.  
  40.     public function updateCreateDelete($data, $whereColumn=null, $model)
  41.     {
  42.         $values = array();
  43.  
  44.         $all = $model->all();
  45.  
  46.         //data within the Laravel app
  47.         $localSearchArray = array();
  48.  
  49.         //the data coming from Exact Target
  50.         $remoteSearchArray = array();
  51.  
  52.         foreach ($all as $k => $v)
  53.         {
  54.             $localSearchArray[] = $v->$whereColumn;
  55.         }
  56.  
  57.         foreach ($data->results as $v)
  58.         {
  59.             foreach ($v->Properties->Property as $val)
  60.             {
  61.                 if ($val->Name == $whereColumn)
  62.                 {
  63.                     $remoteSearchArray[] = $val->Value;
  64.                 }
  65.                 $values[$val->Name] = $val->Value;
  66.             }
  67.  
  68.             //check the current preferences table for this record
  69.             //if it exists update it -- if not insert it
  70.             $custName = $values[$whereColumn];
  71.  
  72.             //run a select where query and set boolean if it exists
  73.             $model->updateOrCreate([$whereColumn => $custName], $values);
  74.         }
  75.  
  76.         //persist deletions to local databaase
  77.         $deletions = array_diff($localSearchArray, $remoteSearchArray);
  78.  
  79.         if ($deletions)
  80.         {
  81.             foreach ($deletions as $k => $v)
  82.             {
  83.                 $model->where($whereColumn, '=', $v)->delete();
  84.             }
  85.         }
  86.  
  87.         return 'successful update no deletions';
  88.     }
  89.  
  90.     /**
  91.      * Execute the command.
  92.      *
  93.      * @return void
  94.      */
  95.     public function handle(MasterPreference $mPref, LineLevelPreference $lPref)
  96.     {
  97.  
  98.         //array of data extension names to get rows for
  99.         $DataExtensions = [
  100.             'MasterPreferences',
  101.             'SubPreferences'
  102.         ];
  103.  
  104.         //fire off main method -- update, create, or persist deletions
  105.         foreach ($DataExtensions as $k => $v)
  106.         {
  107.             $data = $this->etConnect()->getRows($v);
  108.  
  109.             if ($k == 0)
  110.             {
  111.                 $this->updateCreateDelete($data, 'customer_id', $mPref);
  112.             }
  113.             else if ($k == 1)
  114.             {
  115.                 $this->updateCreateDelete($data, 'phone_number', $lPref);
  116.             }
  117.         }
  118.     }
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement