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\Project; |
15
|
|
|
use Doctrine\Common\Persistence\ObjectManager; |
16
|
|
|
use Doctrine\ORM\QueryBuilder; |
17
|
|
|
|
18
|
|
|
class DependencyRepository |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* Entity alias. |
22
|
|
|
* |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
const ENTITY_ALIAS = 'd'; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Entity to use. |
29
|
|
|
* |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
const ENTITY_CLASS = 'AppBundle:Dependency'; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Object 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 dependencies for a certain manager. |
53
|
|
|
* |
54
|
|
|
* @param Project $project |
55
|
|
|
* @param string $sortField |
56
|
|
|
* @param string $sortOrder |
57
|
|
|
* |
58
|
|
|
* @return array |
59
|
|
|
*/ |
60
|
|
View Code Duplication |
public function findAllByProject(Project $project, $sortField = 'pa.name', $sortOrder = 'ASC') |
|
|
|
|
61
|
|
|
{ |
62
|
|
|
return $this->getQueryBuilder() |
63
|
|
|
->leftJoin(self::ENTITY_ALIAS . '.project', 'p') |
64
|
|
|
->leftJoin(self::ENTITY_ALIAS . '.package', 'pa') |
65
|
|
|
->where('p = :project') |
66
|
|
|
->setParameter('project', $project) |
67
|
|
|
->orderBy($sortField, $sortOrder) |
68
|
|
|
->getQuery() |
69
|
|
|
->getResult(); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Find all dependencies for a certain project and manager. |
74
|
|
|
* |
75
|
|
|
* @param Project $project |
76
|
|
|
* @param string $manager |
77
|
|
|
* @param string $sortField |
78
|
|
|
* @param string $sortOrder |
79
|
|
|
* |
80
|
|
|
* @return array |
81
|
|
|
*/ |
82
|
|
View Code Duplication |
public function findAllByManager(Project $project, $manager, $sortField = 'pa.name', $sortOrder = 'ASC') |
|
|
|
|
83
|
|
|
{ |
84
|
|
|
return $this->getQueryBuilder() |
85
|
|
|
->leftJoin(self::ENTITY_ALIAS . '.project', 'p') |
86
|
|
|
->leftJoin(self::ENTITY_ALIAS . '.package', 'pa') |
87
|
|
|
->where('p = :project') |
88
|
|
|
->andWhere('pa.manager = :manager') |
89
|
|
|
->setParameter('project', $project) |
90
|
|
|
->setParameter('manager', $manager) |
91
|
|
|
->orderBy($sortField, $sortOrder) |
92
|
|
|
->getQuery() |
93
|
|
|
->getResult(); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* get QueryBuilder. |
98
|
|
|
* |
99
|
|
|
* @return QueryBuilder |
100
|
|
|
*/ |
101
|
|
|
public function getQueryBuilder() |
102
|
|
|
{ |
103
|
|
|
return $this->objectManager |
104
|
|
|
->getRepository(self::ENTITY_CLASS) |
105
|
|
|
->createQueryBuilder(self::ENTITY_ALIAS); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
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.