UserBlockService   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 3
dl 0
loc 42
ccs 0
cts 31
cp 0
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 1
A run() 0 16 2
A triggerEvents() 0 5 1
1
<?php
2
3
/*
4
 * This file is part of the 2amigos/yii2-usuario project.
5
 *
6
 * (c) 2amigOS! <http://2amigos.us/>
7
 *
8
 * For the full copyright and license information, please view
9
 * the LICENSE file that was distributed with this source code.
10
 */
11
12
namespace Da\User\Service;
13
14
use Da\User\Contracts\ServiceInterface;
15
use Da\User\Controller\AdminController;
16
use Da\User\Event\UserEvent;
17
use Da\User\Helper\SecurityHelper;
18
use Da\User\Model\User;
19
20
class UserBlockService implements ServiceInterface
21
{
22
    protected $model;
23
    protected $event;
24
    protected $controller;
25
    protected $securityHelper;
26
27
    public function __construct(
28
        User $model,
29
        UserEvent $event,
30
        AdminController $controller,
31
        SecurityHelper $securityHelper
32
    ) {
33
        $this->model = $model;
34
        $this->event = $event;
35
        $this->controller = $controller;
36
        $this->securityHelper = $securityHelper;
37
    }
38
39
    public function run()
40
    {
41
        if ($this->model->getIsBlocked()) {
42
            $this->triggerEvents(UserEvent::EVENT_BEFORE_UNBLOCK);
43
            $result = (bool)$this->model->updateAttributes(['blocked_at' => null]);
44
            $this->triggerEvents(UserEvent::EVENT_AFTER_UNBLOCK);
45
        } else {
46
            $this->triggerEvents(UserEvent::EVENT_BEFORE_BLOCK);
47
            $result = (bool)$this->model->updateAttributes(
48
                ['blocked_at' => time(), 'auth_key' => $this->securityHelper->generateRandomString()]
49
            );
50
            $this->triggerEvents(UserEvent::EVENT_AFTER_BLOCK);
51
        }
52
53
        return $result;
54
    }
55
56
    protected function triggerEvents($name)
57
    {
58
        $this->controller->trigger($name, $this->event);
59
        $this->model->trigger($name, $this->event);
60
    }
61
}
62