AddressHandler.php 6.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
<?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;
    }

    /**
     * Delete an address. If there's a default adress, we should replace it in
     * dons and contreparties. If there's none and the adress is used
     * in contreparties, we should raise an error.
     *
     * @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) {
            // First lets' check if there is a default Address we could use
            $default = $this->getDefaultAddress($user_id);
            $params = ['id' => $command->getAddressId(),
                'default_id' => $default];

            if ($default != '' and $default != $addressId) {
                // We have a default address which is not the current one
43
                $this->connection->executeUpdate("UPDATE dons SET adresse_id = :default_id WHERE adresse_id = :id", $params);
44 45
                $this->connection->executeUpdate("UPDATE contreparties SET adresse_id= :default_id WHERE adresse_id = :id", $params);
            } else {
46
                // We do not have a default address and the address is used somehow
47 48 49 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 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 116 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
                throw new AddressUsedException();
            };
        };
        $this->connection->executeUpdate("DELETE FROM adresses WHERE id = :id", ['id' => $addressId]);
    }

    /**
     * Update an address.
     *
     * @param AddressUpdateCommand $command
     */
    public function handleAddressUpdateCommand(AddressUpdateCommand $command)
    {
        $userId = $command->getUserId();
        if ($isDefault = $command->isDefault()) {
            $this->undefaultAddresses($userId);
        }
        $query = <<<EOF
UPDATE adresses
SET
  alias = :alias,
  adresse = :address,
  adresse2 = :address2,
  codepostal = :postal_code,
  ville = :city,
  pays = :country,
  etat = :state,
  defaut = :default
WHERE id = :address_id
  AND user_id = :user_id;
EOF;

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

    /**
     * Create an address.
     *
     * @param AddressCreateCommand $command
     */
    public function handleAddressCreateCommand(AddressCreateCommand $command)
    {
        $userId = $command->getUserId();
        $alias = $command->getAlias();
        if ($this->addressExists($userId, $alias)) {
            throw new AddressAlreadyExistsException();
        }

        if ($isDefault = $command->isDefault()) {
            $this->undefaultAddresses($userId);
        }

        $query =<<<EOF
INSERT INTO adresses(user_id, nom, alias, adresse, adresse2, codepostal, ville, etat, pays, defaut)
VALUES (:user_id, :name, :alias, :address, :address2, :postal_code, :city, :state, :country, :default)
EOF;

        $stmt = $this->connection->prepare($query);
        $stmt->bindValue('user_id', $command->getUserId());
        $stmt->bindValue('name', $command->getName());
        $stmt->bindValue('alias', $command->getAlias());
        $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->bindValue('default', $isDefault);
        $stmt->execute();
    }

    /**
     * Undefault addresses of a given user.
     *
     * @param int $userId
     */
    private function undefaultAddresses($userId)
    {
        $this->connection->executeUpdate("UPDATE adresses SET defaut = FALSE WHERE user_id = :user_id", [
            'user_id' => $userId,
        ]);
    }

    /**
     * Get the default address of a given user.
     *
     * @param int $userId
     *
     * @return int
     */
    private function getDefaultAddress($userId)
    {
        return (int) $this->connection->fetchColumn(
            "SELECT id FROM adresses WHERE user_id = :user_id AND defaut = TRUE",
            [
                'user_id' => $userId,
154 155
            ],
            0
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
        );
    }

    /**
     * Check if an address already exists.
     *
     * @param int $userId
     * @param string $alias
     *
     * @return bool
     */
    private function addressExists($userId, $alias)
    {
        return (bool) $this->connection->fetchColumn(
            "SELECT 1 FROM adresses WHERE user_id = :user_id AND alias = :alias",
            [
                'user_id' => $userId,
                'alias' => $alias,
174 175
            ],
            0
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
        );
    }

    /**
     * Check if an adress is used.
     *
     * @param int $addressId
     *
     * @return bool
     */
    private function addressUsed($addressId)
    {
        $count = (int) $this->connection->fetchColumn(
            "SELECT count(1) FROM contreparties, dons WHERE contreparties.adresse_id = :id OR dons.adresse_id = :id",
            [
                'id' => $addressId,
192 193
            ],
            0
194 195 196 197
        );
        return ($count > 0);
    }
}