Skip to content

Commit

Permalink
LoggerBehavior deprecated logScope, logIssuer, logMessageBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
nojimage committed Jan 24, 2019
1 parent 5081dd6 commit 927414b
Show file tree
Hide file tree
Showing 4 changed files with 252 additions and 49 deletions.
121 changes: 102 additions & 19 deletions src/Model/Behavior/LoggerBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,6 @@ public function implementedEvents()
];
}

/**
* @return array
*/
public function implementedMethods()
{
return parent::implementedMethods() + [
'activityLog' => 'log',
];
}

/**
* Table.initializeの後に実行
*
Expand Down Expand Up @@ -136,19 +126,24 @@ public function afterDelete(Event $event, Entity $entity, ArrayObject $options)
$this->saveLogs($logs);
}

/**
* ログスコープの取得
*
* @return array
*/
public function getLogScope()
{
return $this->getConfig('scope');
}

/**
* ログスコープの設定
*
* @param mixed $args if $args === false リセット
* @return Table
* @return void
*/
public function logScope($args = null)
public function setLogScope($args)
{
if ($args === null) {
// getter
return $this->getConfig('scope');
}

if ($args === false) {
// reset
$this->setConfig('scope', $this->getConfig('originalScope'), false);
Expand All @@ -168,15 +163,60 @@ public function logScope($args = null)
}
$this->setConfig('scope', $scope);
}
}

/**
* ログスコープの設定
*
* @param mixed $args if $args === false リセット
* @return Table|array
* @deprecated 1.2.0 use setLogScope()/getLogScope() instead.
*/
public function logScope($args = null)
{
if ($args === null) {
// getter
return $this->getLogScope();
}

$this->setLogScope($args);

return $this->_table;
}

/**
* ログ発行者の取得
*
* @return array
*/
public function getLogIssuer()
{
return $this->getConfig('issuer');
}

/**
* ログ発行者の設定
*
* @param Entity $issuer the issuer
* @return void
*/
public function setLogIssuer(Entity $issuer)
{
$this->setConfig('issuer', $issuer);

// scopeに含む場合、併せてscopeにセット
list($issuerModel, $issuerId) = $this->buildObjectParameter($this->getConfig('issuer'));
if (array_key_exists($issuerModel, $this->getConfig('scope'))) {
$this->setLogScope($issuer);
}
}

/**
* ログ発行者の設定
*
* @param Entity $issuer the issuer
* @return Table
* @deprecated 1.2.0 use setLogIssuer()/getLogIssuer() instead.
*/
public function logIssuer(Entity $issuer = null)
{
Expand All @@ -190,17 +230,39 @@ public function logIssuer(Entity $issuer = null)
// scopeに含む場合、併せてscopeにセット
list($issuerModel, $issuerId) = $this->buildObjectParameter($this->getConfig('issuer'));
if (array_key_exists($issuerModel, $this->getConfig('scope'))) {
$this->logScope($issuer);
$this->setLogScope($issuer);
}

return $this->_table;
}

/**
* メッセージ生成メソッドの取得
*
* @return callable|null
*/
public function getLogMessageBuilder()
{
return $this->getConfig('messageBuilder');
}

/**
* メッセージ生成メソッドの設定
*
* @param callable $handler the message build method
* @return void
*/
public function setLogMessageBuilder(callable $handler = null)
{
$this->setConfig('messageBuilder', $handler);
}

/**
* メッセージ生成メソッドの設定
*
* @param callable $handler the message build method
* @return callable
* @deprecated 1.2.0 use setLogMessageBuilder()/getLogMessageBuilder() instead.
*/
public function logMessageBuilder(callable $handler = null)
{
Expand All @@ -227,7 +289,7 @@ public function logMessageBuilder(callable $handler = null)
* ]
* @return ActivityLog[]|array
*/
public function log($level, $message, array $context = [])
public function activityLog($level, $message, array $context = [])
{
$entity = !empty($context['object']) ? $context['object'] : null;
$issuer = !empty($context['issuer']) ? $context['issuer'] : $this->getConfig('issuer');
Expand All @@ -253,6 +315,27 @@ public function log($level, $message, array $context = [])
return $logs;
}

