Skip to content

Commit 2d77f71

Browse files
authored
Merge pull request #14 from niden-code/1.x
Catching up with the tests video #6
2 parents fc71aeb + cff4a1a commit 2d77f71

35 files changed

+941
-102
lines changed

src/Domain/ADR/Action/ActionHandler.php

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

1616
use Phalcon\Api\Domain\ADR\Domain\DomainInterface;
17+
use Phalcon\Api\Domain\ADR\Domain\Input;
1718
use Phalcon\Api\Domain\ADR\Responder\ResponderInterface;
19+
use Phalcon\Api\Domain\Services\Http\Response;
20+
use Phalcon\Http\Request;
1821

1922
final readonly class ActionHandler implements ActionInterface
2023
{
2124
public function __construct(
25+
private Request $request,
26+
private Response $response,
2227
private DomainInterface $service,
2328
private ResponderInterface $responder
2429
) {
2530
}
2631

2732
public function __invoke(): void
2833
{
34+
$input = new Input();
35+
$data = $input->__invoke($this->request);
36+
2937
$this->responder->__invoke(
30-
$this->service->__invoke()
38+
$this->response,
39+
$this->service->__invoke($data)
3140
);
3241
}
3342
}

src/Domain/ADR/Domain/DomainInterface.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,16 @@
1515

1616
use Phalcon\Domain\Payload;
1717

18+
/**
19+
* @phpstan-import-type THelloInput from InputTypes
20+
* @phpstan-import-type TUserInput from InputTypes
21+
*/
1822
interface DomainInterface
1923
{
20-
public function __invoke(): Payload;
24+
/**
25+
* @param THelloInput|TUserInput $input
26+
*
27+
* @return Payload
28+
*/
29+
public function __invoke(array $input): Payload;
2130
}

