Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
82.14% covered (warning)
82.14%
23 / 28
50.00% covered (danger)
50.00%
3 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
DomainLogicBase
82.14% covered (warning)
82.14%
23 / 28
50.00% covered (danger)
50.00%
3 / 6
10.57
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 openDatabase
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setResponseJson
83.33% covered (warning)
83.33%
5 / 6
0.00% covered (danger)
0.00%
0 / 1
2.02
 getAuditUserInfo
n/a
0 / 0
n/a
0 / 0
0
 writeAuditLogCore
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
2
 writeAuditLogCurrentUser
66.67% covered (warning)
66.67%
4 / 6
0.00% covered (danger)
0.00%
0 / 1
2.15
 writeAuditLogTargetUser
50.00% covered (danger)
50.00%
2 / 4
0.00% covered (danger)
0.00%
0 / 1
2.50
1<?php
2
3declare(strict_types=1);
4
5namespace PeServer\App\Models\Domain;
6
7use PeServer\App\Models\AppDatabase;
8use PeServer\App\Models\Dao\Entities\UserAuditLogsEntityDao;
9use PeServer\App\Models\IAuditUserInfo;
10use PeServer\App\Models\ResponseJson;
11use PeServer\Core\Collection\Arr;
12use PeServer\Core\Database\DatabaseContext;
13use PeServer\Core\Database\IDatabaseConnection;
14use PeServer\Core\Database\IDatabaseContext;
15use PeServer\Core\DI\Inject;
16use PeServer\Core\Mime;
17use PeServer\Core\Mvc\LogicBase;
18use PeServer\Core\Mvc\LogicParameter;
19use PeServer\Core\Serialization\ISerializer;
20use PeServer\Core\Serialization\Json;
21use PeServer\Core\Serialization\JsonSerializer;
22use PeServer\Core\Serialization\SerializerBase;
23use PeServer\Core\Text;
24
25abstract class DomainLogicBase extends LogicBase
26{
27    #[Inject] //@phpstan-ignore-next-line [INJECT]
28    private IDatabaseConnection $databaseConnection;
29
30    public function __construct(LogicParameter $parameter)
31    {
32        parent::__construct($parameter);
33    }
34
35    /**
36     * データベース接続処理。
37     *
38     * @return DatabaseContext
39     */
40    protected function openDatabase(): DatabaseContext
41    {
42        //return AppDatabase::open($this->logger);
43        return $this->databaseConnection->open();
44    }
45
46    protected function setResponseJson(ResponseJson $responseJson): void
47    {
48        $result = [
49            'data' => $responseJson->data,
50        ];
51        if ($responseJson->error !== null) {
52            $result['error'] = $responseJson->error;
53        }
54
55        $this->setJsonContent($result);
56    }
57
58    /**
59     * 監査ログ用ユーザー情報の取得。
60     *
61     * ドメインロジックで明示的に使用しない想定。
62     *
63     * @return IAuditUserInfo|null
64     */
65    abstract protected function getAuditUserInfo(): ?IAuditUserInfo;
66
67    /**
68     * 監査ログ出力内部処理。
69     *
70     * @param string $userId
71     * @param string $event
72     * @param mixed|null $info
73     * @param IDatabaseContext|null $context
74     * @return int
75     */
76    private function writeAuditLogCore(string $userId, string $event, mixed $info, ?IDatabaseContext $context, ?ISerializer $serializer = null): int
77    {
78        $ipAddress = $this->stores->special->getServer('REMOTE_ADDR');
79        $userAgent = $this->stores->special->getServer('HTTP_USER_AGENT');
80        $dumpInfo = Text::EMPTY;
81        if ($info !== null) {
82            $serializer ??= new JsonSerializer();
83
84            $dumpInfo = strval($serializer->save($info));
85        }
86
87        $db = $context ?? $this->openDatabase();
88        $userAuditLogsEntityDao = new UserAuditLogsEntityDao($db);
89        $userAuditLogsEntityDao->insertLog($userId, $event, $dumpInfo, $ipAddress, $userAgent);
90        return $userAuditLogsEntityDao->selectLastLogId();
91    }
92
93    /**
94     * 現在ログインユーザーの監査ログ出力。
95     *
96     * ※DBじゃなくてテキストファイルでいいかも
97     *
98     * @param string $event
99     * @param mixed $info
100     * @param IDatabaseContext|null $context
101     * @return int
102     */
103    protected function writeAuditLogCurrentUser(string $event, mixed $info = null, ?IDatabaseContext $context = null): int
104    {
105        $userInfo = $this->getAuditUserInfo();
106        if ($userInfo === null) {
107            $this->logger->error('監査ログ ユーザー情報取得失敗のため書き込み中止');
108            return -1;
109        }
110
111        $userId = $userInfo->getUserId();
112
113        return $this->writeAuditLogCore($userId, $event, $info, $context);
114    }
115
116    /**
117     * 対象ユーザーとして監査ログ出力。
118     *
119     * @param string $userId
120     * @param string $event
121     * @param array<mixed>|null $info
122     * @param IDatabaseContext|null $context
123     * @return int
124     */
125    protected function writeAuditLogTargetUser(string $userId, string $event, ?array $info = null, ?IDatabaseContext $context = null): int
126    {
127        if (Text::isNullOrWhiteSpace($userId)) {
128            $this->logger->error('監査ログ ユーザーID不正のため書き込み中止');
129            return -1;
130        }
131
132        return $this->writeAuditLogCore($userId, $event, $info, $context);
133    }
134}