/**
* カスタムログの記述
*
* @param string $level log level
* @param string $message log message
* @param array $context context data
* [
* 'object' => Entity,
* 'issuer' => Entity,
* 'scope' => Entity[],
* 'action' => string,
* 'data' => array,
* ]
* @return ActivityLog[]|array
* @deprecated 1.2.0 use activityLog() instead.
*/
public function log($level, $message, array $context = [])
{
return $this->activityLog($level, $message, $context);
}

/**
* アクティビティログの取得
*
Expand Down
2 changes: 2 additions & 0 deletions src/Model/Behavior/LoggerBehaviorCompletion.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

/**
* LoggerBehaviorのメソッド補完
*
* @deprecated since 1.2.0, use @mixin annotation
*/
// @codingStandardsIgnoreStart
trait LoggerBehaviorCompletion// @codingStandardsIgnoreEnd
Expand Down
127 changes: 121 additions & 6 deletions tests/TestCase/Model/Behavior/LoggerBehaviorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,12 @@ public function testInitialization()
$this->assertSame([
'Elastic/ActivityLogger.Authors' => null,
'\MyApp' => true,
], $this->Authors->logScope(), 'システムスコープがセットされている');
], $this->Authors->getLogScope(), 'システムスコープがセットされている');
$this->assertSame([
'Elastic/ActivityLogger.Authors' => null,
'Elastic/ActivityLogger.Articles' => null,
'Elastic/ActivityLogger.Users' => null,
], $this->Comments->logScope(), 'systemScope = false ならばシステムスコープはセットされない');
], $this->Comments->getLogScope(), 'systemScope = false ならばシステムスコープはセットされない');
$this->markTestIncomplete('Not cover all');
}

Expand Down Expand Up @@ -199,6 +199,58 @@ public function testLogScope()
], $this->Articles->logScope(), 'ログのスコープがリセットされている');
}

public function testLogScopeSetterGetter()
{
$this->assertSame([
'Elastic/ActivityLogger.Authors' => null,
'\MyApp' => true,
], $this->Authors->getLogScope(), 'ログのスコープが取得できる');
//
$this->assertSame([
'Elastic/ActivityLogger.Articles' => null,
'Elastic/ActivityLogger.Authors' => null,
'\MyApp' => true,
], $this->Articles->getLogScope(), 'ログのスコープが取得できる');

// セットして取得
$author = $this->Authors->get(1);
$this->Authors->setLogScope($author);
$this->assertSame([
'Elastic/ActivityLogger.Authors' => 1,
'\MyApp' => true,
], $this->Authors->getLogScope(), 'ログのスコープが更新されている');
//
$article = $this->Articles->get(2);
$this->Articles->setLogScope([$article, $author]);
$this->assertSame([
'Elastic/ActivityLogger.Articles' => 2,
'Elastic/ActivityLogger.Authors' => 1,
'\MyApp' => true,
], $this->Articles->getLogScope(), 'ログのスコープが取得できる');

// スコープの追加
$this->Articles->setLogScope($this->Comments->get(3));
$this->Articles->setLogScope('Custom');
$this->Articles->setLogScope(['Another' => 4, 'Foo' => '005', 'Hoge']);
$this->assertSame([
'Elastic/ActivityLogger.Articles' => 2,
'Elastic/ActivityLogger.Authors' => 1,
'\MyApp' => true,
'Elastic/ActivityLogger.Comments' => 3,
'Custom' => true,
'Another' => 4,
'Foo' => '005',
'Hoge' => true,
], $this->Articles->getLogScope(), 'ログのスコープがセットされている');
// スコープのリセット
$this->Articles->setLogScope(false);
$this->assertSame([
'Elastic/ActivityLogger.Articles' => null,
'Elastic/ActivityLogger.Authors' => null,
'\MyApp' => true,
], $this->Articles->getLogScope(), 'ログのスコープがリセットされている');
}

