LogRepository::getLogById()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 12
ccs 9
cts 9
cp 1
rs 9.9666
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2
1
<?php
2
3
namespace Norsys\LogsBundle\Model;
4
5
use Doctrine\DBAL\Connection;
6
7
/**
8
 * Class LogRepository
9
 */
10
class LogRepository
11
{
12
    /**
13
     * @var Connection $conn
14
     */
15
    protected $conn;
16
17
    /**
18
     * @var string $tableName
19
     */
20
    private $tableName;
21
22
    /**
23
     * @param Connection $conn
24
     * @param string     $tableName
25
     */
26
    public function __construct(Connection $conn, string $tableName)
27
    {
28 1
        $this->conn      = $conn;
29 1
        $this->tableName = $tableName;
30 1
    }
31
32
    /**
33
     * @return \Doctrine\DBAL\Query\QueryBuilder
34
     */
35
    protected function createQueryBuilder()
36
    {
37 1
        return $this->conn->createQueryBuilder();
38
    }
39
40
    /**
41
     * Initialize a QueryBuilder of latest log entries.
42
     *
43
     * @return \Doctrine\DBAL\Query\QueryBuilder
44
     */
45
    public function getLogsQueryBuilder()
46
    {
47 1
        return $this->createQueryBuilder()
48 1
                    ->select('l.channel')
49 1
                    ->addSelect('l.level')
50 1
                    ->addSelect('l.level_name')
51 1
                    ->addSelect('l.message')
52 1
                    ->addSelect('MAX(l.id) AS id')
53 1
                    ->addSelect('MAX(l.datetime) AS datetime')
54 1
                    ->addSelect('COUNT(l.id) AS count')
55 1
                    ->from($this->tableName, 'l')
56 1
                    ->groupBy('l.channel, l.level, l.level_name, l.message')
57 1
                    ->orderBy('datetime', 'DESC');
58
    }
59
60
    /**
61
     * Retrieve a log entry by his ID.
62
     *
63
     * @param integer $id
64
     *
65
     * @return Log|null
66
     */
67
    public function getLogById(int $id)
68
    {
69 1
        $log = $this->createQueryBuilder()
70 1
                    ->select('l.*')
71 1
                    ->from($this->tableName, 'l')
72 1
                    ->where('l.id = :id')
73 1
                    ->setParameter(':id', $id)
74 1
                    ->execute()
75 1
                    ->fetch();
76
77 1
        if (false !== $log) {
78 1
            return new Log($log);
79
        }
80
    }
81
}
82