CounterpartHandler.php 4.95 KB
Newer Older
1
2
3
4
5
6
7
<?php

namespace LQDN\Handler;

use Doctrine\DBAL\Connection;
use LQDN\Command\CounterpartCreateCommand;
use LQDN\Command\CounterpartDeleteCommand;
8
use LQDN\Command\CounterpartChangeStateCommand;
Okhin's avatar
Okhin committed
9
use LQDN\Command\AdminUpdateParentCommand;
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
use LQDN\Exception\CounterpartAlreadyExistsException;

class CounterpartHandler
{
    private $connection;

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

    /**
     * Delete a counterpart
     *
     * @param CounterpartDeleteCommand $command
     */
    public function handleCounterpartDeleteCommand(CounterpartDeleteCommand $command)
    {
        $counterpartId = $command->getCounterpartId();
29
        // Let's check if the counterpart exist
Okhin's avatar
Linting    
Okhin committed
30
        $this->connection->executeUpdate(
31
32
            "DELETE FROM contreparties WHERE id = :id",
            ['id' => $counterpartId]
Okhin's avatar
Linting    
Okhin committed
33
        );
34
35
36
37
38
39
40
41
42
43
    }

    /**
     * Create a counterpart
     *
     * @param CounterpartCreateCommand $command
     */
    public function handleCounterpartCreateCommand(CounterpartCreateCommand $command)
    {
        $userId = $command->getUserId();
44
        $adresseId = $command->getAddressId();
45
46

        $query =<<<EOF
47
48
INSERT INTO contreparties(datec, user_id, adresse_id, quoi, taille, status, commentaire, parent)
VALUES (:datec, :user_id, :adresse_id, :quoi, :taille, :status, :commentaire, :parent)
49
50
EOF;
        $stmt = $this->connection->prepare($query);
51
        $stmt->bindValue('datec', $command->getDateCreation());
52
53
54
55
56
        $stmt->bindValue('user_id', $command->getUserId());
        $stmt->bindValue('adresse_id', $command->getAddressId());
        $stmt->bindValue('quoi', $command->getQuoi());
        $stmt->bindValue('taille', $command->getTaille());
        $stmt->bindValue('status', $command->getStatus());
57
        $stmt->bindValue('commentaire', $command->getCommentaire());
58
        $stmt->bindValue('parent', $command->getParent());
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
        $stmt->execute();
    }

    /**
     * Chnge the state of a counterpart
     *
     * @param CounterpartChangeStateCommand $command
     */
    public function handleCounterpartChangeStateCommand(CounterpartChangeStateCommand $command)
    {
        $counterpartId = $command->getCounterpartId();
        $status = $command->getState();

        $this->connection->executeUpdate(
            "UPDATE contreparties SET status = :status WHERE id=:id",
            [
                'status' => $status,
                'id' => $counterpartId,
            ]
        );
    }

    /**
     * Test if a counterpart exists
     *
     * @param int $counterpartId
     *
     * @return bool
     */
    private function counterpartExists($counterpartId)
    {
        return (bool) $this->connection->fetchColumn(
            "SELECT 1 FROM contreparties WHERE id = :counterpart_id",
            [
                'counterpart_id' => $counterpartId,
            ],
            0
        );
    }

    /**
     * Test if the counterpart can be deleted
     *
     * @param int $countepartId
     *
     * @return bool
     */
    private function counterpartUsed($counterpartId)
    {
        $status = (int) $this->connection->fetchColumn(
            "SELECT status FROM contreparties WHERE id = :id",
            [
                'id' => $counterpartId,
            ],
            0
        );
        return ($status == 2); // status 2 is delivered counterparts
116
    }
Okhin's avatar
Okhin committed
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154

    /**
     * We try to guess how to group all counterparts, if they're created on the same day, they've been asked
     * as a group of counterparts, so we should parent them together.
     *
     * @param AdminUpdateParentCommand $command
     */
    public function handleAdminUpdateParentCommand(AdminUpdateParentCommand $command)
    {
        // Let's get users first
        $user_ids = $this->connection->executeQuery('SELECT id FROM users')->fetchAll(\PDO::FETCH_COLUMN);

        foreach ($user_ids as $user_id) {
            $user_id =(int) $user_id;

            // Those are the parents ID, grouped by day.
            $parents = $this->connection->executeQuery(
                "select id from contreparties WHERE user_id = :user_id group by user_id, dayofyear(datec) order by field(quoi, 'piplome', 'pibag', 'pishirt', 'hoodie')",
                [ 'user_id' => $user_id]
            )->fetchAll(\PDO::FETCH_COLUMN);

            foreach ($parents as $parent) {
                $parent = (int) $parent;

                // Let's get the date of this one
                $day = $this->connection->executeQuery(
                    'SELECT DAYOFYEAR(datec) FROM contreparties WHERE id = :parent',
                    ['parent' => $parent]
                )->fetchAll(\PDO::FETCH_COLUMN)[0];

                // Let's update the parent
                $this->connection->executeUpdate(
                    'UPDATE contreparties SET parent = :parent WHERE DAYOFYEAR(datec) = :day AND user_id = :user_id',
                    ['parent' => $parent, 'day' => (int) $day, 'user_id' => $user_id]
                );
            }
        }
    }
155
}