Skip to content
11 changes: 10 additions & 1 deletion src/Domain/ADR/Action/ActionHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,29 @@
namespace Phalcon\Api\Domain\ADR\Action;

use Phalcon\Api\Domain\ADR\Domain\DomainInterface;
use Phalcon\Api\Domain\ADR\Domain\Input;
use Phalcon\Api\Domain\ADR\Responder\ResponderInterface;
use Phalcon\Api\Domain\Services\Http\Response;
use Phalcon\Http\Request;

final readonly class ActionHandler implements ActionInterface
{
public function __construct(
private Request $request,
private Response $response,
private DomainInterface $service,
private ResponderInterface $responder
) {
}

public function __invoke(): void
{
$input = new Input();
$data = $input->__invoke($this->request);

$this->responder->__invoke(
$this->service->__invoke()
$this->response,
$this->service->__invoke($data)
);
}
}
11 changes: 10 additions & 1 deletion src/Domain/ADR/Domain/DomainInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,16 @@

use Phalcon\Domain\Payload;

/**
* @phpstan-import-type THelloInput from InputTypes
* @phpstan-import-type TUserInput from InputTypes
*/
interface DomainInterface
{
public function __invoke(): Payload;
/**
* @param THelloInput|TUserInput $input
*
* @return Payload
*/
public function __invoke(array $input): Payload;
}
35 changes: 35 additions & 0 deletions src/Domain/ADR/Domain/Input.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

/**
* This file is part of the Phalcon API.
*
* (c) Phalcon Team <team@phalcon.io>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Phalcon\Api\Domain\ADR\Domain;

use Phalcon\Http\Request;

/**
* @phpstan-import-type TRequestQuery from InputTypes
*/
final class Input implements InputInterface
{
/**
* @param Request $request
*
* @return TRequestQuery
*/
public function __invoke(Request $request): array
{
/** @var TRequestQuery $query */
$query = $request->getQuery();

return $query;
}
}
29 changes: 29 additions & 0 deletions src/Domain/ADR/Domain/InputInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

/**
* This file is part of the Phalcon API.
*
* (c) Phalcon Team <team@phalcon.io>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Phalcon\Api\Domain\ADR\Domain;

use Phalcon\Http\Request;

/**
* @phpstan-import-type TRequestQuery from InputTypes
*/
interface InputInterface
{
/**
* @param Request $request
*
* @return TRequestQuery
*/
public function __invoke(Request $request): array;
}
25 changes: 25 additions & 0 deletions src/Domain/ADR/Domain/InputTypes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

/**
* This file is part of the Phalcon API.
*
* (c) Phalcon Team <team@phalcon.io>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Phalcon\Api\Domain\ADR\Domain;

/**
* @phpstan-type THelloInput array{}
* @phpstan-type TUserInput array{
* userId?: int
* }
* @phpstan-type TRequestQuery array<array-key, bool|int|string>
*/
final class InputTypes
{
}
31 changes: 19 additions & 12 deletions src/Domain/ADR/Responder/JsonResponder.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,28 +13,35 @@

namespace Phalcon\Api\Domain\ADR\Responder;

use Exception as BaseException;
use Phalcon\Api\Domain\Services\Http\Response;
use Phalcon\Domain\Payload;

