Skip to content

Commit 455f79f

Browse files
authored
Merge pull request #16 from niden-code/1.x
Video #8
2 parents e479e99 + b35db43 commit 455f79f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+2323
-314
lines changed

.github/workflows/main.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,11 @@ jobs:
9393
MYSQL_DATABASE: phalcon
9494
MYSQL_PASSWORD: secret
9595

96+
redis:
97+
image: redis:8-alpine
98+
ports:
99+
- "6379:6379"
100+
96101
steps:
97102
- uses: actions/checkout@v4
98103

config/.env.ci

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
1+
# Docker prefix
12
PROJECT_NAME="rest"
23

4+
# Environment
5+
APP_ENV_ADAPTER="dotenv"
6+
APP_ENV_FILE_PATH="./"
7+
38
# Mariadb
49
DB_HOST="127.0.0.1"
510
DB_PORT=3306
@@ -8,9 +13,21 @@ DB_PASS="secret"
813
DB_NAME="phalcon"
914
DB_CHARSET="utf8"
1015

11-
# Redis
12-
DATA_REDIS_HOST="app-cache"
13-
DATA_REDIS_PORT=6379
14-
DATA_REDIS_NAME="0"
16+
# Cache
17+
CACHE_ADAPTER="redis"
18+
CACHE_HOST="127.0.0.1"
19+
CACHE_PORT=6379
20+
CACHE_INDEX="0"
21+
CACHE_LIFETIME=86400
22+
CACHE_PREFIX="-rest-"
23+
24+
# Logger
25+
LOG_FILENAME="rest-api"
26+
LOG_PATH="storage/logs"
1527

28+
# Code Coverage
1629
XDEBUG_MODE=coverage
30+
31+
# JWT
32+
TOKEN_EXPIRATION=14400
33+
TOKEN_AUDIENCE="https://rest-api.phalcon.io"

config/.env.example

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,33 @@
1+
# Docker prefix
12
PROJECT_NAME="rest"
23

4+
# Environment
5+
APP_ENV_ADAPTER="dotenv"
6+
APP_ENV_FILE_PATH="./"
7+
38
# Mariadb
4-
DB_HOST="app-db"
9+
DB_HOST="rest-db"
510
DB_PORT=3306
611
DB_USER="root"
712
DB_PASS="secret"
813
DB_NAME="phalcon"
9-
DB_CHARSET= "utf8"
14+
DB_CHARSET="utf8"
15+
16+
# Cache
17+
CACHE_ADAPTER="redis"
18+
CACHE_HOST="rest-cache"
19+
CACHE_PORT=6379
20+
CACHE_INDEX="0"
21+
CACHE_LIFETIME=86400
22+
CACHE_PREFIX="-rest-"
1023

11-
# Redis
12-
DATA_REDIS_HOST="app-cache"
13-
DATA_REDIS_PORT=6379
14-
DATA_REDIS_NAME="0"
24+
# Logger
25+
LOG_FILENAME="rest-api"
26+
LOG_PATH="storage/logs"
1527

28+
# Code Coverage
1629
XDEBUG_MODE=coverage
30+
31+
# JWT
32+
TOKEN_EXPIRATION=14400
33+
TOKEN_AUDIENCE="https://rest-api.phalcon.io"

phpunit.xml.dist

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
bootstrap="./phpunit.php"
55
cacheDirectory="tests/_output/.phpunit.result.cache"
66
colors="true"
7-
processIsolation="true"
87
>
98
<testsuites>
109
<testsuite name="unit tests">

src/Action/ActionHandler.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,15 @@
1515

1616
use Phalcon\Api\Domain\ADR\DomainInterface;
1717
use Phalcon\Api\Domain\ADR\Input;
18+
use Phalcon\Api\Domain\ADR\InputTypes;
1819
use Phalcon\Api\Responder\ResponderInterface;
1920
use Phalcon\Http\RequestInterface;
2021
use Phalcon\Http\ResponseInterface;
2122

