Logging::flush()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 2
b 0
f 0
nc 1
nop 1
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
1
<?php
2
/**
3
 * @author Gerard van Helden <[email protected]>
4
 * @copyright Zicht Online <http://zicht.nl>
5
 */
6
namespace Zicht\Bundle\UrlBundle\Logging;
7
8
use Doctrine\ORM\EntityManager;
9
use Symfony\Component\HttpFoundation\Request;
10
use Zicht\Bundle\UrlBundle\Entity\ErrorLog;
11
12
/**
13
 * Keeps a database log of URL issues, such as 404 and 500 errors
14
 */
15
class Logging
16
{
17
    /**
18
     * Constructor
19
     *
20
     * @param \Doctrine\ORM\EntityManager $manager
21
     */
22 1
    public function __construct(EntityManager $manager)
23
    {
24 1
        $this->manager = $manager;
0 ignored issues
show
Bug Best Practice introduced by
The property manager does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
25 1
    }
26
27
28
    /**
29
     * Create a log entry for the passed request.
30
     *
31
     * @param \Symfony\Component\HttpFoundation\Request $request
32
     * @param string $message
33
     * @return \Zicht\Bundle\UrlBundle\Entity\ErrorLog
34
     */
35 2
    public function createLog(Request $request, $message)
36
    {
37 2
        return new ErrorLog(
38 2
            $message,
39 2
            new \DateTime(),
40 2
            $request->headers->get('referer', null),
41 2
            $request->headers->get('user-agent', null),
42 2
            $request->getClientIp(),
43 2
            $request->getRequestUri()
44
        );
45
    }
46
47
48
    /**
49
     * Persist the log and flush the manager.
50
     *
51
     * @param ErrorLog $entry
52
     * @return void
53
     */
54 1
    public function flush($entry)
55
    {
56 1
        $this->manager->persist($entry);
57 1
        $this->manager->flush($entry);
58 1
    }
59
}
60