NotFoundManager::removeForRedirect()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 10
ccs 6
cts 6
cp 1
crap 2
rs 9.9332
c 0
b 0
f 0
1
<?php
2
3
namespace Zenstruck\RedirectBundle\Service;
4
5
use Doctrine\Persistence\ObjectManager;
6
use Symfony\Component\HttpFoundation\Request;
7
use Zenstruck\RedirectBundle\Model\NotFound;
8
use Zenstruck\RedirectBundle\Model\Redirect;
9
10
/**
11
 * @author Kevin Bond <[email protected]>
12
 */
13
class NotFoundManager
14
{
15
    private $class;
16
17
    private $om;
18
19
    /**
20
     * @param string $class The NotFound class name
21
     */
22 7
    public function __construct($class, ObjectManager $om)
23
    {
24 7
        $this->class = $class;
25 7
        $this->om = $om;
26 7
    }
27
28
    /**
29
     * @return NotFound
30
     */
31 2
    public function createFromRequest(Request $request)
32
    {
33 2
        $notFound = new $this->class(
34 2
            $request->getPathInfo(),
35 2
            $request->getUri(),
36 2
            $request->server->get('HTTP_REFERER')
37
        );
38
39 2
        $this->om->persist($notFound);
40 2
        $this->om->flush();
41
42 2
        return $notFound;
43
    }
44
45
    /**
46
     * Deletes NotFound entities for a Redirect's path.
47
     */
48 6
    public function removeForRedirect(Redirect $redirect)
49
    {
50 6
        $notFounds = $this->om->getRepository($this->class)->findBy(['path' => $redirect->getSource()]);
51
52 6
        foreach ($notFounds as $notFound) {
53 2
            $this->om->remove($notFound);
54
        }
55
56 6
        $this->om->flush();
57 6
    }
58
}
59