域对象和数据映射器应如何在使用MVC的身份验证系统的服务类中进行交互

问题描述 投票:1回答:1

我正在使用后端的Slim 3 PHP和前端的Angular创建身份验证/登录系统,并且我试图了解其中的模型层的“域对象”和“数据映射器”部分MVC结构。我已经阅读了很多有关各种问题的有用答案such as this,据我了解,该模型应由“域对象”,“数据映射器”和“服务”组成。

但是我不能完全确定在用户能够注册并登录网站的情况下该如何构造。

据我了解,我可以拥有一个用户“域对象”,该域具有诸如用户名和密码之类的属性。它还可以使用诸如注册或登录之类的方法来表示业务逻辑。

然后,我将拥有一个服务类来创建用户对象的新实例,在其中将表单数据传递给该对象吗?所以现在我的用户对象实例将设置用户名和密码值?

现在我不确定如何将此对象属性数据插入数据库。我将使用用户对象注册方法通过将用户名和密码作为参数传递来将数据插入数据库吗?

显然,服务应该是域对象和数据映射器交互的位置,但是我不确定如果register方法位于用户域对象中,则该方法将如何工作。

我希望有人可以向我展示一些代码示例,这些代码示例说明了服务类中应该包含的代码示例以及域对象和数据映射器之间的交互如何在用户注册和登录的上下文中起作用。

注意,我不想使用任何框架,我想尝试并手动实现适当的MVC结构,因为我觉得自己会学到更多。

到目前为止,我已经有了用于注册用户的结构:

我有一个带有registerUser方法的AuthenticationController,允许用户创建帐户:

 class AuthenticationController
{
    protected $authenticationService;

    public function __construct(AuthenticationService $authenticationService)
    {
        $this->authenticationService = $authenticationService;
    }

    public function registerUser($request, $response)
    {
        $this->authenticationService->registerUser($request, $response);
    }
}

然后我有了带有registerUser方法的AuthenticationService类:

class AuthenticationService
{
    protected $database;

    public function __construct(PDO $database)
    {
        $this->database = $database;
    }

    public function registerUser ($request, $response)
    {
        $strings = $request→getParsedBody(); // will be sanitised / validated later
        $username = $strings['username'];
        $password = $strings['password'];
        $email = "temp random email";

        $stmt = $this->database->prepare("INSERT INTO users (email, username, password) values (:email, :username, :password)");
        $stmt->bindParam(':email', $email);
        $stmt->bindParam(':username', $username);
        $stmt->bindParam(':password', $password);
        $stmt->execute();
    }
}

稍后,我打算将SQL放入AuthenticationRepository,并将PDO逻辑放入其自己的类中。此AuthenticationService方法还将确保使用PHP的内置函数对用户详细信息进行清理。

我不确定所提议的PDO数据库类或AuthenticationRepository是否会算作数据映射器。

php model-view-controller datamapper slim-3 domain-object
1个回答
0
投票
  • 注册将由服务执行。
  • 该服务可以“直接”使用data mapper,以便“向/从数据库转移实体”。尽管additionally,也可以实现repository。该服务将看到它并与一个或多个实体的集合进行通信。
  • 由于服务是模型层(域模型)的一部分,因此它对任何requestresponse对象一无所知。控制器应从request中提取所需的值,并将them作为参数传递给服务方法。 响应可以由控制器或视图发送。
  • 您说“我打算将PDO逻辑放入其自己的类中]。您确实不需要实现PDO包装器。

这里是注册示例。我根本没有测试。有关更多详细信息,请参见此答案末尾的资源列表。也许从最后一个开始。

控制器:

<?php

namespace MyApp\UI\Web\Controller\Users;

use Psr\Http\Message\ServerRequestInterface;
use MyApp\Domain\Model\Users\Exception\InvalidCredentials;
use MyApp\Domain\Service\Users\Exception\FailedRegistration;
use MyApp\Domain\Service\Users\Registration as RegistrationService;

class Registration {

    private $registration;

    public function __construct(RegistrationService $registration) {
        $this->registration = $registration;
    }

    public function register(ServerRequestInterface $request) {
        $username = $request->getParsedBody()['username'];
        $email = $request->getParsedBody()['email'];
        $password = $request->getParsedBody()['password'];

        try {
            $user = $this->registration->register($username, $email, $password);
        } catch (InvalidCredentials $exc) {
            // Write the exception message to a flash messenger, for example, 
            // in order to be read and displayed by the specific view component.
            var_dump($exc->getMessage());
        } catch (FailedRegistration $exc) {
            // Write the exception message to the flash messenger.
            var_dump($exc->getMessage());
        }

        // In the view component, if no exception messages are found in the flash messenger, display a success message.
        var_dump('Successfully registered.');
    }

}

