Passed
Push — master ( fd4fa7...218eec )
by Jan
03:02
created

UserController   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 19
dl 0
loc 54
rs 10
c 0
b 0
f 0
wmc 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A userInfo() 0 18 3
A getGravatar() 0 13 3
1
<?php
2
/**
3
 *
4
 * part-db version 0.1
5
 * Copyright (C) 2005 Christoph Lechner
6
 * http://www.cl-projects.de/
7
 *
8
 * part-db version 0.2+
9
 * Copyright (C) 2009 K. Jacobs and others (see authors.php)
10
 * http://code.google.com/p/part-db/
11
 *
12
 * Part-DB Version 0.4+
13
 * Copyright (C) 2016 - 2019 Jan Böhmer
14
 * https://github.com/jbtronics
15
 *
16
 * This program is free software; you can redistribute it and/or
17
 * modify it under the terms of the GNU General Public License
18
 * as published by the Free Software Foundation; either version 2
19
 * of the License, or (at your option) any later version.
20
 *
21
 * This program is distributed in the hope that it will be useful,
22
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24
 * GNU General Public License for more details.
25
 *
26
 * You should have received a copy of the GNU General Public License
27
 * along with this program; if not, write to the Free Software
28
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
29
 *
30
 */
31
32
namespace App\Controller;
33
34
35
use App\Entity\User;
36
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
37
use Symfony\Component\Asset\Packages;
38
use Symfony\Component\Routing\Annotation\Route;
39
40
class UserController extends AbstractController
41
{
42
43
    /**
44
     * @Route("/user/info", name="user_info_self")
45
     * @Route("/user/{id}/info")
46
     */
47
    public function userInfo(?User $user, Packages $packages)
48
    {
49
50
        //If no user id was passed, then we show info about the current user
51
        if($user == null) {
52
            $user = $this->getUser();
53
        }
54
55
        if($this->getParameter("use_gravatar")) {
56
            $avatar = $this->getGravatar($user->getEmail(), 200, 'identicon');
57
        } else {
58
            $avatar = $packages->getUrl("/img/default_avatar.png");
59
        }
60
61
62
        return $this->render('Users/user_info.html.twig', [
63
                'user' => $user,
64
                'avatar' => $avatar
65
            ]);
66
    }
67
68
69
    /**
70
     * Get either a Gravatar URL or complete image tag for a specified email address.
71
     *
72
     * @param string $email The email address
73
     * @param string $s Size in pixels, defaults to 80px [ 1 - 2048 ]
74
     * @param string $d Default imageset to use [ 404 | mm | identicon | monsterid | wavatar ]
75
     * @param string $r Maximum rating (inclusive) [ g | pg | r | x ]
76
     * @param bool $img True to return a complete IMG tag False for just the URL
77
     * @param array $atts Optional, additional key/value attributes to include in the IMG tag
78
     * @return String containing either just a URL or a complete image tag
79
     * @source https://gravatar.com/site/implement/images/php/
80
     */
81
    public function getGravatar(string $email, int $s = 80, string $d = 'mm', string $r = 'g', bool $img = false, array $atts = array())
82
    {
83
        $url = 'https://www.gravatar.com/avatar/';
84
        $url .= md5(strtolower(trim($email)));
85
        $url .= "?s=$s&d=$d&r=$r";
86
        if ($img) {
87
            $url = '<img src="' . $url . '"';
88
            foreach ($atts as $key => $val) {
89
                $url .= ' ' . $key . '="' . $val . '"';
90
            }
91
            $url .= ' />';
92
        }
93
        return $url;
94
    }
95
96
}