public function testSaveWithScope()
{
$author = $this->Authors->newEntity([
Expand All @@ -225,7 +277,7 @@ public function testSaveWithScope()
'user_id' => $user->id,
'comment' => 'Awesome!',
]);
$this->Comments->logScope([$article, $user]);
$this->Comments->setLogScope([$article, $user]);
$this->Comments->save($comment);

$logs = $this->ActivityLogs->find()
Expand Down Expand Up @@ -273,7 +325,7 @@ public function testSaveWithScopeMap()
public function testSaveWithIssuer()
{
$user = $this->Users->get(1);
$this->Authors->logIssuer($user);
$this->Authors->setLogIssuer($user);
$author = $this->Authors->newEntity([
'username' => 'foo',
'password' => 'bar',
Expand All @@ -292,8 +344,8 @@ public function testSaveWithIssuer()
'user_id' => $user->id,
'comment' => 'Awesome!',
]);
$this->Comments->logIssuer($user);
$this->Comments->logScope($article);
$this->Comments->setLogIssuer($user);
$this->Comments->setLogScope($article);
$this->Comments->save($comment);

$logs = $this->ActivityLogs->find()
Expand Down Expand Up @@ -426,6 +478,69 @@ public function testLogMessageBuilder()
$this->assertSame('nate が記事 #4「バージョン1.0 stableリリース」を削除しました。', $logs[3]->message);
}

public function testLogMessageBuilderSetterGetter()
{
$this->assertNull($this->Articles->getLogMessageBuilder());
//
$this->Articles->setLogMessageBuilder(function (ActivityLog $log, array $context) {
if (!empty($log->message)) {
return $log->message;
}

$message = '';
$object = $context['object'] ?: null;
$issuer = $context['issuer'] ?: null;
switch ($log->action) {
case ActivityLog::ACTION_CREATE:
$message = sprintf('%3$s が記事 #%1$s「%2$s」を作成しました。', $object->id, $object->title, $issuer->username);
break;
case ActivityLog::ACTION_UPDATE:
$message = sprintf('%3$s が記事 #%1$s「%2$s」を更新しました。', $object->id, $object->title, $issuer->username);
break;
case ActivityLog::ACTION_DELETE:
$message = sprintf('%3$s が記事 #%1$s「%2$s」を削除しました。', $object->id, $object->title, $issuer->username);
break;
default:
break;
}

return $message;
});

// 記事を新規作成
$author = $this->Authors->get(1);
$article = $this->Articles->newEntity([
'title' => 'バージョン1.0リリース',
'body' => '新しいバージョン 1.0 をリリースしました。',
'author' => $author,
]);
$this->Articles->setLogIssuer($author);
$this->Articles->save($article);

// 記事を更新
$article->title = 'バージョン1.0 stableリリース';
$this->Articles->save($article);

// 任意のログ
$this->Articles->activityLog(LogLevel::NOTICE, '記事を更新しています。');

// 別のユーザーが削除
$this->Articles->setLogIssuer($this->Authors->get(2));
$this->Articles->delete($article);

$logs = $this->ActivityLogs->find()
->where(['scope_model' => 'Elastic/ActivityLogger.Authors'])
->order(['id' => 'asc'])
->all()
->toArray();

$this->assertCount(4, $logs);
$this->assertSame('mariano が記事 #4「バージョン1.0リリース」を作成しました。', $logs[0]->message);
$this->assertSame('mariano が記事 #4「バージョン1.0 stableリリース」を更新しました。', $logs[1]->message);
$this->assertSame('記事を更新しています。', $logs[2]->message);
$this->assertSame('nate が記事 #4「バージョン1.0 stableリリース」を削除しました。', $logs[3]->message);
}

public function testFindActivity()
{
$author = $this->Authors->get(1);
Expand Down
Loading

0 comments on commit 927414b

Please sign in to comment.