<?php
namespace App\Service;

use Carbon\Carbon;
use Incoviba\common\Registry as RModel;

class Register
{
  public static function log($user, $action, $variables)
  {
    $data = [
      'user' => $user,
      'action' => $action,
      'time' => Carbon::now(config('app.timezone'))->toDateTimeString()//->format('Y-m-d HH:mm:ss')
    ];
    $registry = model(RModel::class)
      ->where('user', $user)
      ->where('action', $action)
      ->where('time', $data['time'])
      ->findOne();
    if (!$registry) {
      $registry = model(RModel::class)->create($data);
      $registry->save();
    }
    foreach ($variables as $data) {
      $data['registry'] = $registry->id;
      $log = (new Factory(RegistryData::class))->where($data)->find();
      if (!$log) {
        $log = model(RegistryData::class)->create($data);
        $log->save();
      }
    }
  }
}
