User::shouldBeAlerted()   C
last analyzed

Complexity

Conditions 11
Paths 8

Size

Total Lines 38
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 11

Importance

Changes 0
Metric Value
dl 0
loc 38
ccs 21
cts 21
cp 1
rs 5.2653
c 0
b 0
f 0
cc 11
eloc 20
nc 8
nop 1
crap 11

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
namespace Overwatch\UserBundle\Entity;
4
5
use Doctrine\ORM\Mapping as ORM;
6
use FOS\UserBundle\Entity\User as BaseUser;
7
use Overwatch\ResultBundle\Entity\TestResult;
8
use Overwatch\UserBundle\Enum\AlertSetting;
9
10
/**
11
 * User
12
 * 
13
 * @ORM\Entity
14
 * @ORM\Table(name="User")
15
 * @ORM\HasLifecycleCallbacks
16
 */
17
class User extends BaseUser implements \JsonSerializable
18
{
19
    /**
20
     * @ORM\Id
21
     * @ORM\Column(type="integer")
22
     * @ORM\GeneratedValue(strategy="AUTO")
23
     */
24
    protected $id;
25
    
26
    /**
27
     * @ORM\ManyToMany(targetEntity="Overwatch\TestBundle\Entity\TestGroup", inversedBy="users")
28
     * @ORM\JoinTable(name="UsersTestGroups")
29
     */
30
    protected $groups;
31
    
32
    /**
33
     * @ORM\Column(type="string", length=40)
34
     */
35
    protected $apiKey = null;
36
    
37
    /**
38
     * @ORM\Column(type="integer")
39
     */
40
    protected $alertSetting = AlertSetting::CHANGE;
41
    
42
    /**
43
     * @ORM\Column(type="string", length=35, nullable=true)
44
     */
45
    protected $telephoneNumber = null;
46
    
47
    /**
48
     * Constructor
49
     */
50 152
    public function __construct()
51
    {
52 152
        parent::__construct();
53 152
        $this->groups = new \Doctrine\Common\Collections\ArrayCollection();
54 152
    }
55
56
    /**
57
     * Serialize object to JSON
58
     */
59 13
    public function jsonSerialize()
60
    {
61
        return [
62 13
            'id'              => $this->getId(),
63 13
            'email'           => $this->getEmail(),
64 13
            'alertSetting'    => $this->getAlertSetting(),
65 13
            'telephoneNumber' => $this->getTelephoneNumber(),
66 13
            'lastLogin'       => $this->getLastLogin() ? $this->getLastLogin()->getTimestamp() : '',
67 13
            'locked'          => $this->isLocked(),
68 13
            'roles'           => $this->getRoles()
69 13
        ];
70
    }
71
    
72
    /**
73
     * Set Email (and username)
74
     * 
75
     * @param string $email
76
     * @return User
77
     */
78 152
    public function setEmail($email)
79
    {
80 152
        parent::setEmail($email);
81 152
        $this->setUsername($email);
82
        
83 152
        return $this;
84
    }
85
    
86
    /**
87
     * Get Alert Setting for this user
88
     * 
89
     * @return integer $alertSetting
90
     */
91 29
    public function getAlertSetting()
92
    {
93 29
        return $this->alertSetting;
94
    }
95
    
96
    /**
97
     * Set this user's alert setting
98
     * 
99
     * @param integer $setting
100
     * @return User
101
     */
102 136
    public function setAlertSetting($setting)
103
    {
104 136
        AlertSetting::isValid($setting);
105 135
        $this->alertSetting = $setting;
106
        
107 135
        return $this;
108
    }
109
    
110 129
    public function getApiKey()
111
    {
112 129
        return $this->apiKey;
113
    }
114
    
115 143
    public function resetApiKey()
116
    {
117 143
        $this->apiKey = sha1(random_bytes(10));
118
        
119 143
        return $this;
120
    }
121
    
122
    /**
123
     * @ORM\PrePersist()
124
     * @ORM\PreUpdate()
125
     */
126 127
    public function generateApiKey()
127
    {
128 127
        if (empty($this->getApiKey())) {
129 127
            $this->resetApiKey();
130 127
        }
131
        
132 127
        return $this;
133
    }
134
    
135
    /**
136
     * Get this user's telephone number
137
     * 
138
     * @return string
139
     */
140 15
    public function getTelephoneNumber()
141
    {
142 15
        return $this->telephoneNumber;
143
    }
144
145
    /**
146
     * Set user's telephone number
147
     * 
148
     * @param string $telephoneNumber
149
     * @return User
150
     */
151 136
    public function setTelephoneNumber($telephoneNumber)
152
    {
153 136
        $this->telephoneNumber = $telephoneNumber;
154
        
155 136
        return $this;
156
    }
157
    
158
    /**
159
     * Should this user be alerted for the passed TestResult?
160
     * 
161
     * @param TestResult $result
162
     * @return bool
163
     */
164 16
    public function shouldBeAlerted(TestResult $result)
165
    {
166 16
        $setting = $this->getAlertSetting();
167
        
168 16
        if ($this->isLocked() || $setting === AlertSetting::NONE) {
169 11
            return false;
170
        }
171
        
172 15
        if (!$this->hasGroup($result->getTest()->getGroup()->getName())) {
173 1
            return false;
174
        }
175
        
176 14
        if ($setting === AlertSetting::ALL) {
177 10
            return true;
178
        }
179
        
180
        if (
181 4
            $result->isUnsuccessful()
182 4
            && ($setting === AlertSetting::CHANGE_ALL)
183 4
        ) {
184 2
            return true;
185
        }
186
        
187 4
        if ($result->isAChange()) {
188 2
            if (in_array($setting, [AlertSetting::CHANGE, AlertSetting::CHANGE_ALL])) {
189 2
                return true;
190
            }
191
            
192
            if (
193 2
                $result->isUnsuccessful()
194 2
                && $setting === AlertSetting::CHANGE_BAD
195 2
            ) {
196 1
                return true;
197
            }
198 1
        }
199
        
200 3
        return false;
201
    }
202
}
203