/**
* @phpstan-import-type TResult from ResponderTypes
*/
final class JsonResponder implements ResponderInterface
{
public function __construct(
private Response $response
) {
}

public function __invoke(Payload $payload): Response
{
/**
* @param Response $response
* @param Payload $payload
*
* @return Response
* @throws BaseException
*/
public function __invoke(
Response $response,
Payload $payload
): Response {
$result = $payload->getResult();
/** @var string $content */
/** @var TResult $content */
$content = $result['results'];

$this
->response
->withPayloadData([$content])
$response
->withPayloadData($content)
->render()
;

return $this->response;
return $response;
}
}
6 changes: 5 additions & 1 deletion src/Domain/ADR/Responder/ResponderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,14 @@

namespace Phalcon\Api\Domain\ADR\Responder;

use Phalcon\Api\Domain\Services\Http\Response;
use Phalcon\Domain\Payload;
use Phalcon\Http\ResponseInterface;

interface ResponderInterface
{
public function __invoke(Payload $payload): ResponseInterface;
public function __invoke(
Response $response,
Payload $payload
): ResponseInterface;
}
24 changes: 24 additions & 0 deletions src/Domain/ADR/Responder/ResponderTypes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

/**
* This file is part of the Phalcon API.
*
* (c) Phalcon Team <team@phalcon.io>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Phalcon\Api\Domain\ADR\Responder;

/**
* @phpstan-type TResultItem array<array-key, bool|int|string>
* @phpstan-type TResult array{
* result: array<array-key, bool|int|string|TResultItem>
* }
*/
final class ResponderTypes
{
}
50 changes: 50 additions & 0 deletions src/Domain/DataSource/User/UserRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

/**
* This file is part of the Phalcon API.
*
* (c) Phalcon Team <team@phalcon.io>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Phalcon\Api\Domain\DataSource\User;

use Phalcon\DataMapper\Pdo\Connection;
use Phalcon\DataMapper\Query\Select;

/**
* @phpstan-import-type TUserRecord from UserTypes
*/
final class UserRepository
{
public function __construct(
private readonly Connection $connection,
) {
}

/**
* @param int|string $userId
*
* @return UserTransport
*/
public function findById(int | string $userId): UserTransport
{
$result = [];
if (true !== empty($userId)) {
$select = Select::new($this->connection);

/** @var TUserRecord $result */
$result = $select
->from('co_users')
->where('usr_id = ', $userId)
->fetchOne()
;
}

return new UserTransport($result);
}
}
77 changes: 77 additions & 0 deletions src/Domain/DataSource/User/UserTransport.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

/**
* This file is part of the Phalcon API.
*
* (c) Phalcon Team <team@phalcon.io>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Phalcon\Api\Domain\DataSource\User;

use Phalcon\Api\Domain\Exceptions\InvalidConfigurationArgumentException;

/**
* @method int getId()
* @method int getStatus()
* @method string getUsername()
* @method string getPassword()
*
* @phpstan-import-type TUserRecord from UserTypes
* @phpstan-import-type TUserTransport from UserTypes
*/
final class UserTransport
{
/** @var TUserTransport */
private array $store;

/**
* @param TUserRecord $input
*/
public function __construct(array $input)
{
$this->store = [
'id' => (int)($input['usr_id'] ?? 0),
'status' => (int)($input['usr_status_flag'] ?? 0),
'username' => (string)($input['usr_username'] ?? ''),
'password' => (string)($input['usr_password'] ?? ''),
];
}

/**
* @param string $name
* @param array<mixed> $arguments
*
* @return int|string
*/
public function __call(string $name, array $arguments): int | string
{
return match ($name) {
'getId' => $this->store['id'],
'getStatus' => $this->store['status'],
'getUsername' => $this->store['username'],
'getPassword' => $this->store['password'],
default => throw new InvalidConfigurationArgumentException(
'The ' . $name . ' method is not supported. ['
. json_encode($arguments) . ']',
),
};
}

public function isEmpty(): bool
{
return 0 === $this->store['id'];
}

/**
* @return array<int, TUserTransport>
*/
public function toArray(): array
{
return [$this->store['id'] => $this->store];
}
}
33 changes: 33 additions & 0 deletions src/Domain/DataSource/User/UserTypes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

/**
* This file is part of the Phalcon API.
*
* (c) Phalcon Team <team@phalcon.io>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Phalcon\Api\Domain\DataSource\User;

/**
* @phpstan-type TUserRecord array{}|array{
* usr_id: int,
* usr_status_flag: int,
* usr_username: string,
* usr_password: string
* }
*
* @phpstan-type TUserTransport array{
* id: int,
* status: int,
* username: string,
* password: string
* }
*/
final class UserTypes
{
}
Loading
Loading