DoctrineQbIndexer   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 103
Duplicated Lines 28.16 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 0
dl 29
loc 103
ccs 0
cts 74
cp 0
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A getRangeCriterias() 0 14 1
A getItemsByIds() 14 14 1
A getItemsByInterval() 15 15 1
A processItems() 0 4 1
A serializeItem() 0 4 1
A getQueryBuilder() 0 4 1
A getEntityName() 0 4 1
A getRootIdentifier() 0 4 1
A getHydrationMode() 0 4 1
A checkConnection() 0 8 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Brouzie\Sphinxy\Indexer;
4
5
use Doctrine\ORM\AbstractQuery;
6
use Doctrine\ORM\EntityManager;
7
8
abstract class DoctrineQbIndexer implements IndexerInterface
9
{
10
    /**
11
     * @var EntityManager
12
     */
13
    protected $em;
14
15
    public function __construct(EntityManager $em)
16
    {
17
        $this->em = $em;
18
19
        $this->em->getConfiguration()->setSQLLogger(null);
20
        $this->em->getConnection()->getConfiguration()->setSQLLogger(null);
21
    }
22
23
    public function getRangeCriterias()
24
    {
25
        $this->checkConnection();
26
27
        $rootId = $this->getRootIdentifier();
28
        $qb = $this->getQueryBuilder();
29
30
        $range = $qb
31
            ->select($qb->expr()->min($rootId), $qb->expr()->max($rootId))
32
            ->getQuery()
33
            ->getSingleResult();
34
35
        return array('min' => array_shift($range), 'max' => array_shift($range));
36
    }
37
38 View Code Duplication
    public function getItemsByIds(array $ids)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
39
    {
40
        $this->checkConnection();
41
        $rootId = $this->getRootIdentifier();
42
43
        $items = $this->getQueryBuilder()
44
            ->andWhere(sprintf('%s IN (:ids)', $rootId))
45
            ->setParameter('ids', $ids)
46
            ->getQuery()
47
            ->getResult($this->getHydrationMode())
48
        ;
49
50
        return $items;
51
    }
52
53 View Code Duplication
    public function getItemsByInterval($idFrom, $idTo)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
54
    {
55
        $this->checkConnection();
56
        $rootId = $this->getRootIdentifier();
57
58
        $items = $this->getQueryBuilder()
59
            ->andWhere(sprintf('%s >= :min AND %s < :max', $rootId, $rootId))
60
            ->setParameter('min', $idFrom)
61
            ->setParameter('max', $idTo)
62
            ->getQuery()
63
            ->getResult($this->getHydrationMode())
64
        ;
65
66
        return $items;
67
    }
68
69
    public function processItems(array $items)
70
    {
71
        return $items;
72
    }
73
74
    public function serializeItem($item)
75
    {
76
        return $item;
77
    }
78
79
    /**
80
     * @return \Doctrine\ORM\QueryBuilder
81
     */
82
    protected function getQueryBuilder()
83
    {
84
        return $this->em->getRepository($this->getEntityName())->createQueryBuilder('e');
85
    }
86
87
    protected function getEntityName()
88
    {
89
        throw new \BadMethodCallException('You should override this method.');
90
    }
91
92
    protected function getRootIdentifier()
93
    {
94
        return 'e.id';
95
    }
96
97
    protected function getHydrationMode()
98
    {
99
        return AbstractQuery::HYDRATE_ARRAY;
100
    }
101
102
    private function checkConnection()
103
    {
104
        $connection = $this->em->getConnection();
105
        if ($connection->ping() === false) {
106
            $connection->close();
107
            $connection->connect();
108
        }
109
    }
110
}
111