UserHandler.php 3.75 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
28
29
30
31
32
33
34

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)
    {
        $this->connection->executeUpdate('UPDATE users SET pseudo = :username, email = :email, commentaire = :comment WHERE id = :id', [
            'username' => $command->getUsername(),
            'email' => $command->getEmail(),
            'comment' => $command->getComment(),
            'id' => $command->getId(),
        ]);
    }
35

Okhin's avatar
Okhin committed
36
37
38
39
40
41
42
    /**
     * Create a user in database
     *
     * @param UserCreateCommand $command
     */
    public function handleUserCreateCommand(UserCreateCommand $command)
    {
43
44
45
46
47
48
49
        $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
50
51
    }

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
    /**
     * 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(),
            ]
        );
    }
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97

    /**
     * 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
98
99
100
101
102
103
104
105
106
107
108
109
            ])->fetchColumn();

            // Let's compute the cumul too
            // quoi = 'hoopie' somme = 250
            // quoi = 'pishirt' somme = 100
            // quoi = 'pibag' somme = 50
            // quoi = 'piplome' somme = 30
            $spent = $this->connection->executeQuery(
                "SELECT sum(IF(quoi = 'hoopie',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",
                [ 'user_id' => $user_id]
            )->fetchColumn();
            $this->connection->executeUpdate('UPDATE users SET total = :total, cumul = :cumul WHERE id = :user_id', ['total' => $total, 'cumul' => $total - $spent, 'user_id' => $user_id]);
110
111
        }
    }
112
}