CounterpartFinder.php 4.58 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
<?php

namespace LQDN\Finder;

use Doctrine\DBAL\Connection;

class CounterpartFinder
{
    private $connection;

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

    /**
     * Return all counterparts of a given user.
     *
     * @param mixed $userId
     *
     * @return array
     */
    public function findByUserId($userId)
    {
        $userId = (int) $userId;
        $counterparts = [];
        $stmt = $this->connection->executeQuery('SELECT * FROM contreparties WHERE user_id = :user_id', ['user_id' => $userId]);
        while ($counterpart = $stmt->fetch()) {
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
            $counterpart['pdf_id'] = '';
            $counterpart['pdf_nom'] = '';
            $counterpart['pdf_url'] = '';

            // Of course, piplomes are messy
            // @TODO: This part is probably not working.
            if ('piplome' === $counterpart['quoi']) {
                $query = <<<EOQ
SELECT d.id as id, a.nom as pseudo, d.pdf as pdf
FROM dons d
JOIN adresses a ON a.id = d.adresse_id
JOIN contreparties c ON c.id = d.taille
WHERE d.taille = :size
EOQ;

                $pdf = $this->connection->fetchAssoc($query, [
                    'size' => $counterpart['taille']
                ]);
                if (false !== $pdf) {
                    $counterpart['pdf_id'] = $pdf['id'];
                    $counterpart['pdf_nom'] = $pdf['pseudo'];
                    $counterpart['pdf_url'] = $pdf['pdf'];
                }
            }

            $counterparts[$counterpart['id']] = $counterpart;
        }

        return $counterparts;
    }

60 61 62 63 64 65 66 67 68 69 70 71
    /**
     * return the last inserted id
     *
     * @return int
     */
    public function getLastInsertedId()
    {
        return (int) $this->connection->fetchColumn(
            "SELECT AUTO_INCREMENT FROM information_schema.tables WHERE table_name = 'contreparties' AND table_schema = DATABASE()"
        );
    }

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
    /**
     * Return all counterparts in a specific status
     *
     * @param mixed $status
     *
     * @return array
     */
    public function findByStatus($status)
    {
        $status = (int) $status;
        $counterparts = [];
        $stmt = $this->connection->executeQuery('SELECT * FROM contreparties WHERE status = :status', ['status' => $status]);
        while ($counterpart = $stmt->fetch()) {
            $counterpart['pdf_id'] = '';
            $counterpart['pdf_nom'] = '';
            $counterpart['pdf_url'] = '';

            // Of course, piplomes are messy
            // @TODO: This part is probably not working.
            if ('piplome' === $counterpart['quoi']) {
                $query = <<<EOQ
SELECT d.id as id, a.nom as pseudo, d.pdf as pdf
FROM dons d
JOIN adresses a ON a.id = d.adresse_id
JOIN contreparties c ON c.id = d.taille
WHERE d.taille = :size
EOQ;

                $pdf = $this->connection->fetchAssoc($query, [
                    'size' => $counterpart['taille']
                ]);
                if (false !== $pdf) {
                    $counterpart['pdf_id'] = $pdf['id'];
                    $counterpart['pdf_nom'] = $pdf['pseudo'];
                    $counterpart['pdf_url'] = $pdf['pdf'];
                }
            }

            $counterparts[$counterpart['id']] = $counterpart;
        }

        return $counterparts;
    }

    /** Return all counterparts by quoi
     *
     * @param mixed $quoi
     *
     * @return array
     */
    public function findByQuoi($quoi)
    {
        $quoi = (string) $quoi;
        $counterparts = [];
        $stmt = $this->connection->executeQuery('SELECT * FROM contreparties WHERE quoi = :quoi', ['quoi' => $quoi]);
        while ($counterpart = $stmt->fetch()) {
128 129 130 131 132 133 134 135
            $counterpart['pdf_id'] = '';
            $counterpart['pdf_nom'] = '';
            $counterpart['pdf_url'] = '';

            // Of course, piplomes are messy
            // @TODO: This part is probably not working.
            if ('piplome' === $counterpart['quoi']) {
                $query = <<<EOQ
136
SELECT d.id as id, a.nom as pseudo, d.pdf as pdf
137 138
FROM dons d
JOIN adresses a ON a.id = d.adresse_id
139 140
JOIN contreparties c ON c.id = d.taille
WHERE d.taille = :size
141 142 143
EOQ;

                $pdf = $this->connection->fetchAssoc($query, [
144
                    'size' => $counterpart['taille']
145 146
                ]);
                if (false !== $pdf) {
147 148 149
                    $counterpart['pdf_id'] = $pdf['id'];
                    $counterpart['pdf_nom'] = $pdf['pseudo'];
                    $counterpart['pdf_url'] = $pdf['pdf'];
150 151 152 153 154 155 156 157 158
                }
            }

            $counterparts[$counterpart['id']] = $counterpart;
        }

        return $counterparts;
    }
}