Skip to content

Commit 9edecf1

Browse files
author
grinderspro
committed
Search Yii project
1 parent 163e3bc commit 9edecf1

6 files changed

Lines changed: 618 additions & 0 deletions

File tree

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
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+
}

yii-search/models/ArtResource.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
namespace app\modules\search\models;
4+
5+
class ArtResource
6+
{
7+
public static $search = 'http://xxx.xxx.xxx.xxx/art/api/search';
8+
public static $stat = 'http://xxx.xxx.xxx.xxx/art/api/stat';
9+
}

yii-search/models/ArtSource.php

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
<?php
2+
3+
namespace app\modules\search\models;
4+
5+
use \app\components\httpclient\HttpClientInterface;
6+
use yii\helpers\ArrayHelper;
7+
8+
class ArtSource
9+
{
10+
private $client;
11+
12+
public function __construct(HttpClientInterface $client)
13+
{
14+
$this->client = $client;
15+
}
16+
17+
public function searchDomains($name, $limit, $offset)
18+
{
19+
$result = $this->client->get(ArtResource::$search, [
20+
'name' => $name,
21+
'limit' => $limit,
22+
'offset' => $offset,
23+
]);
24+
25+
if ($result) {
26+
return $this->parseDomains($result);
27+
}
28+
29+
return $result;
30+
}
31+
32+
public function searchAchievements($name, $params, $similar)
33+
{
34+
$result = $this->client->get(ArtResource::$stat, [
35+
'name' => $name,
36+
'params' => $params,
37+
'similar' => $similar,
38+
]);
39+
40+
if ($result) {
41+
return $this->parseAchievements($result);
42+
}
43+
44+
return $result;
45+
}
46+
47+
private function parseAchievements($result)
48+
{
49+
$achievements = ArrayHelper::getValue($result, 'data', []);
50+
$achievements = array_filter($achievements);
51+
52+
if (isset($achievements['busy1']) && isset($achievements['busy2'])) {
53+
if ($achievements['busy1'] > $achievements['busy2']) {
54+
unset($achievements['busy2']);
55+
} else {
56+
unset($achievements['busy1']);
57+
}
58+
}
59+
60+
if (isset($achievements['arty']) && $achievements['arty'] > 0) {
61+
$achievements['arty'] = $achievements['arty'] + 6;
62+
$achievements['arty'] = $achievements['arty'] > 10 ? 10 : $achievements['arty'];
63+
}
64+
65+
$achievements = ['early' => 10] + $achievements;
66+
67+
return $achievements;
68+
}
69+
70+
private function parseDomains($result)
71+
{
72+
$likeDomains = [];
73+
74+
$data = ArrayHelper::getValue($result, 'like', []);
75+
foreach ($data as $item) {
76+
foreach ($item as $domain) {
77+
$likeDomains[$domain['name']] = $this->parseDomain($domain);
78+
}
79+
}
80+
81+
$withDomains = [];
82+
83+
$data = ArrayHelper::getValue($result, 'with', []);
84+
foreach ($data as $domain) {
85+
$withDomains[$domain['name']] = $this->parseDomain($domain);
86+
}
87+
88+
$domains = ArrayHelper::merge($likeDomains, $withDomains);
89+
90+
return $domains;
91+
}
92+
93+
private function parseDomain($item)
94+
{
95+
return [
96+
'name' => $item['name'],
97+
'price' => $item['price'] * 1.2,
98+
'weight' => $item['weight'],
99+
];
100+
}
101+
}
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
<?php
2+
3+
namespace app\modules\search\models;
4+
5+
use yii\helpers\ArrayHelper;
6+
use \app\components\eppclient\EppClientInterface;
7+
8+
class CentralNicSource
9+
{
10+
private $client;
11+
12+
public function __construct(EppClientInterface $client)
13+
{
14+
$this->client = $client;
15+
}
16+
17+
public function check($names)
18+
{
19+
$names = $this->prepare($names);
20+
$result = $this->client->check($names);
21+
22+
if ($result) {
23+
return $this->parseResult($result);
24+
}
25+
26+
return $result;
27+
}
28+
29+
private function parseResult($result)
30+
{
31+
$data = [];
32+
33+
$result = ArrayHelper::getValue($result, ['chkData', 'cd'], []);
34+
if (isset($result['name'])) {
35+
$result = [$result];
36+
}
37+
38+
foreach ($result as $item) {
39+
if (is_array($item['name'])) {
40+
$item['name'] = array_shift($item['name']);
41+
}
42+
43+
$item['name'] = str_replace('.art', '', $item['name']);
44+
45+
$data[$item['name']]['name'] = $item['name'];
46+
47+
$available = $this->parseavailable($item);
48+
if ($available !== null) {
49+
$data[$item['name']]['available'] = $available;
50+
}
51+
52+
//Premium
53+
$premium = $this->parsePremium($item);
54+
if ($premium !== null) {
55+
$data[$item['name']]['premium'] = $premium;
56+
}
57+
58+
$price = $this->parsePrice($item);
59+
if ($price !== null) {
60+
$data[$item['name']]['price'] = $price;
61+
}
62+
}
63+
64+
return $data;
65+
}
66+
67+
private function parseavailable($item)
68+
{
69+
$available = ArrayHelper::getValue($item, ['@name', 'avail']);
70+
if ($available !== null) {
71+
return (bool)$available;
72+
}
73+
74+
return null;
75+
}
76+
77+
private function parsePrice($item)
78+
{
79+
$prices = ArrayHelper::getValue($item, ['fee']);
80+
if (is_array($prices)) {
81+
$price = array_shift($prices);
82+
if ($price >= 10) {
83+
return $price * 1.2;
84+
}
85+
return $price * 2;
86+
}
87+
88+
return null;
89+
}
90+
91+
/**
92+
* Parce premium domain
93+
*
94+
* Is the domain premium?
95+
*
96+
* @param $item Array
97+
* @return int|null
98+
*/
99+
private function parsePremium($item)
100+
{
101+
$premium = null;
102+
103+
if(!empty($item['class']))
104+
$item['class'] == 'premium' ? $premium = 1 : $premium = 0;
105+
106+
return $premium;
107+
}
108+
109+
private function prepare($names)
110+
{
111+
foreach ($names as $k => $name) {
112+
$names[$k] = $name . '.art';
113+
}
114+
115+
return $names;
116+
}
117+
}

yii-search/models/DomainPrefix.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace app\modules\search\models;
4+
5+
/**
6+
* ActiveRecord for table "domain_prefix"
7+
*
8+
* @property integer $id
9+
* @property string $title
10+
*/
11+
class DomainPrefix extends \yii\db\ActiveRecord
12+
{
13+
/**
14+
* @inheritdoc
15+
* @internal
16+
*/
17+
public static function tableName()
18+
{
19+
return 'domain_prefix';
20+
}
21+
}

0 commit comments

Comments
 (0)