src/Domain/ADR/Domain/Input.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
/**
4+
* This file is part of the Phalcon API.
5+
*
6+
* (c) Phalcon Team <[email protected]>
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\ADR\Domain;
15+
16+
use Phalcon\Http\Request;
17+
18+
/**
19+
* @phpstan-import-type TRequestQuery from InputTypes
20+
*/
21+
final class Input implements InputInterface
22+
{
23+
/**
24+
* @param Request $request
25+
*
26+
* @return TRequestQuery
27+
*/
28+
public function __invoke(Request $request): array
29+
{
30+
/** @var TRequestQuery $query */
31+
$query = $request->getQuery();
32+
33+
return $query;
34+
}
35+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
/**
4+
* This file is part of the Phalcon API.
5+
*
6+
* (c) Phalcon Team <[email protected]>
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\ADR\Domain;
15+
16+
use Phalcon\Http\Request;
17+
18+
/**
19+
* @phpstan-import-type TRequestQuery from InputTypes
20+
*/
21+
interface InputInterface
22+
{
23+
/**
24+
* @param Request $request
25+
*
26+
* @return TRequestQuery
27+
*/
28+
public function __invoke(Request $request): array;
29+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
/**
4+
* This file is part of the Phalcon API.
5+
*
6+
* (c) Phalcon Team <[email protected]>
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\ADR\Domain;
15+
16+
/**
17+
* @phpstan-type THelloInput array{}
18+
* @phpstan-type TUserInput array{
19+
* userId?: int
20+
* }
21+
* @phpstan-type TRequestQuery array<array-key, bool|int|string>
22+
*/
23+
final class InputTypes
24+
{
25+
}

src/Domain/ADR/Responder/JsonResponder.php

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,28 +13,35 @@
1313

1414
namespace Phalcon\Api\Domain\ADR\Responder;
1515

16+
use Exception as BaseException;
1617
use Phalcon\Api\Domain\Services\Http\Response;
1718
use Phalcon\Domain\Payload;
1819

20+
/**
21+
* @phpstan-import-type TResult from ResponderTypes
22+
*/
1923
final class JsonResponder implements ResponderInterface
2024
{
21-
public function __construct(
22-
private Response $response
23-
) {
24-
}
25-
26-
public function __invoke(Payload $payload): Response
27-
{
25+
/**
26+
* @param Response $response
27+
* @param Payload $payload
28+
*
29+
* @return Response
30+
* @throws BaseException
31+
*/
32+
public function __invoke(
33+
Response $response,
34+
Payload $payload
35+
): Response {
2836
$result = $payload->getResult();
29-
/** @var string $content */
37+
/** @var TResult $content */
3038
$content = $result['results'];
3139

32-
$this
33-
->response
34-
->withPayloadData([$content])
40+
$response
41+
->withPayloadData($content)
3542
->render()
3643
;
3744

38-
return $this->response;
45+
return $response;
3946
}
4047
}

src/Domain/ADR/Responder/ResponderInterface.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,14 @@
1313

1414
namespace Phalcon\Api\Domain\ADR\Responder;
1515

16+
use Phalcon\Api\Domain\Services\Http\Response;
1617
use Phalcon\Domain\Payload;
1718
use Phalcon\Http\ResponseInterface;
1819

1920
interface ResponderInterface
2021
{
21-
public function __invoke(Payload $payload): ResponseInterface;
22+
public function __invoke(
23+
Response $response,
24+
Payload $payload
25+
): ResponseInterface;
2226
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
/**
4+
* This file is part of the Phalcon API.
5+
*
6+
* (c) Phalcon Team <[email protected]>
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\ADR\Responder;
15+
16+
/**
17+
* @phpstan-type TResultItem array<array-key, bool|int|string>
18+
* @phpstan-type TResult array{
19+
* result: array<array-key, bool|int|string|TResultItem>
20+
* }
21+
*/
22+
final class ResponderTypes
23+
{
24+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
/**
4+
* This file is part of the Phalcon API.
5+
*
6+
* (c) Phalcon Team <[email protected]>
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\DataSource\User;
15+
16+
use Phalcon\DataMapper\Pdo\Connection;
17+
use Phalcon\DataMapper\Query\Select;
18+
19+
/**
20+
* @phpstan-import-type TUserRecord from UserTypes
21+
*/
22+
final class UserRepository
23+
{
24+
public function __construct(
25+
private readonly Connection $connection,
26+
) {
27+
}
28+
29+
/**
30+
* @param int|string $userId
31+
*
32+
* @return UserTransport
33+
*/
34+
public function findById(int | string $userId): UserTransport
35+
{
36+
$result = [];
37+
if (true !== empty($userId)) {
38+
$select = Select::new($this->connection);
39+
40+
/** @var TUserRecord $result */
41+
$result = $select
42+
->from('co_users')
43+
->where('usr_id = ', $userId)
44+
->fetchOne()
45+
;
46+
}
47+
48+
return new UserTransport($result);
49+
}
50+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?php
2+
3+
/**
4+
* This file is part of the Phalcon API.
5+
*
6+
* (c) Phalcon Team <[email protected]>
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\DataSource\User;
15+
16+
use Phalcon\Api\Domain\Exceptions\InvalidConfigurationArgumentException;
17+
18+
/**
19+
* @method int getId()
20+
* @method int getStatus()
21+
* @method string getUsername()
22+
* @method string getPassword()
23+
*
24+
* @phpstan-import-type TUserRecord from UserTypes
25+
* @phpstan-import-type TUserTransport from UserTypes
26+
*/
27+
final class UserTransport
28+
{
29+
/** @var TUserTransport */
30+
private array $store;
31+
32+
/**
33+
* @param TUserRecord $input
34+
*/
35+
public function __construct(array $input)
36+
{
37+
$this->store = [
38+
'id' => (int)($input['usr_id'] ?? 0),
39+
'status' => (int)($input['usr_status_flag'] ?? 0),
40+
'username' => (string)($input['usr_username'] ?? ''),
41+
'password' => (string)($input['usr_password'] ?? ''),
42+
];
43+
}
44+
45+
/**
46+
* @param string $name
47+
* @param array<mixed> $arguments
48+
*
49+
* @return int|string
50+
*/
51+
public function __call(string $name, array $arguments): int | string
52+
{
53+
return match ($name) {
54+
'getId' => $this->store['id'],
55+
'getStatus' => $this->store['status'],
56+
'getUsername' => $this->store['username'],
57+
'getPassword' => $this->store['password'],
58+
default => throw new InvalidConfigurationArgumentException(
59+
'The ' . $name . ' method is not supported. ['
60+
. json_encode($arguments) . ']',
61+
),
62+
};
63+
}
64+
65+
public function isEmpty(): bool
66+
{
67+
return 0 === $this->store['id'];
68+
}
69+
70+
/**
71+
* @return array<int, TUserTransport>
72+
*/
73+
public function toArray(): array
74+
{
75+
return [$this->store['id'] => $this->store];
76+
}
77+
}

0 commit comments

Comments
 (0)