23+
/**
24+
* @phpstan-import-type TLoginInput from InputTypes
25+
* @phpstan-import-type TUserInput from InputTypes
26+
*/
2227
final readonly class ActionHandler implements ActionInterface
2328
{
2429
public function __construct(
@@ -32,6 +37,7 @@ public function __construct(
3237
public function __invoke(): void
3338
{
3439
$input = new Input();
40+
/** @var TLoginInput|TUserInput $data */
3541
$data = $input->__invoke($this->request);
3642

3743
$this->responder->__invoke(

src/Domain/ADR/DomainInterface.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@
1616
use Phalcon\Domain\Payload;
1717

1818
/**
19-
* @phpstan-import-type THelloInput from InputTypes
19+
* @phpstan-import-type TLoginInput from InputTypes
2020
* @phpstan-import-type TUserInput from InputTypes
2121
*/
2222
interface DomainInterface
2323
{
2424
/**
25-
* @param THelloInput|TUserInput $input
25+
* @param TLoginInput|TUserInput $input
2626
*
2727
* @return Payload
2828
*/

src/Domain/ADR/Input.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515

1616
use Phalcon\Http\RequestInterface;
1717

18+
use function array_merge;
19+
1820
/**
1921
* @phpstan-import-type TRequestQuery from InputTypes
2022
*/
@@ -29,7 +31,9 @@ public function __invoke(RequestInterface $request): array
2931
{
3032
/** @var TRequestQuery $query */
3133
$query = $request->getQuery();
34+
/** @var TRequestQuery $post */
35+
$post = $request->getPost();
3236

33-
return $query;
37+
return array_merge($query, $post);
3438
}
3539
}

src/Domain/ADR/InputTypes.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@
1414
namespace Phalcon\Api\Domain\ADR;
1515

1616
/**
17-
* @phpstan-type THelloInput array{}
17+
* @phpstan-type TLoginInput array{
18+
* email?: string,
19+
* password?: string
20+
* }
1821
* @phpstan-type TUserInput array{
1922
* userId?: int
2023
* }
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
<?php
2+
3+
/**
4+
* This file is part of the Phalcon API.
5+
*
6+
* (c) Phalcon Team <team@phalcon.io>
7+
*
8+
* For the full copyright and license information, please view
9+
* the LICENSE file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace Phalcon\Api\Domain\Components\Cache;
15+
16+
use DateTimeImmutable;
17+
use Phalcon\Api\Domain\Components\Constants\Dates;
18+
use Phalcon\Api\Domain\Components\DataSource\User\UserTransport;
19+
use Phalcon\Api\Domain\Components\Env\EnvManager;
20+
use Phalcon\Cache\Cache as PhalconCache;
21+
use Psr\SimpleCache\InvalidArgumentException;
22+
23+
use function sha1;
24+
25+
class Cache extends PhalconCache
26+
{
27+
/** @var int */
28+
public const CACHE_LIFETIME_DAY = 86400;
29+
/** @var int */
30+
public const CACHE_LIFETIME_HOUR = 3600;
31+
/**
32+
* Cache Timeouts
33+
*/
34+
/** @var int */
35+
public const CACHE_LIFETIME_MINUTE = 60;
36+
/** @var int */
37+
public const CACHE_LIFETIME_MONTH = 2592000;
38+
/**
39+
* Default token expiry - 4 hours
40+
*/
41+
/** @var int */
42+
public const CACHE_TOKEN_EXPIRY = 14400;
43+
/**
44+
* Cache masks
45+
*/
46+
/** @var string */
47+
private const MASK_TOKEN_USER = 'tk-%s-%s';
48+
49+
/**
50+
* @param UserTransport $domainUser
51+
* @param string $token
52+
*
53+
* @return string
54+
*/
55+
public function getCacheTokenKey(UserTransport $domainUser, string $token): string
56+
{
57+
return sprintf(
58+
self::MASK_TOKEN_USER,
59+
$domainUser->getId(),
60+
sha1($token)
61+
);
62+
}
63+
64+
/**
65+
* @param EnvManager $env
66+
* @param UserTransport $domainUser
67+
* @param string $token
68+
*
69+
* @return bool
70+
* @throws InvalidArgumentException
71+
*/
72+
public function storeTokenInCache(
73+
EnvManager $env,
74+
UserTransport $domainUser,
75+
string $token
76+
): bool {
77+
$cacheKey = $this->getCacheTokenKey($domainUser, $token);
78+
/** @var int $expiration */
79+
$expiration = $env->get('TOKEN_EXPIRATION', self::CACHE_TOKEN_EXPIRY, 'int');
80+
$expirationDate = (new DateTimeImmutable())
81+
->modify('+' . $expiration . ' seconds')
82+
->format(Dates::DATE_TIME_FORMAT)
83+
;
84+
85+
$payload = [
86+
'token' => $token,
87+
'expiry' => $expirationDate,
88+
];
89+
90+
return $this->set($cacheKey, $payload, $expiration);
91+
}
92+
}

0 commit comments

Comments
 (0)