Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
2.70% covered (danger)
2.70%
1 / 37
33.33% covered (danger)
33.33%
1 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
ApiLogicBase
2.70% covered (danger)
2.70%
1 / 37
33.33% covered (danger)
33.33%
1 / 3
388.44
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
 validateJsonProperty
0.00% covered (danger)
0.00%
0 / 18
0.00% covered (danger)
0.00%
0 / 1
156
 getAuditUserInfo
0.00% covered (danger)
0.00%
0 / 18
0.00% covered (danger)
0.00%
0 / 1
56
1<?php
2
3declare(strict_types=1);
4
5namespace PeServer\App\Models\Domain\Api;
6
7use PeServer\App\Models\AppDatabaseCache;
8use PeServer\App\Models\Dao\Domain\UserDomainDao;
9use PeServer\App\Models\Domain\DomainLogicBase;
10use PeServer\App\Models\HttpHeaderName;
11use PeServer\App\Models\IAuditUserInfo;
12use PeServer\Core\Collection\Arr;
13use PeServer\Core\DI\Inject;
14use PeServer\Core\Http\HttpRequest;
15use PeServer\Core\Mvc\LogicCallMode;
16use PeServer\Core\Mvc\LogicParameter;
17use PeServer\Core\Text;
18use PeServer\Core\Throws\NotImplementedException;
19use PeServer\Core\Utc;
20
21abstract class ApiLogicBase extends DomainLogicBase
22{
23    #region define
24
25    protected const VALIDATE_JSON_PROPERTY_TEXT = 1;
26    protected const VALIDATE_JSON_PROPERTY_NON_EMPTY_TEXT = 2;
27    protected const VALIDATE_JSON_PROPERTY_INT = 3;
28    protected const VALIDATE_JSON_PROPERTY_TIMESTAMP_UTC = 4;
29
30    #endregion
31
32    #region variable
33
34    #[Inject] //@phpstan-ignore-next-line [INJECT]
35    private HttpRequest $request;
36
37    private IAuditUserInfo|null $auditUserInfo = null;
38
39    #endregion
40
41    public function __construct(LogicParameter $parameter)
42    {
43        parent::__construct($parameter);
44    }
45
46    #region function
47
48    /**
49     * JSONプロパティ型判定処理。
50     *
51     * @param array<string,mixed> $json
52     * @param string $key
53     * @param int $validateJsonProperty
54     * @param string|null $parent
55     * @return bool
56     */
57    protected function validateJsonProperty(array $json, string $key, int $validateJsonProperty, ?string $parent = null): bool
58    {
59        if (isset($json[$key])) {
60            switch ($validateJsonProperty) {
61                case self::VALIDATE_JSON_PROPERTY_TEXT:
62                case self::VALIDATE_JSON_PROPERTY_NON_EMPTY_TEXT:
63                    if (is_string($json[$key])) {
64                        if ($validateJsonProperty === self::VALIDATE_JSON_PROPERTY_NON_EMPTY_TEXT) {
65                            return !Text::isNullOrWhiteSpace($json[$key]);
66                        }
67                        return true;
68                    }
69                    // if ($validateJsonProperty === self::VALIDATE_JSON_PROPERTY_TEXT && $json[$key] === null) { //@-phpstan-ignore-line
70                    //     return true;
71                    // }
72                    break;
73
74                case self::VALIDATE_JSON_PROPERTY_INT:
75                    if (is_int($json[$key])) {
76                        return true;
77                    }
78                    break;
79
80                case self::VALIDATE_JSON_PROPERTY_TIMESTAMP_UTC:
81                    if (is_string($json[$key])) {
82                        return Utc::tryParse($json[$key], $unused);
83                    }
84                    break;
85
86                default:
87                    break;
88            }
89        } else {
90            $errorKey = Text::isNullOrWhiteSpace($parent)
91                ? $key
92                : $parent . '.' . $key;
93            $this->addError($errorKey, 'not found property');
94        }
95
96        return false;
97    }
98
99    #endregion
100
101    #region DomainLogicBase
102
103    protected function getAuditUserInfo(): ?IAuditUserInfo
104    {
105        if ($this->auditUserInfo === null) {
106            if ($this->request->httpHeader->existsHeader(HttpHeaderName::API_KEY)) {
107                if ($this->request->httpHeader->existsHeader(HttpHeaderName::SECRET_KEY)) {
108                    $apiKeys = $this->request->httpHeader->getValues(HttpHeaderName::API_KEY);
109                    $secrets = $this->request->httpHeader->getValues(HttpHeaderName::SECRET_KEY);
110
111                    if (Arr::getCount($apiKeys) === 1 && Arr::getCount($secrets) === 1) {
112                        $apiKey = $apiKeys[0];
113                        $secret = $secrets[0];
114
115                        $database = $this->openDatabase();
116                        $userDomainDao = new UserDomainDao($database);
117                        $userId = $userDomainDao->selectUserIdFromApiKey($apiKey, $secret);
118
119                        if ($userId !== null) {
120                            $this->auditUserInfo = new class ($userId) implements IAuditUserInfo
121                            {
122                                public function __construct(private string $userId)
123                                {
124                                }
125
126                                //[IAuditUserInfo]
127
128                                public function getUserId(): string
129                                {
130                                    return $this->userId;
131                                }
132                            };
133                        }
134                    }
135                }
136            }
137        }
138
139        return $this->auditUserInfo;
140    }
141
142    #endregion
143}