Created
April 4, 2017 11:19
-
-
Save princealikhan/0d33f99963515674f2ff4a184ad77eaa to your computer and use it in GitHub Desktop.
Sync Mautic User To Laravel/Lumen Application Artisan Command
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 | |
use Illuminate\Database\Schema\Blueprint; | |
use Illuminate\Database\Migrations\Migration; | |
class CreateMauticUser extends Migration { | |
/** | |
* Run the migrations. | |
* | |
* @return void | |
*/ | |
public function up() | |
{ | |
Schema::create('mautic_users', function(Blueprint $table) | |
{ | |
$table->integer('id', true); | |
$table->integer('user_id'); | |
$table->integer('mautic_id'); | |
$table->integer('status')->nullable(); | |
$table->timestamps(); | |
}); | |
} | |
/** | |
* Reverse the migrations. | |
* | |
* @return void | |
*/ | |
public function down() | |
{ | |
Schema::drop('mautic_users'); | |
} | |
} |
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\Console\Commands\Marketing\Sync; | |
use Illuminate\Console\Command; | |
use Princealikhan\Mautic\Facades\Mautic; | |
use App\Models\User; | |
use App\Models\MauticUser; | |
use Symfony\Component\Console\Helper\ProgressBar; | |
use Symfony\Component\Console\Output\ConsoleOutput; | |
use Symfony\Component\Console\Input\InputOption; | |
use Symfony\Component\Console\Input\InputArgument; | |
class SyncUser extends Command { | |
/** | |
* The console command name. | |
* | |
* @var string | |
*/ | |
protected $name = 'marketing:sync'; | |
/** | |
* The console command description. | |
* | |
* @var string | |
*/ | |
protected $description = 'Sync User to Mautic User.'; | |
/** | |
* Create a new command instance. | |
* | |
* @return void | |
*/ | |
public function __construct() | |
{ | |
parent::__construct(); | |
} | |
/** | |
* Execute the console command. | |
* | |
* @return mixed | |
*/ | |
public function fire() | |
{ | |
$users = User::leftJoin('mautic_users', 'users.id', '=', 'mautic_users.user_id') | |
->whereNull('mautic_users.user_id') | |
->select(['users.id','username','email','confirmed']) | |
->get() | |
->toArray(); | |
$output = new ConsoleOutput(); | |
$progress = new ProgressBar($output, count($users)); | |
$progress->start(); | |
$arguments = $this->argument(); | |
foreach ($users as $key => $user) | |
{ | |
$data = array( | |
'userid' => $user['id'], | |
'firstname' => $user['username'], | |
'email' => $user['email'], | |
'status' => $user['confirmed'], | |
'company' => 'company' | |
); | |
$mauticUserRaw = Mautic::request('POST','contacts/new',$data); | |
$mautiUserId = $mauticUserRaw['contact']['id']; | |
MauticUser::create([ | |
'user_id'=>$user['id'], | |
'mautic_id' => $mautiUserId, | |
'status'=> $user['confirmed'] | |
]); | |
$endpoint = 'segments/'.$arguments['segment_id'].'/contact/add/'.$mautiUserId; | |
Mautic::request('POST',$endpoint); | |
$table[] = array( | |
'user_id' => $user['id'], | |
'name' => $user['username'], | |
'email' => $user['email'], | |
'status' => $user['confirmed'] | |
); | |
$progress->advance(); | |
} | |
$progress->finish(); | |
if(count($users) > 0){ | |
echo "\n"; | |
$headers = ['User ID','Username', 'Email','Status']; | |
$this->table($headers, $table); | |
echo "\n"; | |
$this->info("Here the list of users pushed to Marketing App."); | |
}else{ | |
echo "\n"; | |
$this->info("There notihing to sync."); | |
} | |
} | |
/** | |
* Get the console command arguments. | |
* | |
* @return array | |
*/ | |
protected function getArguments() | |
{ | |
return array( | |
array('segment_id', InputArgument::REQUIRED, 'The name of the migration'), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment