|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace app\commands; |
| 4 | + |
| 5 | +use Yii; |
| 6 | +use yii\base\Exception; |
| 7 | +use yii\console\Controller; |
| 8 | +use yii\helpers\Console; |
| 9 | +use yii\helpers\ArrayHelper; |
| 10 | +use yii\httpclient\Client; |
| 11 | +use TheIconic\Tracking\GoogleAnalytics\Analytics; |
| 12 | +use app\models\Order; |
| 13 | + |
| 14 | +/** |
| 15 | + * Command for sync data from the CentralNic to Google Analytics |
| 16 | + */ |
| 17 | +class SyncGoogleController extends Controller |
| 18 | +{ |
| 19 | + public $debug; |
| 20 | + |
| 21 | + private $analytics; |
| 22 | + |
| 23 | + public function options($actionID) |
| 24 | + { |
| 25 | + return ['debug']; |
| 26 | + } |
| 27 | + |
| 28 | + public function init() |
| 29 | + { |
| 30 | + $analytics = new Analytics(true); |
| 31 | + $analytics |
| 32 | + ->setDebug(true) // (bool)$this->debug |
| 33 | + ->setProtocolVersion('1') |
| 34 | + ->setEventCategory('Domain') |
| 35 | + ->setEventAction('Create') |
| 36 | + ->setTrackingId('UA-98103468-2'); |
| 37 | + |
| 38 | + $this->analytics = $analytics; |
| 39 | + } |
| 40 | + |
| 41 | + public function actionStart() |
| 42 | + { |
| 43 | + $date = date('Y-m-d', strtotime('yesterday')); |
| 44 | + |
| 45 | + $centralNicData = $this->centralNicData($date); |
| 46 | + if (!count($centralNicData)) { |
| 47 | + $this->stdout("Empty data in CentralNic ($date)\n", Console::FG_RED); |
| 48 | + return; |
| 49 | + } |
| 50 | + |
| 51 | + $orders = $this->orders($date); |
| 52 | + if (!count($orders)) { |
| 53 | + $this->stdout("No orders ($date)\n", Console::FG_RED); |
| 54 | + return; |
| 55 | + } |
| 56 | + |
| 57 | + foreach ($orders as $order) { |
| 58 | + $registrarId = $order['registrar_id']; |
| 59 | + $domains = explode(',', $order['domains']); |
| 60 | + foreach ($domains as $domain) { |
| 61 | + $key = array_search($domain, ArrayHelper::getValue($centralNicData, [$registrarId], [])); |
| 62 | + if ($key === false) { |
| 63 | + $this->stdout( |
| 64 | + "Order №{$order['id']}: {$domain} ({$registrarId}) not found at the CentralNic\n", |
| 65 | + Console::FG_RED |
| 66 | + ); |
| 67 | + continue; |
| 68 | + } |
| 69 | + |
| 70 | + $googleClientId = ArrayHelper::getValue(unserialize($order['params']), 'googleClientId', ''); |
| 71 | + if (empty($googleClientId)) { |
| 72 | + $this->stdout("Order №{$order['id']}: has empty google client id\n", Console::FG_RED); |
| 73 | + continue; |
| 74 | + } |
| 75 | + |
| 76 | + unset($centralNicData[$registrarId][$key]); |
| 77 | + $this->sendToGoogle($order['id'], $googleClientId, $registrarId, $domain); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + $this->stdout("Done!\n", Console::FG_GREEN); |
| 82 | + } |
| 83 | + |
| 84 | + private function sendToGoogle(string $orderId, string $clientId, string $registrarId, string $domain) |
| 85 | + { |
| 86 | + $this->stdout("Order №{$orderId}: {$domain} ({$registrarId}): sent to google $clientId\n", Console::FG_YELLOW); |
| 87 | + |
| 88 | + $this->analytics |
| 89 | + ->setClientId($clientId) |
| 90 | + ->setEventLabel($domain) |
| 91 | + ->sendEvent(); |
| 92 | + } |
| 93 | + |
| 94 | + private function fetchCentralNicData(string $date): array |
| 95 | + { |
| 96 | + $client = new Client(['baseUrl' => 'https://stats.art.art']); |
| 97 | + $response = $client->get('info/t.php', [ |
| 98 | + 'created-before' => $date, |
| 99 | + 'created-after' => $date, |
| 100 | + ])->send(); |
| 101 | + |
| 102 | + if (!$response->isOk) { |
| 103 | + throw new Exception(Yii::t('app.msg', 'An error occurred while getting data from stats.art.art')); |
| 104 | + } |
| 105 | + |
| 106 | + $data = $response->getData(); |
| 107 | + $data = ArrayHelper::getValue($data, 'data', []); |
| 108 | + |
| 109 | + return $data; |
| 110 | + } |
| 111 | + |
| 112 | + private function centralNicData(string $date): array |
| 113 | + { |
| 114 | + $data = $this->fetchCentralNicData($date); |
| 115 | + |
| 116 | + $dataByRegistrar = []; |
| 117 | + foreach ($data as $item) { |
| 118 | + if ($item['type'] !== 'create') { |
| 119 | + continue; |
| 120 | + } |
| 121 | + |
| 122 | + $domain = ArrayHelper::getValue($item, 'domain.name'); |
| 123 | + $registrarId = ArrayHelper::getValue($item, 'registrar.id'); |
| 124 | + |
| 125 | + $dataByRegistrar[$registrarId][] = $domain; |
| 126 | + } |
| 127 | + |
| 128 | + return $dataByRegistrar; |
| 129 | + } |
| 130 | + |
| 131 | + private function orders(string $date): array |
| 132 | + { |
| 133 | + return Order::find() |
| 134 | + ->select(['id', 'registrar_id', 'domains', 'params']) |
| 135 | + ->where(['DATE(date_create)' => $date]) |
| 136 | + ->asArray() |
| 137 | + ->all(); |
| 138 | + } |
| 139 | +} |
0 commit comments