CounterpartHandler.php 3.29 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;
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
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();
28
        // Let's check if the counterpart exist
okhin's avatar
okhin committed
29
        $this->connection->executeUpdate(
30 31
            "DELETE FROM contreparties WHERE id = :id",
            ['id' => $counterpartId]
okhin's avatar
okhin committed
32
        );
33 34 35 36 37 38 39 40 41 42
    }

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

        $query =<<<EOF
46 47
INSERT INTO contreparties(datec, user_id, adresse_id, quoi, taille, status, commentaire, parent)
VALUES (:datec, :user_id, :adresse_id, :quoi, :taille, :status, :commentaire, :parent)
48 49
EOF;
        $stmt = $this->connection->prepare($query);
50
        $stmt->bindValue('datec', $command->getDateCreation());
51 52 53 54 55
        $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());
56
        $stmt->bindValue('commentaire', $command->getCommentaire());
57
        $stmt->bindValue('parent', $command->getParent());
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 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
        $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
115 116
    }
}