EloquentFacultiesRepository::countPerFaculty()   B
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 35
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 35
c 0
b 0
f 0
rs 8.8571
cc 3
eloc 13
nc 4
nop 0
1
<?php
2
3
/**
4
 * Storgman - Student Organizations Management
5
 * Copyright (C) 2014-2016, Dejan Angelov <[email protected]>
6
 *
7
 * This file is part of Storgman.
8
 *
9
 * Storgman is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU General Public License as published by
11
 * the Free Software Foundation, either version 3 of the License, or
12
 * (at your option) any later version.
13
 *
14
 * Storgman is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU General Public License
20
 * along with Storgman.  If not, see <http://www.gnu.org/licenses/>.
21
 *
22
 * @package Storgman
23
 * @copyright Copyright (C) 2014-2016, Dejan Angelov <[email protected]>
24
 * @license https://github.com/angelov/storgman/blob/master/LICENSE
25
 * @author Dejan Angelov <[email protected]>
26
 */
27
28
namespace Angelov\Storgman\Faculties\Repositories;
29
30
use Angelov\Storgman\Core\Repositories\AbstractEloquentRepository;
31
use Angelov\Storgman\Faculties\Faculty;
32
use Angelov\Storgman\Faculties\Reports\MembersPerFacultyReport;
33
use Illuminate\Support\Facades\DB;
34
35
class EloquentFacultiesRepository extends AbstractEloquentRepository implements FacultiesRepositoryInterface
36
{
37
    public function __construct(Faculty $entity)
38
    {
39
        parent::__construct($entity);
40
    }
41
42
    public function store(Faculty $faculty)
43
    {
44
        $faculty->save();
45
    }
46
47
    public function getEnabled()
48
    {
49
        return Faculty::where('enabled', true)->get()->all();
50
    }
51
52
    public function countPerFaculty()
53
    {
54
        // The query works with both MySQL and PostgreSQL
55
        $results = (array)DB::select(
56
            '
57
                SELECT faculty_id,
58
                       count(id) AS members
59
                FROM members
60
                WHERE faculty_id IS NOT NULL
61
                AND members.approved = TRUE
62
                GROUP BY faculty_id
63
                ORDER BY members DESC;
64
            '
65
        );
66
67
        $resultsById = [];
68
69
        foreach ($results as $result) {
70
            $resultsById[$result->faculty_id] = (int) $result->members;
71
        }
72
73
        $ids = array_keys($resultsById);
74
75
        /** @var Faculty[] $faculties */
76
        $faculties = $this->getByIds($ids);
77
78
        $report = new MembersPerFacultyReport();
79
80
        foreach ($faculties as $faculty) {
81
            $count = $resultsById[$faculty->getId()];
82
            $report->addFaculty($faculty, $count);
83
        }
84
85
        return $report;
86
    }
87
}