服务:

<?php

namespace MyApp\Domain\Service\Users;

use MyApp\Domain\Model\Users\User;
use MyApp\Domain\Model\Users\Email;
use MyApp\Domain\Model\Users\Password;
use MyApp\Domain\Service\Users\Exception\UserExists;
use MyApp\Domain\Model\Users\UserCollection as UserCollectionInterface;

class Registration {

    /**
     * User collection, e.g. user repository.
     * 
     * @var UserCollectionInterface
     */
    private $userCollection;

    public function __construct(UserCollectionInterface $userCollection) {
        $this->userCollection = $userCollection;
    }

    /**
     * Register user.
     * 
     * @param string $username Username.
     * @param string $email Email.
     * @param string $password Password.
     * @return User User.
     */
    public function register(string $username, string $email, string $password) {
        // Create a new user.
        $user = $this->createUser($username, $email, $password);

        return $this->storeUser($user);
    }

    /**
     * Create user.
     * 
     * @param string $username Username.
     * @param string $email Email.
     * @param string $password Password.
     * @return User User.
     */
    private function createUser(string $username, string $email, string $password) {
        // Create the object values (containing specific validation).
        $email = new Email($email);
        $password = new Password($password);

        // Create the entity (e.g. the domain object).
        $user = new User();

        $user->setUsername($username);
        $user->setEmail($email);
        $user->setPassword($password);

        return $user;
    }

    /**
     * Store user.
     * 
     * @param User $user User.
     * @return User User.
     */
    private function storeUser(User $user) {
        // Check if user already exists.
        if ($this->userCollection->exists($user)) {
            throw new UserExists();
        }

        return $this->userCollection->store($user);
    }

}

尝试注册现有用户时抛出的异常

<?php

namespace MyApp\Domain\Service\Users\Exception;

use MyApp\Domain\Service\Users\Exception\FailedRegistration;

class UserExists extends FailedRegistration {

    public function __construct(\Exception $previous = null) {
        $message = 'User already exists.';
        $code = 123;

        parent::__construct($message, $code, $previous);
    }

}

<?php

namespace MyApp\Domain\Service\Users\Exception;

abstract class FailedRegistration extends \Exception {

    public function __construct(string $message, int $code = 0, \Exception $previous = null) {
        $message = 'Registration failed: ' . $message;

        parent::__construct($message, $code, $previous);
    }

}

域对象(实体):

<?php

namespace MyApp\Domain\Model\Users;

use MyApp\Domain\Model\Users\Email;
use MyApp\Domain\Model\Users\Password;

/**
 * User entity (e.g. domain object).
 */
class User {

    private $id;
    private $username;
    private $email;
    private $password;

    public function getId() {
        return $this->id;
    }

    public function setId(int id) {
        $this->id = $id;
        return $this;
    }

    public function getUsername() {
        return $this->username;
    }

    public function setUsername(string $username) {
        $this->username = $username;
        return $this;
    }

    public function getEmail() {
        return $this->email;
    }

    public function setEmail(Email $email) {
        $this->email = $email;
        return $this;
    }

    public function getPassword() {
        return $this->password;
    }

    public function setPassword(Password $password) {
        $this->password = $password;
        return $this;
    }

}

实体使用的值对象:

<?php

namespace MyApp\Domain\Model\Users;

use MyApp\Domain\Model\Users\Exception\InvalidEmail;

/**
 * Email object value.
 */
class Email {

    private $email;

    public function __construct(string $email) {
        if (!$this->isValid($email)) {
            throw new InvalidEmail();
        }

        $this->email = $email;
    }

    private function isValid(string $email) {
        return (isEmpty($email) || !isWellFormed($email)) ? false : true;
    }

    private function isEmpty(string $email) {
        return empty($email) ? true : false;
    }

    private function isWellFormed(string $email) {
        return !filter_var($email, FILTER_VALIDATE_EMAIL) ? false : true;
    }

    public function __toString() {
        return $this->email;
    }

}

<?php

namespace MyApp\Domain\Model\Users;

use MyApp\Domain\Model\Users\Exception\InvalidPassword;

/**
 * Password object value.
 */
class Password {

    private const MIN_LENGTH = 8;

    private $password;


    public function __construct(string $password) {
        if (!$this->isValid($password)) {
            throw new InvalidPassword();
        }

        $this->password = $password;
    }

