Item::getList()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 19
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 19
ccs 0
cts 8
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 7
nc 2
nop 2
crap 6
1
<?php
2
/**
3
 * AnimeDb package.
4
 *
5
 * @author    Peter Gribanov <[email protected]>
6
 * @copyright Copyright (c) 2011, Peter Gribanov
7
 * @license   http://opensource.org/licenses/GPL-3.0 GPL v3
8
 */
9
10
namespace AnimeDb\Bundle\CatalogBundle\Repository;
11
12
use Doctrine\ORM\EntityRepository;
13
use AnimeDb\Bundle\CatalogBundle\Entity\Item as ItemEntity;
14
15
/**
16
 * @author  Peter Gribanov <[email protected]>
17
 */
18
class Item extends EntityRepository
19
{
20
    /**
21
     * @return int
22
     */
23
    public function count()
24
    {
25
        return $this->getEntityManager()->createQuery('
26
            SELECT
27
                COUNT(i)
28
            FROM
29
                AnimeDbCatalogBundle:Item i
30
        ')->getSingleScalarResult();
31
    }
32
33
    /**
34
     * @param int|null $limit
35
     * @param int|null $offset
36
     *
37
     * @return ItemEntity[]
38
     */
39
    public function getList($limit = 0, $offset = 0)
40
    {
41
        $query = $this->getEntityManager()->createQuery('
42
            SELECT
43
                i
44
            FROM
45
                AnimeDbCatalogBundle:Item i
46
            ORDER BY
47
                i.id DESC
48
        ');
49
50
        if ($limit) {
51
            $query
52
                ->setFirstResult($offset)
53
                ->setMaxResults($limit);
54
        }
55
56
        return $query->getResult();
57
    }
58
59
    /**
60
     * @param ItemEntity $item
61
     *
62
     * @return ItemEntity[]
63
     */
64
    public function findDuplicate(ItemEntity $item)
65
    {
66
        // get all names
67
        $names = [$item->getName()];
68
        foreach ($item->getNames() as $name) {
69
            $names[] = $name->getName();
70
        }
71
72
        // find from item main name
73
        $duplicate = $this->getEntityManager()->createQuery('
74
            SELECT
75
                i
76
            FROM
77
                AnimeDbCatalogBundle:Item i
78
            WHERE
79
                i.name IN (:names)
80
        ')
81
            ->setParameter(':names', $names)
82
            ->getResult();
83
84
        // find frim item other names
85
        $item_names = $this->getEntityManager()->createQuery('
86
            SELECT
87
                n
88
            FROM
89
                AnimeDbCatalogBundle:Name n
90
            WHERE
91
                n.name IN (:names)
92
            GROUP BY
93
                n.item
94
        ')
95
            ->setParameter(':names', $names)
96
            ->getResult();
97
98
        foreach ($item_names as $item_name) {
99
            // element has been added
100
            foreach ($duplicate as $item) {
101
                if ($item === $item_name->getItem()) {
102
                    continue 2;
103
                }
104
            }
105
            $duplicate[] = $item_name->getItem();
106
        }
107
108
        return $duplicate;
109
    }
110
111
    /**
112
     * @param int|null $id
113
     *
114
     * @return \DateTime|null
115
     */
116 View Code Duplication
    public function getLastUpdate($id = null)
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...
117
    {
118
        if ($id) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $id of type integer|null is loosely compared to true; this is ambiguous if the integer can be zero. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
119
            $result = $this->getEntityManager()->createQuery('
120
                SELECT
121
                    i.date_update
122
                FROM
123
                    AnimeDbCatalogBundle:Item i
124
                WHERE
125
                    i.id = :id'
126
            )
127
                ->setParameter(':id', $id)
128
                ->getOneOrNullResult();
129
        } else {
130
            $result = $this->getEntityManager()->createQuery('
131
                SELECT
132
                    i.date_update
133
                FROM
134
                    AnimeDbCatalogBundle:Item i
135
                ORDER BY
136
                    i.date_update DESC'
137
            )
138
                ->setMaxResults(1)
139
                ->getOneOrNullResult();
140
        }
141
142
        return $result ? $result['date_update'] : null;
143
    }
144
}
145