Completed
Push — master ( fb3a67...8803de )
by Andrii
02:10
created

Service   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 1
Bugs 1 Features 0
Metric Value
wmc 10
c 1
b 1
f 0
lcom 1
cbo 1
dl 0
loc 53
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setStorage() 0 4 1
A getStorage() 0 12 4
B mailToken() 0 28 5
1
<?php
2
3
/*
4
 * Library for confirmation tokens
5
 *
6
 * @link      https://github.com/hiqdev/php-confirmator
7
 * @package   php-confirmator
8
 * @license   BSD-3-Clause
9
 * @copyright Copyright (c) 2016, HiQDev (http://hiqdev.com/)
10
 */
11
12
namespace hiqdev\yii2\confirmator;
13
14
use hiqdev\php\confirmator\ServiceTrait;
15
use Yii;
16
use yii\helpers\Inflector;
17
18
class Service extends \yii\base\Component
19
{
20
    use ServiceTrait;
21
22
    protected $_storage;
23
24
    public function setStorage($value)
25
    {
26
        $this->_storage = $value;
27
    }
28
29
    public function getStorage()
30
    {
31
        if (is_array($this->_storage)) {
32
            $config = $this->_storage;
33
            if (isset($config['path']) && is_string($config['path'])) {
34
                $config['path'] = Yii::getAlias($config['path']);
35
            }
36
            $this->_storage = Yii::createObject($config);
37
        }
38
39
        return $this->_storage;
40
    }
41
42
    public function mailToken($user, $action, array $data = [])
43
    {
44
        if (!$user) {
45
            return false;
46
        }
47
48
        if (Yii::$app->has('authManager')) {
49
            $auth = Yii::$app->authManager;
50
            if ($auth->getItem($action) && !$auth->checkAccess($user->id, $action)) {
51
                return false;
52
            }
53
        }
54
55
        $token = $this->issueToken(array_merge([
56
            'action'    => $action,
57
            'email'     => $user->email,
58
            'username'  => $user->username,
59
            'notAfter'  => '+ 3 days',
60
        ], $data));
61
62
        $view = lcfirst(Inflector::id2camel($action . '-token'));
63
64
        return Yii::$app->mailer->compose()
65
            ->renderHtmlBody($view, compact('user', 'token'))
66
            ->setTo([$user->email => $user->name])
67
            ->send()
68
        ;
69
    }
70
}
71