    private function isValid(string $password) {
        return (isEmpty($password) || isTooShort($password)) ? false : true;
    }

    private function isEmpty(string $password) {
        return empty($password) ? true : false;
    }

    private function isTooShort(string $password) {
        return strlen($password) < self::MIN_LENGTH ? true : false;
    }

    public function __toString() {
        return $this->password;
    }

}

值对象抛出的异常:

<?php

namespace MyApp\Domain\Model\Users\Exception;

use MyApp\Domain\Model\Users\Exception\InvalidCredentials;

class InvalidEmail extends InvalidCredentials {

    public function __construct(\Exception $previous = null) {
        $message = 'The provided email is not valid.';
        $code = 123402;

        parent::__construct($message, $code, $previous);
    }

}

<?php

namespace MyApp\Domain\Model\Users\Exception;

use MyApp\Domain\Model\Users\Exception\InvalidCredentials;

class InvalidPassword extends InvalidCredentials {

    public function __construct(\Exception $previous = null) {
        $message = 'The provided password is not valid.';
        $code = 123401;

        parent::__construct($message, $code, $previous);
    }

}

<?php

namespace MyApp\Domain\Model\Users\Exception;

abstract class InvalidCredentials extends \LogicException {

    public function __construct(string $message, int $code = 0, \Exception $previous = null) {
        $message = 'Invalid credentials: ' . $message;

        parent::__construct($message, $code, $previous);
    }

}

存储库接口:

<?php

namespace MyApp\Domain\Model\Users;

use MyApp\Domain\Model\Users\User;

/**
 * User collection, e.g. user repository.
 */
interface UserCollection {

    /**
     * Find a user by id.
     * 
     * @param int $id User id.
     * @return User|null User.
     */
    public function findById(int $id);

    /**
     * Find all users.
     * 
     * @return User[] User list.
     */
    public function findAll();

    /**
     * Check if the given user exists.
     * 
     * @param User $user User
     * @return bool True if user exists, false otherwise.
     */
    public function exists(User $user);

    /**
     * Store a user.
     * 
     * @param User $user User
     * @return User User.
     */
    public function store(User $user);

}

存储库:

<?php

namespace MyApp\Domain\Infrastructure\Repository\Users;

use MyApp\Domain\Model\Users\User;
use MyApp\Domain\Infrastructure\Mapper\Users\UserMapper;
use MyApp\Domain\Model\Users\UserCollection as UserCollectionInterface;

/**
 * User collection, e.g. user repository.
 */
class UserCollection implements UserCollectionInterface {

    private $userMapper;

    public function __construct(UserMapper $userMapper) {
        $this->userMapper = $userMapper;
    }

    /**
     * Find a user by id.
     * 
     * @param int $id User id.
     * @return User|null User.
     */
    public function findById(int $id) {
        return $this->userMapper->fetchUserById($id);
    }

    /**
     * Find all users.
     * 
     * @return User[] User list.
     */
    public function findAll() {
        return $this->userMapper->fetchAllUsers();
    }

    /**
     * Check if the given user exists.
     * 
     * @param User $user User
     * @return bool True if user exists, false otherwise.
     */
    public function exists(User $user) {
        return $this->userMapper->userExists($user);
    }

    /**
     * Store a user.
     * 
     * @param User $user User
     * @return User User.
     */
    public function store(User $user) {
        return $this->userMapper->saveUser($user);
    }

}

数据映射器接口:

<?php

namespace MyApp\Domain\Infrastructure\Mapper\Users;

use MyApp\Domain\Model\Users\User;

/**
 * User mapper.
 */
interface UserMapper {

    /**
     * Fetch a user by id.
     * 
     * @param int $id User id.
     * @return User|null User.
     */
    public function fetchUserById(int $id);

    /**
     * Fetch all users.
     * 
     * @return User[] User list.
     */
    public function fetchAllUsers();

    /**
     * Check if the given user exists.
     * 
     * @param User $user User.
     * @return bool True if the user exists, false otherwise.
     */
    public function userExists(User $user);

    /**
     * Save a user.
     * 
     * @param User $user User.
     * @return User User.
     */
    public function saveUser(User $user);
}

数据映射器:

<?php

namespace MyApp\Domain\Infrastructure\Mapper\Users;

use PDO;
use MyApp\Domain\Model\Users\User;
use MyApp\Domain\Model\Users\Email;
use MyApp\Domain\Model\Users\Password;
use MyApp\Domain\Infrastructure\Mapper\Users\UserMapper;

