UserFinder.php 1.71 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
<?php

namespace LQDN\Finder;

use Doctrine\DBAL\Connection;

class UserFinder
{
    private $connection;

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

    /**
     * findById
     *
     * @param int $id
     *
     * @return []
     */
    public function findById($id)
    {
        return $this->connection->fetchAssoc("SELECT * FROM users WHERE id = :id", ['id' => (int) $id]);
    }

Okhin's avatar
Okhin committed
28
29
30
31
32
33
34
35
36
37
38
39
    /**
     * findByEmail
     *
     * @param mixed $email
     *
     * @return []
     */
    public function findByEmail($email)
    {
        return $this->connection->fetchAssoc("SELECT * FROM users WHERE email = :email", ['email' => $email]);
    }

Okhin's avatar
Okhin committed
40
41
42
43
44
45
46
47
48
49
50
51
    /**
     * findByPseudo
     *
     * @param mixed pseudo
     *
     * @return []
     */
    public function findByPseudo($pseudo)
    {
        return $this->connection->fetchAssoc("SELECT * FROM users WHERE pseudo = :pseudo", ['pseudo' => $pseudo]);
    }

52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
    /**
     * The search from the admin.
     *
     * @param string $text
     * @param string $status
     * @param int $limit
     *
     * @return array
     */
    public function adminSearch($text, $status, $limit = 50)
    {
        $query = 'SELECT id, email, pseudo, total, cumul, expiration, status, commentaire FROM users WHERE 1 = 1';
        $params = [];

        if ('' !== $text) {
Guinness's avatar
Guinness committed
67
            $query .= " AND (pseudo LIKE :text OR email LIKE :text OR id LIKE :text)";
68
69
70
71
72
73
74
75
76
77
78
79
80
            $params['text'] = "%$text%";
        }

        if ('' !== $status) {
            $query .= " AND status = :status";
            $params['status'] = $status;
        }

        $query .= " LIMIT $limit";

        return $this->connection->fetchAll($query, $params);
    }
}