AddressHandler.php 2.92 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
<?php

namespace LQDN\Handler;

use Doctrine\DBAL\Connection;
use LQDN\Command\AddressCreateCommand;
use LQDN\Command\AddressDeleteCommand;
use LQDN\Command\AddressUpdateCommand;
use LQDN\Exception\AddressAlreadyExistsException;
use LQDN\Exception\AddressUsedException;

class AddressHandler
{
    private $connection;

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

    /**
22
     * Delete an address.
23 24 25 26 27 28 29 30 31 32 33
     *
     * @param AddressDeleteCommand $command
     */
    public function handleAddressDeleteCommand(AddressDeleteCommand $command)
    {
        // Before deleting, we need to recover the adresse_id and remove it from
        // dons and contreparties table.
        $user_id = $command->getUserId();
        $addressId = $command->getAddressId();
        // Let's check if the address is used
        if ($this->addressUsed($addressId) == true) {
34 35
            // The address is used somehow
            throw new AddressUsedException();
36 37 38 39 40 41 42 43 44 45 46 47 48 49
        };
        $this->connection->executeUpdate("DELETE FROM adresses WHERE id = :id", ['id' => $addressId]);
    }

    /**
     * Create an address.
     *
     * @param AddressCreateCommand $command
     */
    public function handleAddressCreateCommand(AddressCreateCommand $command)
    {
        $userId = $command->getUserId();

        $query =<<<EOF
50 51
REPLACE INTO adresses(user_id, nom, adresse, adresse2, codepostal, ville, etat, pays)
VALUES (:user_id, :name, :address, :address2, :postal_code, :city, :state, :country)
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
EOF;

        $stmt = $this->connection->prepare($query);
        $stmt->bindValue('user_id', $command->getUserId());
        $stmt->bindValue('name', $command->getName());
        $stmt->bindValue('address', $command->getAddress());
        $stmt->bindValue('address2', $command->getAddress2());
        $stmt->bindValue('postal_code', $command->getPostalCode());
        $stmt->bindValue('city', $command->getCity());
        $stmt->bindValue('state', $command->getState());
        $stmt->bindValue('country', $command->getCountry());
        $stmt->execute();
    }

    /**
     * Check if an address already exists.
     *
     * @param int $userId
     * @param string $alias
     *
     * @return bool
     */
74
    private function addressExists($userId)
75 76
    {
        return (bool) $this->connection->fetchColumn(
77
            "SELECT 1 FROM adresses WHERE user_id = :user_id",
78 79
            [
                'user_id' => $userId,
80 81
            ],
            0
82 83 84 85 86 87 88 89 90 91 92 93 94
        );
    }

    /**
     * Check if an adress is used.
     *
     * @param int $addressId
     *
     * @return bool
     */
    private function addressUsed($addressId)
    {
        $count = (int) $this->connection->fetchColumn(
95
            "SELECT count(1) FROM contreparties, dons WHERE contreparties.adresse_id = :id AND contreparties.status = 1;",
96 97
            [
                'id' => $addressId,
98 99
            ],
            0
100 101 102 103
        );
        return ($count > 0);
    }
}