Completed
Push — master ( c94855...6d1490 )
by Alexey
04:43
created

AvatarWidget::calculateSomething()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

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