diff --git a/.gitignore b/.gitignore index cb818a1..a385a3b 100644 --- a/.gitignore +++ b/.gitignore @@ -4,4 +4,4 @@ Homestead.json Homestead.yaml .env .phpunit.result.cache -/node_modules \ No newline at end of file +/node_modules diff --git a/app/Console/Commands/SyncCustomers.php b/app/Console/Commands/SyncCustomers.php new file mode 100644 index 0000000..f0ca3e7 --- /dev/null +++ b/app/Console/Commands/SyncCustomers.php @@ -0,0 +1,114 @@ +error('Customers synchronisation is globally disabled.'); + return null; + } + + + $this->line('Starting synchronisation'); + $customers = $tasks = $contacts = []; + + // Getting active customers + $opts = [ + CURLOPT_POST => true, + CURLOPT_RETURNTRANSFER => true, + CURLOPT_FOLLOWLOCATION => false, + CURLOPT_FAILONERROR => true, + CURLOPT_POSTFIELDS => [ + 'access' => env('CMS_API_ACCESS'), + 'token' => env('CMS_API_TOKEN') + ], + CURLOPT_URL => env('CMS_API_URL') + ]; + + $ch = curl_init(); + curl_setopt_array($ch, $opts); + if ($result = curl_exec($ch)) { + $customers = json_decode($result); + + $bar = $this->output->createProgressBar(count($customers)); + $bar->start(); + + + // Getting existing tasks + $query = app('db')->select('SELECT * FROM tasks'); + foreach ($query as $t) { + $tasks[$t->id] = preg_replace('~^https?://~', '', $t->host); + } + + // Getting existing contacts + $contacts = app('db')->select('SELECT * FROM contacts'); + + // First we insert new customers + foreach($customers as $c) { + $bar->advance(); + if (false === $key = array_search($c->domain, $tasks)) { + $ret = app('db')->insert(' + INSERT INTO tasks (`host`, `type`, `params`, `creation_date`, `frequency`, `active`, `group_id`) + VALUES(:host, :type, :params, :creation_date, :frequency, :active, :group_id) + ', [ + 'host' => 'https://'.$c->domain, + 'type' => 'http', + 'params' => 'propulsé par', + 'creation_date' => date('Y-m-d H:i:s'), + 'frequency' => 600, + 'active' => 1, + 'group_id' => $c->id + ]); + } + } + + // Then we delete old customers + foreach ($tasks as $t) { + + } + } + } +} + diff --git a/app/Console/Kernel.php b/app/Console/Kernel.php index ad6e311..c879242 100644 --- a/app/Console/Kernel.php +++ b/app/Console/Kernel.php @@ -4,6 +4,7 @@ namespace App\Console; use Illuminate\Console\Scheduling\Schedule; use Laravel\Lumen\Console\Kernel as ConsoleKernel; +use App\Console\Commands\SyncCustomers; class Kernel extends ConsoleKernel { @@ -13,7 +14,7 @@ class Kernel extends ConsoleKernel * @var array */ protected $commands = [ - // + SyncCustomers::class ]; /** @@ -24,6 +25,12 @@ class Kernel extends ConsoleKernel */ protected function schedule(Schedule $schedule) { - // + /** + * This is for my own needs + * You may safely remove this scheduled task + */ + if (env('CMS_ENABLE_SYNC') == true) { + $schedule->command('customers:sync')->everyMinute(); + } } }