CounterpartFinder.php 1.54 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
<?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
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
Okhin's avatar
Okhin committed
40
WHERE d.id = :don_id
41 42 43
EOQ;

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

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

        return $counterparts;
    }
}