ViewDonors   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 116
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 37
dl 0
loc 116
rs 10
c 0
b 0
f 0
wmc 18

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getPagesTotal() 0 8 2
A countAllDonors() 0 3 1
A countQueriedDonors() 0 3 1
A getAllDonors() 0 3 1
A getFilterCriteria() 0 21 5
A __invoke() 0 13 4
A getCurrentPage() 0 3 1
A getQueriedDonors() 0 12 3
1
<?php
2
/**
3
 * View donors page controller class file
4
 *
5
 * @package    EBloodBank
6
 * @subpackage Controllers
7
 * @since      1.0
8
 */
9
namespace EBloodBank\Controllers;
10
11
use EBloodBank as EBB;
12
use EBloodBank\Options;
13
14
/**
15
 * View donors page controller class
16
 *
17
 * @since 1.0
18
 */
19
class ViewDonors extends Controller
20
{
21
    /**
22
     * @return void
23
     * @since 1.0
24
     */
25
    public function __invoke()
26
    {
27
        $isSitePublic = ('on' === EBB\Options::getOption('site_publication'));
28
        if (! $isSitePublic && (! $this->hasAuthenticatedUser() || ! $this->getAcl()->isUserAllowed($this->getAuthenticatedUser(), 'Donor', 'read'))) {
29
            $this->viewFactory->displayView('error-403');
30
        } else {
31
            $this->viewFactory->displayView('view-donors', [
32
                'donors'             => $this->getQueriedDonors(),
33
                'pagination.total'   => $this->getPagesTotal(),
34
                'pagination.current' => $this->getCurrentPage(),
35
                'filter.criteria'    => $this->getFilterCriteria(),
36
                'cityRepository'     => $this->getCityRepository(),
37
                'districtRepository' => $this->getDistrictRepository(),
38
            ]);
39
        }
40
    }
41
42
    /**
43
     * @return int
44
     * @since 1.0
45
     */
46
    public function getPagesTotal()
47
    {
48
        $total = 1;
49
        $limit = (int) Options::getOption('entities_per_page');
50
        if ($limit >= 1) {
51
            $total = (int) ceil($this->countAllDonors() / $limit);
52
        }
53
        return $total;
54
    }
55
56
    /**
57
     * @return int
58
     * @since 1.0
59
     */
60
    public function getCurrentPage()
61
    {
62
        return max((int) filter_input(INPUT_GET, 'page'), 1);
63
    }
64
65
    /**
66
     * @return array
67
     * @since 1.0
68
     */
69
    public function getFilterCriteria()
70
    {
71
        $criteria = [];
72
73
        if (filter_has_var(INPUT_POST, 'city_id')) {
74
            $criteria['city'] = filter_input(INPUT_POST, 'city_id');
75
        }
76
77
        if (filter_has_var(INPUT_POST, 'district_id')) {
78
            $criteria['district'] = filter_input(INPUT_POST, 'district_id');
79
        }
80
81
        if (filter_has_var(INPUT_POST, 'blood_group')) {
82
            $criteria['blood_group'] = filter_input(INPUT_POST, 'blood_group');
83
        }
84
85
        if (filter_has_var(INPUT_POST, 'blood_group_alternatives')) {
86
            $criteria['blood_group_alternatives'] = (filter_input(INPUT_POST, 'blood_group_alternatives') === 'on');
87
        }
88
89
        return $criteria;
90
    }
91
92
    /**
93
     * @return \EBloodBank\Models\Donor[]
94
     * @since 1.0
95
     */
96
    public function getAllDonors()
97
    {
98
        return $this->getDonorRepository()->findAll([], ['created_at' => 'DESC']);
0 ignored issues
show
Unused Code introduced by
The call to Doctrine\ORM\EntityRepository::findAll() has too many arguments starting with array(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

98
        return $this->getDonorRepository()->/** @scrutinizer ignore-call */ findAll([], ['created_at' => 'DESC']);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
99
    }
100
101
    /**
102
     * @return int
103
     * @since 1.0
104
     */
105
    public function countAllDonors()
106
    {
107
        return $this->getDonorRepository()->countAll();
108
    }
109
110
    /**
111
     * @return \EBloodBank\Models\Donor[]
112
     * @since 1.0
113
     */
114
    public function getQueriedDonors()
115
    {
116
        $criteria = $this->getFilterCriteria();
117
118
        $limit = (int) Options::getOption('entities_per_page');
119
        $offset = ($this->getCurrentPage() - 1) * $limit;
120
121
        if (! $this->hasAuthenticatedUser() || ! $this->getAcl()->isUserAllowed($this->getAuthenticatedUser(), 'Donor', 'approve')) {
122
            $criteria['status'] = 'approved';
123
        }
124
125
        return $this->getDonorRepository()->findBy($criteria, ['created_at' => 'DESC'], $limit, $offset);
126
    }
127
128
    /**
129
     * @return int
130
     * @since 1.0
131
     */
132
    public function countQueriedDonors()
133
    {
134
        return count($this->getQueriedDonors());
135
    }
136
}
137