Skip to content

Commit

Permalink
logging done
Browse files Browse the repository at this point in the history
  • Loading branch information
miserenkov committed Dec 7, 2016
1 parent f30f6ab commit 210c461
Show file tree
Hide file tree
Showing 14 changed files with 151 additions and 206 deletions.
2 changes: 0 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,13 @@ matrix:

services:
- mongodb
- redis-server
- mysql
- postgresql

before_install:
- mysql -u root -e "CREATE DATABASE travis_test;"
- psql -c "CREATE DATABASE travis_test;" -U postgres
- pecl install -f mongodb-1.2.0
- pecl install -f redis-3.0.0
- mongo travis_test --eval 'db.addUser("travis", "test");'

install:
Expand Down
5 changes: 2 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "miserenkov/yii2-sms",
"description": "Yii2 component for sending SMS messages",
"keywords": ["yii2", "sms", "smsc", "smsc.ru", "smsc.ua"],
"type": "yii2-extension",
"type": "yii2-component",
"license": "MIT",
"support": {
"email": "[email protected]",
Expand All @@ -18,8 +18,7 @@
"require": {
"php": ">=5.5.0",
"yiisoft/yii2": "^2.0.6",
"yiisoft/yii2-mongodb": "^2.1.2",
"yiisoft/yii2-redis": "^2.0.5"
"yiisoft/yii2-mongodb": "^2.1.2"
},
"autoload": {
"psr-4": {
Expand Down
10 changes: 6 additions & 4 deletions src/Sms.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ public function send($numbers, $message, $type = self::TYPE_DEFAULT_MESSAGE)
throw new \InvalidArgumentException('For sending sms, please, set phone number and message');
}

$sms_id = $this->_client->sendMessage([
$response = $this->_client->sendMessage([
'phones' => $numbers,
'message' => $message,
'id' => $this->smsIdGenerator(),
Expand All @@ -190,21 +190,23 @@ public function send($numbers, $message, $type = self::TYPE_DEFAULT_MESSAGE)
if (is_array($numbers)) {
foreach ($numbers as $number) {
$this->_logger->setRecord([
'sms_id' => !$sms_id ? '' : $sms_id,
'sms_id' => isset($response['id']) ? $response['id'] : '',
'phone' => $number,
'message' => $message,
'error' => isset($response['error']) ? $response['error'] : 0,
]);
}
} else {
$this->_logger->setRecord([
'sms_id' => !$sms_id ? '' : $sms_id,
'sms_id' => isset($response['id']) ? $response['id'] : '',
'phone' => $numbers,
'message' => $message,
'error' => isset($response['error']) ? $response['error'] : 0,
]);
}
}

return $sms_id;
return isset($response['id']) ? $response['id'] : false;
}

/**
Expand Down
4 changes: 3 additions & 1 deletion src/client/SoapClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,12 +144,14 @@ public function sendMessage(array $params)
$response = (array) $response->sendresult;

if (!isset($response['error'])) {
return $response['id'];
return ['id' => $response['id']];
} else {
\Yii::error(SendException::getErrorString((int) $response['error']), self::class);
if ($this->_throwExceptions) {
throw new SendException((int) $response['error']);
}

return ['error' => $response['error']];
}
}

Expand Down
22 changes: 5 additions & 17 deletions src/logging/Logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,11 @@

use yii\base\InvalidConfigException;
use yii\base\Object;
use yii\db\BaseActiveRecord;
use yii\di\Instance;
use yii\db\Connection as SqlConnection;
use yii\helpers\VarDumper;
use yii\mongodb\Connection as MongoConnection;
use yii\redis\Connection as RedisConnection;
use miserenkov\sms\logging\models\Sql;
use miserenkov\sms\logging\models\Mongo;
use miserenkov\sms\logging\models\Redis;

class Logger extends Object implements LoggerInterface
{
Expand All @@ -29,13 +25,13 @@ class Logger extends Object implements LoggerInterface
public $tableName = '{{%sms_log}}';

/**
* @var array|string|\yii\db\Connection|\yii\mongodb\Connection|\yii\redis\Connection
* @var array|string|\yii\db\Connection|\yii\mongodb\Connection
*/
public $connection = null;

/**
* Log table model
* @var Mongo|Redis|Sql
* @var Mongo|Sql
*/
private $_model;

Expand All @@ -47,10 +43,8 @@ public function init()
$this->_model = Sql::class;
} elseif ($this->connection instanceof MongoConnection) {
$this->_model = Mongo::class;
} elseif ($this->connection instanceof RedisConnection) {
$this->_model = Redis::class;
} else {
throw new InvalidConfigException('');
throw new InvalidConfigException("This connections doesn't support.");
}
}

Expand All @@ -70,8 +64,6 @@ public function getConnection()
public function setRecord($data)
{
$record = new $this->_model();
$record->setDb($this->connection);
$record->setTableName($this->tableName);
foreach ($data as $key => $value) {
if ($record->hasAttribute($key)) {
$record->$key = $value;
Expand All @@ -88,10 +80,8 @@ public function updateRecordBySmsId($sms_id, $data)
{
if (!empty($sms_id)) {
$record = new $this->_model();
$record->setDb($this->connection);
$record->setTableName($this->tableName);
$record = $record->findOne(['sms_id' => $sms_id]);
if ($record instanceof BaseActiveRecord) {
if ($record) {
foreach ($data as $key => $value) {
if ($record->hasAttribute($key)) {
$record->$key = $value;
Expand All @@ -112,10 +102,8 @@ public function updateRecordBySmsIdAndPhone($sms_id, $phone, $data)
{
if (!empty($sms_id)) {
$record = new $this->_model();
$record->setDb($this->connection);
$record->setTableName($this->tableName);
$record = $record->findOne(['sms_id' => $sms_id, 'phone' => $phone]);
if ($record instanceof BaseActiveRecord) {
if ($record) {
foreach ($data as $key => $value) {
if ($record->hasAttribute($key)) {
$record->$key = $value;
Expand Down
18 changes: 2 additions & 16 deletions src/logging/models/Mongo.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,28 +13,14 @@

class Mongo extends ActiveRecord
{
public static $db;

public static $tableName;

public function setDb($connection)
{
static::$db = $connection;
}

public static function getDb()
{
return static::$db;
}

public static function setTableName($tableName)
{
static::$tableName = $tableName;
return \Yii::$app->sms->getLogger()->getConnection();
}

public static function collectionName()
{
return preg_replace('/[^A-z_]/', '', static::$tableName);
return preg_replace('/[^A-z_]/', '', \Yii::$app->sms->getLogger()->getTableName());
}


Expand Down
63 changes: 0 additions & 63 deletions src/logging/models/Redis.php

This file was deleted.

26 changes: 13 additions & 13 deletions src/logging/models/Sql.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,27 @@

class Sql extends ActiveRecord
{
public static $db;

public static $tableName;

public function setDb($connection)
{
static::$db = $connection;
}

public static function getDb()
{
return static::$db;
return \Yii::$app->sms->getLogger()->getConnection();
}

public static function tableName()
{
return static::$tableName;
return \Yii::$app->sms->getLogger()->getTableName();
}

public static function setTableName($tableName)
public function rules()
{
static::$tableName = $tableName;
return [
[['phone', 'message'], 'required'],
[['type', 'send_time', 'status', 'error'], 'integer'],
[['sms_id'], 'string', 'max' => 40],
[['phone'], 'string', 'max' => 25],
[['message'], 'string', 'max' => 800],
[['cost'], 'number'],
[['operator'], 'string', 'max' => 50],
[['region'], 'string', 'max' => 150],
];
}
}
2 changes: 1 addition & 1 deletion src/migrations/m161203_204919_init.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function safeUp()
'phone' => $this->string(25)->notNull(),
'message' => $this->string(800),
'type' => $this->smallInteger(3)->defaultValue(0),
'send_time' => $this->integer(11)->unsigned()->notNull(),
'send_time' => $this->integer(11)->unsigned(),
'cost' => $this->money(5,2)->unsigned(),
'status' => $this->smallInteger(3),
'error' => $this->smallInteger(3),
Expand Down
7 changes: 6 additions & 1 deletion tests/_data/MongoModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,14 @@

class MongoModel extends \yii\mongodb\ActiveRecord
{
public static function getDb()
{
return \Yii::$app->sms->getLogger()->getConnection();
}

public static function collectionName()
{
return 'sms_log';
return preg_replace('/[^A-z_]/', '', \Yii::$app->sms->getLogger()->getTableName());
}

public function attributes()
Expand Down
32 changes: 0 additions & 32 deletions tests/_data/RedisModel.php

This file was deleted.

Loading

0 comments on commit 210c461

Please sign in to comment.