DoctrineScriptCache   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 7
c 1
b 0
f 1
lcom 1
cbo 4
dl 0
loc 63
ccs 0
cts 32
cp 0
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A get() 0 10 2
A set() 0 14 2
A clear() 0 9 2
1
<?php
2
3
namespace Dekalee\AdbackAnalyticsBundle\Driver;
4
5
use Dekalee\AdbackAnalytics\Driver\ScriptCacheInterface;
6
use Dekalee\AdbackAnalytics\Driver\SqlScriptCache;
7
use Dekalee\AdbackAnalyticsBundle\Entity\ApiCache;
8
use Dekalee\AdbackAnalyticsBundle\Repository\ApiCacheRepository;
9
use Doctrine\Common\Persistence\ObjectManager;
10
11
/**
12
 * Class DoctrineScriptCache
13
 */
14
class DoctrineScriptCache extends SqlScriptCache implements ScriptCacheInterface
15
{
16
    protected $repository;
17
    protected $entityManager;
18
19
    /**
20
     * @param ApiCacheRepository $repository
21
     * @param ObjectManager      $manager
22
     */
23
    public function __construct(ApiCacheRepository $repository, ObjectManager $manager)
24
    {
25
        $this->repository = $repository;
26
        $this->entityManager = $manager;
27
    }
28
29
    /**
30
     * @param string $key
31
     *
32
     * @return string|null
33
     */
34
    protected function get($key)
35
    {
36
        $element = $this->repository->findOneByKey($key);
37
38
        if ($element instanceof ApiCache) {
39
            return $element->getValue();
40
        }
41
42
        return null;
43
    }
44
45
    /**
46
     * @param string $key
47
     * @param string $value
48
     */
49
    protected function set($key, $value)
50
    {
51
        $element = $this->repository->findOneByKey($key);
52
53
        if (!$element instanceof ApiCache) {
54
            $element = new ApiCache();
55
            $element->setKey($key);
56
            $this->entityManager->persist($element);
57
        }
58
59
        $element->setValue($value);
60
61
        $this->entityManager->flush($element);
62
    }
63
64
    /**
65
     * @param string $key
66
     */
67
    protected function clear($key)
68
    {
69
        $element = $this->repository->findOneByKey($key);
70
71
        if (!$element instanceof ApiCache) {
72
            $this->entityManager->remove($element);
0 ignored issues
show
Documentation introduced by
$element is of type null, but the function expects a object.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
73
            $this->entityManager->flush($element);
74
        }
75
    }
76
}
77