CounterpartFinder.php 1.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 29 30 31 32 33 34 35 36
<?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()) {
            $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
37
SELECT d.id as id, a.nom as pseudo, d.pdf as pdf
38 39
FROM dons d
JOIN adresses a ON a.id = d.adresse_id
40 41
JOIN contreparties c ON c.id = d.taille
WHERE d.taille = :size
42 43 44
EOQ;

                $pdf = $this->connection->fetchAssoc($query, [
45
                    'size' => $counterpart['taille']
46 47
                ]);
                if (false !== $pdf) {
48 49 50
                    $counterpart['pdf_id'] = $pdf['id'];
                    $counterpart['pdf_nom'] = $pdf['pseudo'];
                    $counterpart['pdf_url'] = $pdf['pdf'];
51 52 53 54 55 56 57 58 59
                }
            }

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

        return $counterparts;
    }
}