UserHandler.php 3.92 KB
Newer Older
1 2 3 4 5 6
<?php

namespace LQDN\Handler;

use Doctrine\DBAL\Connection;
use LQDN\Command\UserUpdateByAdminCommand;
7 8
use LQDN\Command\UserUpdateTotalCommand;
use LQDN\Command\UserUpdateCumulCommand;
okhin's avatar
okhin committed
9
use LQDN\Command\UserCreateCommand;
10
use LQDN\Command\AdminUpdateTotalUsersCommand;
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27

class UserHandler
{
    private $connection;

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

    /**
     * Update of a user from the admin.
     *
     * @param UserUpdateByAdminCommand $command
     */
    public function handleUserUpdateByAdminCommand(UserUpdateByAdminCommand $command)
    {
28
        $this->connection->executeUpdate('UPDATE users SET pseudo = :username, email = :email, commentaire = :comment, cumul = :cumul, total = :total WHERE id = :id', [
29 30 31 32
            'username' => $command->getUsername(),
            'email' => $command->getEmail(),
            'comment' => $command->getComment(),
            'id' => $command->getId(),
33 34
            'total' => $command->getTotal(),
            'cumul' => $command->getCumul(),
35 36
        ]);
    }
37

okhin's avatar
okhin committed
38 39 40 41 42 43 44
    /**
     * Create a user in database
     *
     * @param UserCreateCommand $command
     */
    public function handleUserCreateCommand(UserCreateCommand $command)
    {
45 46 47 48 49 50 51
        $this->connection->executeUpdate('INSERT INTO users(email, hash, pseudo, total, cumul) VALUES (:email, :hash, :pseudo, :total, :cumul)', [
            'email'=> $command->getEmail(),
            'hash'=> $command->getHash(),
            'pseudo'=> $command->getPseudo(),
            'total'=> $command->getTotal(),
            'cumul'=> $command->getCumul(),
        ]);
okhin's avatar
okhin committed
52 53
    }

54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
    /**
     * Update the user total
     *
     * @param UserUpdateTotalCommand $command
     */
    public function handleUserUpdateTotalCommand(UserUpdateTotalCommand $command)
    {
        $this->connection->executeUpdate(
            'UPDATE users SET total = :total WHERE id = :id',
            [
                'total' => $command->getTotal(),
                'id'=> $command->getId()
            ]
        );
    }

    /**
     * Update the user cumul
     *
     * @param UserUpdateCumulCommand $command
     */
    public function handleUserUpdateCumulCommand(UserUpdateCumulCommand $command)
    {
        $this->connection->executeUpdate(
            'UPDATE users SET cumul = :cumul WHERE id = :id',
            [
                'cumul' => $command->getCumul(),
                'id' => $command->getId(),
            ]
        );
    }
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99

    /**
     * Get through all the users and databases to recompute all total from users.
     *
     * @param AdminUpdateTotalUsersCommand $command
     */
    public function handleAdminUpdateTotalUsersCommand(AdminUpdateTotalUsersCommand $command)
    {
        // Let's get all users first
        $user_ids = $this->connection->executeQuery('SELECT id FROM users')->fetchAll(\PDO::FETCH_ASSOC);

        foreach ($user_ids as $user_id) {
            $user_id = (int) $user_id;
            $total = $this->connection->executeQuery('SELECT SUM(somme) AS total FROM dons WHERE user_id = :user_id AND STATUS IN (1, 102)', [
                'user_id' => $user_id
100
            ])->fetchAll(\PDO::FETCH_COLUMN)[0];
101 102

            // Let's compute the cumul too
okhin's avatar
okhin committed
103
            // quoi = 'hoodie' somme = 250
104 105 106 107
            // quoi = 'pishirt' somme = 100
            // quoi = 'pibag' somme = 50
            // quoi = 'piplome' somme = 30
            $spent = $this->connection->executeQuery(
okhin's avatar
okhin committed
108
                "SELECT sum(IF(quoi = 'hoodie',250,0)) + sum(IF(quoi = 'pishirt', 100, 0)) + sum(IF(quoi = 'pibag', 50, 0)) + sum(IF(quoi = 'piplome', 30, 0)) AS spent FROM contreparties WHERE user_id = :user_id",
109
                [ 'user_id' => $user_id]
110
            )->fetchAll(\PDO::FETCH_COLUMN)[0];
okhin's avatar
okhin committed
111
            $this->connection->executeUpdate('UPDATE users SET total = :total, cumul = :cumul WHERE id = :user_id', ['total' => (int) $total, 'cumul' => (int) $total - (int) $spent, 'user_id' => $user_id]);
112 113
        }
    }
114
}