UserRepository::getQueryBuilder()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 6
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 6
loc 6
ccs 0
cts 6
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 0
crap 2
1
<?php
2
3
/**
4
 * This file is part of Packy.
5
 *
6
 * (c) Peter Nijssen
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace AppBundle\Repository;
13
14
use AppBundle\Entity\User;
15
use Doctrine\Common\Persistence\ObjectManager;
16
use Doctrine\ORM\QueryBuilder;
17
18 View Code Duplication
class UserRepository
0 ignored issues
show
Duplication introduced by
This class 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...
19
{
20
    /**
21
     * Entity alias.
22
     *
23
     * @var string
24
     */
25
    const ENTITY_ALIAS = 'u';
26
27
    /**
28
     * Entity to use.
29
     *
30
     * @var string
31
     */
32
    const ENTITY_CLASS = 'AppBundle:User';
33
34
    /**
35
     * Entity manager.
36
     *
37
     * @var ObjectManager
38
     */
39
    private $objectManager;
40
41
    /**
42
     * Constructor.
43
     *
44
     * @param ObjectManager $objectManager
45
     */
46
    public function __construct(ObjectManager $objectManager)
47
    {
48
        $this->objectManager = $objectManager;
49
    }
50
51
    /**
52
     * Find all users.
53
     *
54
     * @param string $sortField
55
     * @param string $sortOrder
56
     *
57
     * @return array
58
     */
59
    public function findAll($sortField = 'createdAt', $sortOrder = 'DESC')
60
    {
61
        return $this->getQueryBuilder()
62
            ->orderBy(self::ENTITY_ALIAS . '.' . $sortField, $sortOrder)
63
            ->getQuery()
64
            ->getResult();
65
    }
66
67
    /**
68
     * Create an user.
69
     *
70
     * @param User $user
71
     */
72
    public function create(User $user)
73
    {
74
        $this->objectManager->persist($user);
75
        $this->objectManager->flush();
76
    }
77
78
    /**
79
     * Update an user.
80
     *
81
     * @param User $user
82
     */
83
    public function update(User $user)
84
    {
85
        $this->objectManager->persist($user);
86
        $this->objectManager->flush();
87
    }
88
89
    /**
90
     * Delete an user.
91
     *
92
     * @param User $user
93
     */
94
    public function delete(User $user)
95
    {
96
        $this->objectManager->remove($user);
97
        $this->objectManager->flush();
98
    }
99
100
    /**
101
     * Return a new query builder.
102
     *
103
     * @return QueryBuilder
104
     */
105
    public function getQueryBuilder()
106
    {
107
        return $this->objectManager
108
            ->getRepository(self::ENTITY_CLASS)
109
            ->createQueryBuilder(self::ENTITY_ALIAS);
110
    }
111
}
112