automation init...

This commit is contained in:
최준흠 2024-08-23 19:48:19 +09:00
parent 8289577de8
commit e87ce3e741
6 changed files with 170 additions and 94 deletions

View File

@ -94,5 +94,18 @@ define('EVENT_PRIORITY_NORMAL', 100);
define('EVENT_PRIORITY_HIGH', 10); define('EVENT_PRIORITY_HIGH', 10);
define('MANGBOARD', [ define('MANGBOARD', [
'level' => ['unit' => getenv('mangboard.level.unit') ?: 1000] 'point' => ['unit' => getenv('mangboard.point.unit') ?: 1000],
'admin' => ['level' => getenv('mangboard.admin.level') ?: 10],
'manager' => [
'level' => [
'min' => getenv('mangboard.manager.level.min') ?: 7,
'max' => getenv('mangboard.manager.level.max') ?: 9,
]
],
'user' => [
'level' => [
'min' => getenv('mangboard.user.level.min') ?: 1,
'max' => getenv('mangboard.user.level.max') ?: 5,
]
],
]); ]);

View File

@ -3,10 +3,24 @@
namespace App\Controllers\CLI\Mangboard; namespace App\Controllers\CLI\Mangboard;
use App\Controllers\BaseController; use App\Controllers\BaseController;
use App\Entities\Mangboard\UserEntity;
use App\Models\Mangboard\UserModel; use App\Models\Mangboard\UserModel;
use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\HTTP\ResponseInterface;
use Psr\Log\LoggerInterface;
class UserController extends BaseController class UserController extends BaseController
{ {
private $_model = null;
public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger)
{
// Do Not Edit This Line
parent::initController($request, $response, $logger);
// Preload any models, libraries, etc, here.
// E.g.: $this->session = \Config\Services::session();
$this->_model = new UserModel();
}
public function index() public function index()
{ {
// //
@ -15,14 +29,14 @@ class UserController extends BaseController
{ {
$msg = []; $msg = [];
try { try {
$userModel = new UserModel();
$entity = $userModel->getEntity($uid); $entity = $this->_model->getEntity($uid);
if (!$entity) { if (!$entity) {
throw new \Exception("해당 회원[{$uid}]이 없습니다."); throw new \Exception("해당 회원[{$uid}]이 없습니다.");
} }
$old_point = $entity->getPoint(); $old_point = $entity->getPoint();
$entity->setPoint($point, $sign); $entity->setPoint($point, $sign);
$entity = $userModel->setEntity($entity); $entity = $this->_model->setEntity($entity);
$msg[] = "[{$entity}] 회원님의 포인트는 {$old_point}->{$entity->getPoint()} 입니다."; $msg[] = "[{$entity}] 회원님의 포인트는 {$old_point}->{$entity->getPoint()} 입니다.";
} catch (\Exception $e) { } catch (\Exception $e) {
$msg[] = $e->getMessage(); $msg[] = $e->getMessage();
@ -34,14 +48,12 @@ class UserController extends BaseController
{ {
$msg = []; $msg = [];
try { try {
$userModel = new UserModel(); $entity = $this->_model->getEntity($uid);
$entity = $userModel->getEntity($uid);
if (!$entity) { if (!$entity) {
throw new \Exception("해당 회원[{$uid}]이 없습니다."); throw new \Exception("해당 회원[{$uid}]이 없습니다.");
} }
$old_level = $entity->getLevel(); $old_level = $entity->getLevel();
$entity->setLevel(MANGBOARD['level']['unit']); $entity = $this->_model->setLevel($entity);
$entity = $userModel->setEntity($entity);
$msg[] = "[{$entity}] 회원님의 레벨은 {$old_level}->{$entity->getLevel()} 입니다."; $msg[] = "[{$entity}] 회원님의 레벨은 {$old_level}->{$entity->getLevel()} 입니다.";
} catch (\Exception $e) { } catch (\Exception $e) {
$msg[] = $e->getMessage(); $msg[] = $e->getMessage();

View File

@ -14,41 +14,32 @@ class UserEntity extends Entity
{ {
return "{$this->getName()}"; return "{$this->getName()}";
} }
public function getUserID()
{
return $this->attributes['user_id'];
}
public function getName() public function getName()
{ {
return $this->attributes['user_name']; return $this->attributes['user_name'];
} }
public function getPoint() public function getPoint()
{ {
return $this->attributes['user_point']; return $this->attributes['user_point'];
} }
public function setPoint(int $point, string $sign = "plus") public function setPoint(int $point)
{ {
switch (strtolower($sign)) {
case 'minus': $this->attributes['user_point'] = $point;
if ($this->attributes['user_point'] < $point) {
throw new \Exception("기존포인트:{$this->attributes['user_point']}가 감소 포인트:-{$point} 작습니다.\n");
}
$this->attributes['user_point'] -= $point;
break;
case 'plus':
$this->attributes['user_point'] += $point;
break;
default:
throw new \Exception("{$sign}에 해당하는 작업은 수행할수 없습니다.\n");
// break;
}
} }
public function getLevel() public function getLevel()
{ {
return $this->attributes['user_level']; return $this->attributes['user_level'];
} }
public function setLevel(int $level_unit) public function setLevel(int $level)
{ {
$level = intval($this->getPoint() / $level_unit * $level_unit / $level_unit); $this->attributes['user_level'] = $level;
if ($this->attributes['user_level'] != $level) {
$this->attributes['user_level'] = $level;
}
} }
} }

View File

@ -0,0 +1,66 @@
<?php
namespace App\Models\Mangboard;
use CodeIgniter\Model;
class BaseModel extends Model
{
protected $table = '';
protected $primaryKey = '';
protected $useAutoIncrement = true;
protected $returnType = 'array';
protected $useSoftDeletes = false;
protected $protectFields = true;
protected $allowedFields = [];
protected bool $allowEmptyInserts = false;
protected bool $updateOnlyChanged = true;
protected array $casts = [];
protected array $castHandlers = [];
// Dates
protected $useTimestamps = false;
protected $dateFormat = 'datetime';
protected $createdField = 'created_at';
protected $updatedField = 'updated_at';
protected $deletedField = 'deleted_at';
// Validation
protected $validationRules = [];
protected $validationMessages = [];
protected $skipValidation = false;
protected $cleanValidationRules = true;
// Callbacks
protected $allowCallbacks = true;
protected $beforeInsert = [];
protected $afterInsert = [];
protected $beforeUpdate = [];
protected $afterUpdate = [];
protected $beforeFind = [];
protected $afterFind = [];
protected $beforeDelete = [];
protected $afterDelete = [];
protected function getEntity(string $uid)
{
return $this->asObject($this->returnType)->where($this->primaryKey, $uid)->first();
}
protected function setEntity($entity)
{
if ($entity->hasChanged()) {
if (!$this->save($entity)) {
throw new \Exception(__FUNCTION__ . " 오류 발생.\n" . var_export($this->errors(), true));
}
}
return $entity;;
}
protected function getEntitys(): array
{
return $this->asObject($this->returnType)->findAll();
}
}

View File

@ -3,31 +3,16 @@
namespace App\Models\Mangboard; namespace App\Models\Mangboard;
use App\Entities\Mangboard\UserEntity; use App\Entities\Mangboard\UserEntity;
use CodeIgniter\Model;
class UserModel extends Model class UserModel extends BaseModel
{ {
protected $table = 'mb_users'; protected $table = 'mb_users';
protected $primaryKey = 'pid'; protected $primaryKey = 'pid';
protected $useAutoIncrement = true; protected $useAutoIncrement = true;
protected $returnType = 'array'; // protected $returnType = 'array';
protected $useSoftDeletes = false; protected $returnType = UserEntity::class;
protected $protectFields = true;
protected $allowedFields = ['pid', 'user_id', 'passwd', 'user_name', 'user_email', 'user_state', 'user_level', 'user_point']; protected $allowedFields = ['pid', 'user_id', 'passwd', 'user_name', 'user_email', 'user_state', 'user_level', 'user_point'];
protected bool $allowEmptyInserts = false;
protected bool $updateOnlyChanged = true;
protected array $casts = [];
protected array $castHandlers = [];
// Dates
protected $useTimestamps = false;
protected $dateFormat = 'datetime';
protected $createdField = 'created_at';
protected $updatedField = 'updated_at';
protected $deletedField = 'deleted_at';
// Validation // Validation
// protected $validationRules = []; // protected $validationRules = [];
protected $validationRules = [ protected $validationRules = [
@ -46,37 +31,46 @@ class UserModel extends Model
// 'updated_at' => 'if_exist|valid_date', // 'updated_at' => 'if_exist|valid_date',
// 'created_at' => 'if_exist|valid_date', // 'created_at' => 'if_exist|valid_date',
]; ];
protected $validationMessages = [];
protected $skipValidation = false;
protected $cleanValidationRules = true;
// Callbacks public function setPoint(UserEntity $entity, int $point, $sign = '+'): UserEntity
protected $allowCallbacks = true;
protected $beforeInsert = [];
protected $afterInsert = [];
protected $beforeUpdate = [];
protected $afterUpdate = [];
protected $beforeFind = [];
protected $afterFind = [];
protected $beforeDelete = [];
protected $afterDelete = [];
public function getEntity(string $uid): null|UserEntity
{ {
return $this->asObject(UserEntity::class)->where($this->primaryKey, $uid)->first(); switch ($sign) {
} case '-':
public function setEntity(UserEntity $entity): UserEntity if ($entity->getPoint() < $point) {
{ throw new \Exception("기존포인트:{$entity->getPoint()}가 감소 포인트:-{$point} 작습니다.\n");
if ($entity->hasChanged()) { }
if (!$this->save($entity)) { $entity->setPoint($entity->getPoint() - $point);
throw new \Exception(__FUNCTION__ . " 오류 발생.\n" . var_export($this->errors(), true)); break;
} case '+':
$entity->setPoint($entity->getPoint() + $point);
break;
default:
throw new \Exception("{$sign}에 해당하는 작업은 수행할수 없습니다.\n");
// break;
} }
return $entity;; return $this->setEntity($entity);
} }
public function getEntitys(): array private function getLevelByPoint(UserEntity $entity): int
{ {
return $this->asObject(UserEntity::class)->findAll(); return intval($entity->getPoint() / MANGBOARD['point']['unit'] * MANGBOARD['point']['unit'] / MANGBOARD['point']['unit']);
}
final public function setLevel(UserEntity $entity): UserEntity
{
//관리자면 변경불가
if ($entity->getUserID() == MANGBOARD['admin']['level']) {
return $entity;
}
//운영자면 변경불가
if (MANGBOARD['manager']['level']['min'] <= $entity->getLevel() <= MANGBOARD['manager']['level']['max']) {
return $entity;
}
//사용자 Point별 Level 계산
$level = $this->getLevelByPoint($entity);
//사용자 Level 1~5;
$level = $level < MANGBOARD['user']['level']['min'] ? MANGBOARD['user']['level']['min'] : $level;
$level = MANGBOARD['user']['level']['max'] < $level ? MANGBOARD['user']['level']['max'] : $level;
$entity->setLevel($level);
return $this->setEntity($entity);
} }
} }

44
composer.lock generated
View File

@ -144,16 +144,16 @@
}, },
{ {
"name": "psr/log", "name": "psr/log",
"version": "3.0.0", "version": "3.0.1",
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/php-fig/log.git", "url": "https://github.com/php-fig/log.git",
"reference": "fe5ea303b0887d5caefd3d431c3e61ad47037001" "reference": "79dff0b268932c640297f5208d6298f71855c03e"
}, },
"dist": { "dist": {
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/php-fig/log/zipball/fe5ea303b0887d5caefd3d431c3e61ad47037001", "url": "https://api.github.com/repos/php-fig/log/zipball/79dff0b268932c640297f5208d6298f71855c03e",
"reference": "fe5ea303b0887d5caefd3d431c3e61ad47037001", "reference": "79dff0b268932c640297f5208d6298f71855c03e",
"shasum": "" "shasum": ""
}, },
"require": { "require": {
@ -188,9 +188,9 @@
"psr-3" "psr-3"
], ],
"support": { "support": {
"source": "https://github.com/php-fig/log/tree/3.0.0" "source": "https://github.com/php-fig/log/tree/3.0.1"
}, },
"time": "2021-07-14T16:46:02+00:00" "time": "2024-08-21T13:31:24+00:00"
} }
], ],
"packages-dev": [ "packages-dev": [
@ -546,32 +546,32 @@
}, },
{ {
"name": "phpunit/php-code-coverage", "name": "phpunit/php-code-coverage",
"version": "10.1.15", "version": "10.1.16",
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/sebastianbergmann/php-code-coverage.git", "url": "https://github.com/sebastianbergmann/php-code-coverage.git",
"reference": "5da8b1728acd1e6ffdf2ff32ffbdfd04307f26ae" "reference": "7e308268858ed6baedc8704a304727d20bc07c77"
}, },
"dist": { "dist": {
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/sebastianbergmann/php-code-coverage/zipball/5da8b1728acd1e6ffdf2ff32ffbdfd04307f26ae", "url": "https://api.github.com/repos/sebastianbergmann/php-code-coverage/zipball/7e308268858ed6baedc8704a304727d20bc07c77",
"reference": "5da8b1728acd1e6ffdf2ff32ffbdfd04307f26ae", "reference": "7e308268858ed6baedc8704a304727d20bc07c77",
"shasum": "" "shasum": ""
}, },
"require": { "require": {
"ext-dom": "*", "ext-dom": "*",
"ext-libxml": "*", "ext-libxml": "*",
"ext-xmlwriter": "*", "ext-xmlwriter": "*",
"nikic/php-parser": "^4.18 || ^5.0", "nikic/php-parser": "^4.19.1 || ^5.1.0",
"php": ">=8.1", "php": ">=8.1",
"phpunit/php-file-iterator": "^4.0", "phpunit/php-file-iterator": "^4.1.0",
"phpunit/php-text-template": "^3.0", "phpunit/php-text-template": "^3.0.1",
"sebastian/code-unit-reverse-lookup": "^3.0", "sebastian/code-unit-reverse-lookup": "^3.0.0",
"sebastian/complexity": "^3.0", "sebastian/complexity": "^3.2.0",
"sebastian/environment": "^6.0", "sebastian/environment": "^6.1.0",
"sebastian/lines-of-code": "^2.0", "sebastian/lines-of-code": "^2.0.2",
"sebastian/version": "^4.0", "sebastian/version": "^4.0.1",
"theseer/tokenizer": "^1.2.0" "theseer/tokenizer": "^1.2.3"
}, },
"require-dev": { "require-dev": {
"phpunit/phpunit": "^10.1" "phpunit/phpunit": "^10.1"
@ -583,7 +583,7 @@
"type": "library", "type": "library",
"extra": { "extra": {
"branch-alias": { "branch-alias": {
"dev-main": "10.1-dev" "dev-main": "10.1.x-dev"
} }
}, },
"autoload": { "autoload": {
@ -612,7 +612,7 @@
"support": { "support": {
"issues": "https://github.com/sebastianbergmann/php-code-coverage/issues", "issues": "https://github.com/sebastianbergmann/php-code-coverage/issues",
"security": "https://github.com/sebastianbergmann/php-code-coverage/security/policy", "security": "https://github.com/sebastianbergmann/php-code-coverage/security/policy",
"source": "https://github.com/sebastianbergmann/php-code-coverage/tree/10.1.15" "source": "https://github.com/sebastianbergmann/php-code-coverage/tree/10.1.16"
}, },
"funding": [ "funding": [
{ {
@ -620,7 +620,7 @@
"type": "github" "type": "github"
} }
], ],
"time": "2024-06-29T08:25:15+00:00" "time": "2024-08-22T04:31:57+00:00"
}, },
{ {
"name": "phpunit/php-file-iterator", "name": "phpunit/php-file-iterator",