RedirectManager::findAndUpdate()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 8
cts 8
cp 1
rs 9.7666
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2
1
<?php
2
3
namespace Zenstruck\RedirectBundle\Service;
4
5
use Doctrine\Persistence\ObjectManager;
6
use Zenstruck\RedirectBundle\Model\Redirect;
7
8
/**
9
 * @author Kevin Bond <[email protected]>
10
 */
11
class RedirectManager
12
{
13
    private $class;
14
15
    private $om;
16
17
    /**
18
     * @param string $class The Redirect class name
19
     */
20 5
    public function __construct($class, ObjectManager $om)
21
    {
22 5
        $this->class = $class;
23 5
        $this->om = $om;
24 5
    }
25
26
    /**
27
     * @param string $source
28
     *
29
     * @return Redirect|null
30
     */
31 5
    public function findAndUpdate($source)
32
    {
33
        /** @var Redirect|null $redirect */
34 5
        $redirect = $this->om->getRepository($this->class)->findOneBy(['source' => $source]);
35
36 5
        if (null === $redirect) {
37 2
            return null;
38
        }
39
40 3
        $redirect->increaseCount();
41 3
        $redirect->updateLastAccessed();
42 3
        $this->om->flush();
43
44 3
        return $redirect;
45
    }
46
}
47