DonationHandler.php 2.49 KB
Newer Older
1
2
3
4
5
<?php

namespace LQDN\Handler;

use Doctrine\DBAL\Connection;
Okhin's avatar
Okhin committed
6
use LQDN\Command\DonationCreateCommand;
7
8
9
use LQDN\Command\DonationInvalidateCommand;
use LQDN\Command\DonationResetPdfCommand;
use LQDN\Command\DonationValidateCommand;
10
use LQDN\Command\DonationIncStatusCommand;
11
12
13
14
15
16
17
18
19
20

class DonationHandler
{
    private $connection;

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

Okhin's avatar
Okhin committed
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
    /**
     * Create a donation
     *
     * @param DonationCreateCommand $command
     */
    public function handleDonationCreateCommand(DonationCreateCommand $command)
    {
        $query =<<<EOF
INSERT INTO dons(status, datec, somme, user_id, public, cumul)
VALUES(:status, :datec, :somme, :user_id, :public, :cumul)
EOF;
        $stmt = $this->connection->prepare($query);
        $stmt->bindvalue('status', $command->getStatus());
        $stmt->bindvalue('datec', $command->getDateC());
        $stmt->bindvalue('somme', $command->getSomme());
        $stmt->bindvalue('user_id', $command->getUserId());
        $stmt->bindvalue('public', $command->getPublic());
        $stmt->bindvalue('cumul', $command->getCumul());
        $stmt->execute();
    }

42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
    /**
     * Validate a donation.
     *
     * @param DonationValidateCommand $command
     */
    public function handleDonationValidateCommand(DonationValidateCommand $command)
    {
        $this->connection->executeUpdate('UPDATE dons SET status = status + 1 WHERE id = :id AND status IN (0, 100)', ['id' => $command->getId()]);
    }

    /**
     * Invalidate a donation.
     *
     * @param DonationInvalidateCommand $command
     */
    public function handleDonationInvalidateCommand(DonationInvalidateCommand $command)
    {
        $this->connection->executeUpdate('UPDATE dons SET status = status - 1 WHERE id = :id AND status IN (1, 101)', ['id' => $command->getId()]);
    }

    /**
     * Reset the PDF of a donation.
     *
     * @param DonationResetPdfCommand $command
     */
    public function handleDonationResetPdfCommand(DonationResetPdfCommand $command)
    {
        $this->connection->executeUpdate('UPDATE dons SET pdf = "" WHERE id = :id', ['id' => $command->getId()]);
    }
71
72
73
74
75
76
77
78
79
80

    /**
     * Increase the status of a donation
     *
     * @param DonationIncStatusCommand $command
     */
    public function handleDonationIncStatusCommand(DonationIncStatusCommand $command)
    {
        $this->connection->executeUpdate('UPDATE dons SET status = status + 1 WHERE id = :id', ['id' => $command->getId()]);
    }
81
}