Completed
Push — master ( 335a60...4a8059 )
by Antonio
02:24
created

UserBlockService::triggerEvents()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 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