/**
 * PDO user mapper.
 */
class PdoUserMapper implements UserMapper {

    /**
     * Database connection.
     * 
     * @var PDO
     */
    private $connection;

    public function __construct(PDO $connection) {
        $this->connection = $connection;
    }

    /**
     * Fetch a user by id.
     * 
     * Note: PDOStatement::fetch returns FALSE if no record is found.
     * 
     * @param int $id User id.
     * @return User|null User.
     */
    public function fetchUserById(int $id) {
        $sql = 'SELECT * FROM users WHERE id = :id LIMIT 1';

        $statement = $this->connection->prepare($sql);
        $statement->execute([
            'id' => $id,
        ]);

        $record = $statement->fetch(PDO::FETCH_ASSOC);

        return ($record === false) ? null : $this->convertRecordToUser($record);
    }

    /**
     * Fetch all users.
     * 
     * @return User[] User list.
     */
    public function fetchAllUsers() {
        $sql = 'SELECT * FROM users';

        $statement = $this->connection->prepare($sql);
        $statement->execute();

        $recordset = $statement->fetchAll(PDO::FETCH_ASSOC);

        return $this->convertRecordsetToUserList($recordset);
    }

    /**
     * Check if the given user exists.
     * 
     * Note: PDOStatement::fetch returns FALSE if no record is found.
     * 
     * @param User $user User.
     * @return bool True if the user exists, false otherwise.
     */
    public function userExists(User $user) {
        $sql = 'SELECT COUNT(*) as cnt FROM users WHERE username = :username';

        $statement = $this->connection->prepare($sql);
        $statement->execute([
            ':username' => $user->getUsername(),
        ]);

        $record = $statement->fetch(PDO::FETCH_ASSOC);

        return ($record['cnt'] > 0) ? true : false;
    }

    /**
     * Save a user.
     * 
     * @param User $user User.
     * @return User User.
     */
    public function saveUser(User $user) {
        $id = $user->getId();

        if (!isset($id)) {
            return $this->insertUser($user);
        }

        return $this->updateUser($user);
    }

    /**
     * Insert a user.
     * 
     * @param User $user User.
     * @return User User.
     */
    private function insertUser(User $user) {
        $sql = 'INSERT INTO users (
                    username,
                    email,
                    password
                ) VALUES (
                    :username,
                    :email,
                    :password
                )';

        $statement = $this->connection->prepare($sql);
        $statement->execute([
            ':username' => $user->getUsername(),
            ':email' => (string) $user->getEmail(),
            ':password' => (string) $user->getPassword(),
        ]);

        $user->setId($this->connection->lastInsertId());

        return $user;
    }

    /**
     * Update a user.
     * 
     * @param User $user User.
     * @return User User.
     */
    private function updateUser(User $user) {
        $sql = 'UPDATE users 
                SET 
                    username = :username,
                    email = :email,
                    password = :password 
                WHERE id = :id';

        $statement = $this->connection->prepare($sql);
        $statement->execute([
            ':id' => $user->getId(),
            ':username' => $user->getUsername(),
            ':email' => (string) $user->getEmail(),
            ':password' => (string) $user->getPassword(),
        ]);

        return $user;
    }

    /**
     * Convert a record to a user.
     * 
     * @param array $record Record data.
     * @return User User.
     */
    private function convertRecordToUser(array $record) {
        $user = $this->createUser(
                    $record['id'],
                    $record['username'],
                    $record['email'],
                    $record['password']
                );

        return $user;
    }

    /**
     * Convert a recordset to a list of users.
     * 
     * @param array $recordset Recordset data.
     * @return User[] User list.
     */
    private function convertRecordsetToUserList(array $recordset) {
        $users = [];

        foreach ($recordset as $record) {
            $users[] = $this->convertRecordToUser($record);
        }

        return $users;
    }

    /**
     * Create user.
     *
     * @param int $id User id. 
     * @param string $username Username.
     * @param string $email Email.
     * @param string $password Password.
     * @return User User.
     */
    private function createUser(int $id, string $username, string $email, string $password) {
        $user = new User();

        $user
            ->setId($id)
            ->setUsername($username)
            ->setEmail(new Email($email))
            ->setPassword(new Password($password))
        ;

        return $user;
    }

}

使用的文件系统结构:

a)扩展MyApp / UI

Structure: MyApp/UI

b)扩展MyApp / Domain

Structure: MyApp/Domain


资源:

© www.soinside.com 2019 - 2024. All rights reserved.