Completed
Push — master ( 2b33dd...92e09f )
by FX
03:29
created

findCampaignByProjectAndRefId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 9
c 0
b 0
f 0
nc 1
nop 2
dl 0
loc 14
rs 9.4285
1
<?php
2
/**
3
 * Copyright (c) 2017 Francois-Xavier Soubirou.
4
 *
5
 * This file is part of ci-report.
6
 *
7
 * ci-report is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU General Public License as published by
9
 * the Free Software Foundation, either version 3 of the License, or
10
 * (at your option) any later version.
11
 *
12
 * ci-report is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
 * GNU General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU General Public License
18
 * along with ci-report. If not, see <http://www.gnu.org/licenses/>.
19
 */
20
declare(strict_types=1);
21
22
namespace AppBundle\Repository;
23
24
use AppBundle\Entity\Campaign;
25
use AppBundle\Entity\Project;
26
use Gedmo\Sortable\Entity\Repository\SortableRepository;
27
28
/**
29
 * Campaign repository class.
30
 *
31
 * @category  ci-report app
32
 *
33
 * @author    Francois-Xavier Soubirou <[email protected]>
34
 * @copyright 2017 Francois-Xavier Soubirou
35
 * @license   http://www.gnu.org/licenses/   GPLv3
36
 *
37
 * @see      https://www.ci-report.io
38
 */
39
class CampaignRepository extends SortableRepository
40
{
41
    /**
42
     * Get all campaigns for a project.
43
     *
44
     * @param Project $project The project.
45
     *
46
     * @return array List of campaigns
47
     */
48
    public function findCampaignsByProject(Project $project): array
49
    {
50
        $qb = $this->createQueryBuilder('c')
51
            ->innerJoin('c.project', 'p')
52
            ->where('p.id = :projectId')
53
            ->setParameter('projectId', $project->getId())
54
            ->orderBy('c.position', 'DESC');
55
56
        $result = $qb->getQuery()->getResult();
57
58
        return $result;
59
    }
60
61
    /**
62
     * Get a campaign for a project refid and its refid.
63
     *
64
     * @param string $prefid The project refid
65
     * @param int    $refid  Refid of the campaign of the project
66
     *
67
     * @return Campaign|null
68
     */
69
    public function findCampaignByProjectRefidAndRefid(string $prefid, int $refid): ?Campaign
70
    {
71
        $position = $refid - 1;
72
73
        $qb = $this->createQueryBuilder('c')
74
            ->innerJoin('c.project', 'p')
75
            ->where('p.refid = :prefid')
76
            ->andWhere('c.position = :position')
77
            ->setParameter('prefid', $prefid)
78
            ->setParameter('position', $position);
79
80
        $result = $qb->getQuery()->getOneOrNullResult();
81
82
        return $result;
83
    }
84
85
    /**
86
     * Get previous campaign for a campaign.
87
     *
88
     * @param Project  $project  The project
89
     * @param Campaign $campaign The campaign
90
     *
91
     * @return Campaign|null
92
     */
93
    public function findPrevCampaignByProject(Project $project, Campaign $campaign): ?Campaign
94
    {
95
        $qb = $this->createQueryBuilder('c')
96
            ->innerJoin('c.project', 'p')
97
            ->where('p.id = :projectId')
98
            ->andWhere('c.position < :position')
99
            ->setParameter('projectId', $project->getId())
100
            ->setParameter('position', $campaign->getPosition())
101
            ->orderBy('c.position', 'DESC')
102
            ->setMaxResults(1);
103
104
        $result = $qb->getQuery()->getOneOrNullResult();
105
106
        return $result;
107
    }
108
109
    /**
110
     * Get next campaign for a campaign.
111
     *
112
     * @param Project  $project  The project
113
     * @param Campaign $campaign The campaign
114
     *
115
     * @return Campaign|null
116
     */
117
    public function findNextCampaignByProject(Project $project, Campaign $campaign): ?Campaign
118
    {
119
        $qb = $this->createQueryBuilder('c')
120
            ->innerJoin('c.project', 'p')
121
            ->where('p.id = :projectId')
122
            ->andWhere('c.position > :position')
123
            ->setParameter('projectId', $project->getId())
124
            ->setParameter('position', $campaign->getPosition())
125
            ->orderBy('c.position', 'ASC')
126
            ->setMaxResults(1);
127
128
        $result = $qb->getQuery()->getOneOrNullResult();
129
130
        return $result;
131
    }
132
}
133