CampaignRepository::findCampaignsByProject()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 7
nc 1
nop 1
dl 0
loc 11
rs 10
c 0
b 0
f 0
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 App\Repository;
23
24
use App\Entity\Campaign;
25
use App\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    $crefid Refid of the campaign of the project
66
     *
67
     * @return Campaign|null
68
     */
69
    public function findCampaignByProjectRefidAndRefid(string $prefid, int $crefid): ?Campaign
70
    {
71
        $position = $crefid - 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 last campaign for a project refid.
87
     *
88
     * @param string $prefid The project refid
89
     *
90
     * @return Campaign|null
91
     */
92
    public function findLastCampaignByProjectRefid(string $prefid): ?Campaign
93
    {
94
        $qb = $this->createQueryBuilder('c')
95
            ->innerJoin('c.project', 'p')
96
            ->where('p.refid = :prefid')
97
            ->setParameter('prefid', $prefid)
98
            ->orderBy('c.position', 'DESC')
99
            ->setMaxResults(1);
100
101
        $result = $qb->getQuery()->getOneOrNullResult();
102
103
        return $result;
104
    }
105
106
    /**
107
     * Get previous campaign for a campaign.
108
     *
109
     * @param Project  $project  The project
110
     * @param Campaign $campaign The campaign
111
     *
112
     * @return Campaign|null
113
     */
114
    public function findPrevCampaignByProject(Project $project, Campaign $campaign): ?Campaign
115
    {
116
        $qb = $this->createQueryBuilder('c')
117
            ->innerJoin('c.project', 'p')
118
            ->where('p.id = :projectId')
119
            ->andWhere('c.position < :position')
120
            ->setParameter('projectId', $project->getId())
121
            ->setParameter('position', $campaign->getPosition())
122
            ->orderBy('c.position', 'DESC')
123
            ->setMaxResults(1);
124
125
        $result = $qb->getQuery()->getOneOrNullResult();
126
127
        return $result;
128
    }
129
130
    /**
131
     * Get next campaign for a campaign.
132
     *
133
     * @param Project  $project  The project
134
     * @param Campaign $campaign The campaign
135
     *
136
     * @return Campaign|null
137
     */
138
    public function findNextCampaignByProject(Project $project, Campaign $campaign): ?Campaign
139
    {
140
        $qb = $this->createQueryBuilder('c')
141
            ->innerJoin('c.project', 'p')
142
            ->where('p.id = :projectId')
143
            ->andWhere('c.position > :position')
144
            ->setParameter('projectId', $project->getId())
145
            ->setParameter('position', $campaign->getPosition())
146
            ->orderBy('c.position', 'ASC')
147
            ->setMaxResults(1);
148
149
        $result = $qb->getQuery()->getOneOrNullResult();
150
151
        return $result;
152
    }
153
154
    /**
155
     * Get last campaign for every projects.
156
     *
157
     * @return array
158
     */
159
    public function findLastRawCampaignForEveryProjects(): array
160
    {
161
        $sql = '
162
            SELECT c1.id AS cid, project_id AS pid, refid AS prefid, name, position+1 AS crefid,
163
            status, passed, passed + failed + errored + skipped AS available
164
            FROM cir_campaign c1, cir_project p
165
            WHERE position=(SELECT MAX(c2.position)
166
                FROM cir_campaign c2
167
                WHERE c1.project_id = c2.project_id
168
            )
169
            AND c1.project_id = p.id
170
            ORDER BY name ASC
171
        ';
172
        $conn = $this->getEntityManager()->getConnection();
173
        $stmt = $conn->prepare($sql);
174
        $stmt->execute();
175
176
        return $stmt->fetchAll();
177
    }
178
}
179