Completed
Push — master ( 51f787...8bc09a )
by Alexey
03:06
created

AvatarWidget   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 8
eloc 25
dl 0
loc 73
c 0
b 0
f 0
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A run() 0 3 1
A calculateSomething() 0 10 1
A init() 0 4 2
A getGravatarEmail() 0 5 2
A getGravatar() 0 10 2
1
<?php
2
3
namespace modules\users\widgets;
4
5
use Yii;
6
use yii\base\Widget;
7
use yii\helpers\Html;
8
use modules\users\models\User;
9
10
/**
11
 * Class AvatarWidget
12
 * @package modules\users\widgets
13
 *
14
 * @property array $imageOptions
15
 * @property string $email
16
 * @property string|int $size
17
 */
18
class AvatarWidget extends Widget
19
{
20
    public $imageOptions = [
21
        'class' => 'img-circle',
22
    ];
23
    public $email = '';
24
    public $size = '80';
25
26
    /**
27
     * @inheritdoc
28
     */
29
    public function init()
30
    {
31
        parent::init();
32
        $this->email = !empty($this->email) ? $this->email : $this->getGravatarEmail();
33
    }
34
35
    /**
36
     * @inheritdoc
37
     */
38
    public function run()
39
    {
40
        echo $this->getGravatar($this->email, $this->size, 'mm', 'g', true, $this->imageOptions);
41
    }
42
43
    /**
44
     * Get either a Gravatar URL or complete image tag for a specified email address.
45
     *
46
     * @param string $email The email address
47
     * @param string $s Size in pixels, defaults to 80px [ 1 - 2048 ]
48
     * @param string $d Default imageset to use [ 404 | mm | identicon | monsterid | wavatar ]
49
     * @param string $r Maximum rating (inclusive) [ g | pg | r | x ]
50
     * @param bool $img True to return a complete IMG tag False for just the URL
51
     * @param array $attr Optional, additional key/value attributes to include in the IMG tag
52
     * @return string containing either just a URL or a complete image tag
53
     * @source https://gravatar.com/site/implement/images/php/
54
     */
55
    public function getGravatar($email = '', $s = '80', $d = 'mm', $r = 'g', $img = false, $attr = [])
56
    {
57
        $data = ['email' => $email, 's' => $s, 'd' => $d, 'r' => $r];
58
        $key = 'gravatar_' . md5($email);
59
        $duration = 60 * 60; // 3600 сек или 1 час
60
        $cache = Yii::$app->cache;
61
        $url = $cache->getOrSet($key, function () use ($data) {
62
            return $this->calculateSomething($data);
63
        }, $duration);
64
        return $img ? Html::img($url, $attr) : $url;
65
    }
66
67
    /**
68
     * @param array $data
69
     * @return string
70
     */
71
    protected function calculateSomething($data = [])
72
    {
73
        $url = 'https://www.gravatar.com/avatar/';
74
        $url .= md5(strtolower(trim($data['email']))) . '?';
75
        $url .= http_build_query([
76
            's' => $data['s'],
77
            'd' => $data['d'],
78
            'r' => $data['r'],
79
        ]);
80
        return $url;
81
    }
82
83
    /**
84
     * @return string
85
     */
86
    public function getGravatarEmail()
87
    {
88
        /** @var User $user */
89
        $user = Yii::$app->user->identity;
90
        return (!Yii::$app->user->isGuest) ? $user->profile->email_gravatar : $this->email;
91
    }
92
}
93