Passed
Push — master ( c04040...869fe9 )
by Nashwan
02:39
created

DonorRepository::findCompatibleRedBloodCellGroup()   B

Complexity

Conditions 10
Paths 10

Size

Total Lines 35
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 10
eloc 30
c 1
b 0
f 0
nc 10
nop 1
dl 0
loc 35
rs 7.6666

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Donor entity repository class file
4
 *
5
 * @package    EBloodBank
6
 * @subpackage Models
7
 * @since      1.0
8
 */
9
namespace EBloodBank\Models;
10
11
use EBloodBank as EBB;
12
13
/**
14
 * Donor entity repository class
15
 *
16
 * @since 1.0
17
 */
18
class DonorRepository extends EntityRepository
19
{
20
    /**
21
     * Finds entities by a set of criteria.
22
     *
23
     * @param array      $criteria
24
     * @param array|null $orderBy
25
     * @param int|null   $limit
26
     * @param int|null   $offset
27
     *
28
     * @return array The objects.
29
     */
30
    public function findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
31
    {
32
        $criteria = $this->parseCriteria($criteria);
33
        return parent::findBy($criteria, $orderBy, $limit, $offset);
34
    }
35
36
    /**
37
     * Finds a single entity by a set of criteria.
38
     *
39
     * @param array $criteria
40
     * @param array|null $orderBy
41
     *
42
     * @return object|null The entity instance or NULL if the entity can not be found.
43
     */
44
    public function findOneBy(array $criteria, array $orderBy = null)
45
    {
46
        $criteria = $this->parseCriteria($criteria);
47
        return parent::findOneBy($criteria, $orderBy);
48
    }
49
50
    /**
51
     * @return array
52
     * @since 1.0
53
     */
54
    protected function parseCriteria(array $criteria)
55
    {
56
        if (isset($criteria['blood_group_alternatives']) && $criteria['blood_group_alternatives']) {
57
            if (isset($criteria['blood_group']) && is_string($criteria['blood_group'])) {
58
                $criteria['blood_group'] = $this->findCompatibleRedBloodCellGroup($criteria['blood_group']);
59
            }
60
        }
61
62
        unset($criteria['blood_group_alternatives']); // Remove the alternative blood-groups criteria.
63
64
        if (isset($criteria['blood_group']) && 'any' === $criteria['blood_group']) {
65
            unset($criteria['blood_group']); // Remove the blood-group criteria.
66
        }
67
68
        if (isset($criteria['district']) && -1 == $criteria['district']) {
69
            unset($criteria['district']); // Remove the district criteria.
70
        }
71
72
        if (isset($criteria['city']) && empty($criteria['district'])) {
73
            $districts = [];
74
75
            if ($criteria['city'] instanceof City) {
76
                $districts = $criteria['city']->get('districts');
77
            } elseif (EBB\isValidID($criteria['city'])) {
0 ignored issues
show
Bug introduced by
The function isValidID was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

77
            } elseif (/** @scrutinizer ignore-call */ EBB\isValidID($criteria['city'])) {
Loading history...
78
                $em = $this->getEntityManager();
79
                $city = $em->find('Entities:City', $criteria['city']);
80
                if (! empty($city)) {
81
                    $districts = $city->get('districts');
82
                }
83
            }
84
85
            if (! empty($districts)) {
86
                $criteria['district'] = [];
87
88
                foreach ($districts as $district) {
89
                    $criteria['district'][] = (int) $district->get('id');
90
                }
91
            }
92
        }
93
94
        unset($criteria['city']); // Remove the city criteria in any condition.
95
96
        if (isset($criteria['status']) && 'any' === $criteria['status']) {
97
            unset($criteria['status']); // Remove the status criteria.
98
        }
99
100
        return $criteria;
101
    }
102
103
    /**
104
     * @return array
105
     * @since  1.6
106
     */
107
    protected function findCompatibleRedBloodCellGroup(string $bloodGroup)
108
    {
109
        $compatibleGroups = [];
110
111
        switch ($bloodGroup) {
112
            case 'A+':
113
                $compatibleGroups = ['A+', 'A-', 'O+', 'O-'];
114
                break;
115
            case 'A-':
116
                $compatibleGroups = ['A-', 'O-'];
117
                break;
118
            case 'B+':
119
                $compatibleGroups = ['B+', 'B-', 'O+', 'O-'];
120
                break;
121
            case 'B+':
122
                $compatibleGroups = ['B+', 'B-', 'O+', 'O-'];
123
                break;
124
            case 'B-':
125
                $compatibleGroups = ['B-', 'O-'];
126
                break;
127
            case 'O+':
128
                $compatibleGroups = ['O+', 'O-'];
129
                break;
130
            case 'O-':
131
                $compatibleGroups = ['O-'];
132
                break;
133
            case 'AB+':
134
                $compatibleGroups = ['any'];
135
                break;
136
            case 'AB-':
137
                $compatibleGroups = ['A-', 'B-', 'O-', 'AB-'];
138
                break;
139
        }
140
141
        return $compatibleGroups;
142
    }
143
}
144