AdminHandler.php 1.45 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
<?php

namespace LQDN\Handler;

use Doctrine\DBAL\Connection;
use LQDN\Command\AdminChangePasswordCommand;
use LQDN\Command\AdminCreateCommand;
use LQDN\Command\AdminDeleteCommand;

class AdminHandler
{
    private $connection;

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

    /**
     * Create a new admin.
     *
     * @param AdminCreateCommand $command
     */
    public function handleAdminCreateCommand(AdminCreateCommand $command)
    {
        $this->connection->executeUpdate('INSERT INTO admins (user_id, password) VALUES (:username, :password)', [
            'username' => $command->getUsername(),
            'password' => hash('sha256', $command->getPassword()),
        ]);
    }

    /**
     * Delete an admin.
     *
     * @param AdminDeleteCommand $command
     */
    public function handleAdminDeleteCommand(AdminDeleteCommand $command)
    {
        $this->connection->executeUpdate('DELETE FROM admins WHERE admins.id = :id', ['id' => $command->getId()]);
    }

    /**
     * Update the password of an admin.
     *
     * @param AdminChangePasswordCommand $command
     */
    public function handleAdminChangePasswordCommand(AdminChangePasswordCommand $command)
    {
        $this->connection->executeUpdate('UPDATE admins SET password = :password WHERE id = :id', [
            'password' => hash('sha256', $command->getPassword()),
            'id' => $command->getId(),
        ]);
    }
}