Passed
Push — master ( e28eb3...62fe4a )
by Jan
03:05
created

UserController::getGravatar()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 9
nc 2
nop 6
dl 0
loc 13
rs 9.9666
c 0
b 0
f 0
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 App\Form\UserSettingsType;
37
use Doctrine\ORM\EntityManagerInterface;
38
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
39
use Symfony\Component\Asset\Packages;
40
use Symfony\Component\HttpFoundation\Request;
41
use Symfony\Component\Routing\Annotation\Route;
42
43
class UserController extends AbstractController
44
{
45
46
    /**
47
     * @Route("/user/info", name="user_info_self")
48
     * @Route("/user/{id}/info")
49
     */
50
    public function userInfo(?User $user, Packages $packages)
51
    {
52
53
        //If no user id was passed, then we show info about the current user
54
        if($user == null) {
55
            $user = $this->getUser();
56
        }
57
58
        if($this->getParameter("use_gravatar")) {
59
            $avatar = $this->getGravatar($user->getEmail(), 200, 'identicon');
0 ignored issues
show
Bug introduced by
It seems like $user->getEmail() can also be of type null; however, parameter $email of App\Controller\UserController::getGravatar() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

59
            $avatar = $this->getGravatar(/** @scrutinizer ignore-type */ $user->getEmail(), 200, 'identicon');
Loading history...
60
        } else {
61
            $avatar = $packages->getUrl("/img/default_avatar.png");
62
        }
63
64
65
        return $this->render('Users/user_info.html.twig', [
66
                'user' => $user,
67
                'avatar' => $avatar
68
            ]);
69
    }
70
71
    /**
72
     * @Route("/user/settings", name="user_settings")
73
     */
74
    public function userSettings(Request $request, EntityManagerInterface $em)
75
    {
76
        $user = $this->getUser();
77
78
        //When user change its settings, he should be logged  in fully.
79
        $this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');
80
81
82
        $form = $this->createForm(UserSettingsType::class, $user);
83
84
        $form->handleRequest($request);
85
86
        if ($form->isSubmitted() && $form->isValid()) {
87
            $em->persist($user);
88
            $em->flush();
89
            $this->addFlash('success', 'user.settings.saved_flash');
90
        }
91
92
        return $this->render('Users/user_settings.html.twig', [
93
            "settings_form" => $form->createView()
94
        ]);
95
    }
96
97
98
    /**
99
     * Get either a Gravatar URL or complete image tag for a specified email address.
100
     *
101
     * @param string $email The email address
102
     * @param string $s Size in pixels, defaults to 80px [ 1 - 2048 ]
103
     * @param string $d Default imageset to use [ 404 | mm | identicon | monsterid | wavatar ]
104
     * @param string $r Maximum rating (inclusive) [ g | pg | r | x ]
105
     * @param bool $img True to return a complete IMG tag False for just the URL
106
     * @param array $atts Optional, additional key/value attributes to include in the IMG tag
107
     * @return String containing either just a URL or a complete image tag
108
     * @source https://gravatar.com/site/implement/images/php/
109
     */
110
    public function getGravatar(string $email, int $s = 80, string $d = 'mm', string $r = 'g', bool $img = false, array $atts = array())
111
    {
112
        $url = 'https://www.gravatar.com/avatar/';
113
        $url .= md5(strtolower(trim($email)));
114
        $url .= "?s=$s&d=$d&r=$r";
115
        if ($img) {
116
            $url = '<img src="' . $url . '"';
117
            foreach ($atts as $key => $val) {
118
                $url .= ' ' . $key . '="' . $val . '"';
119
            }
120
            $url .= ' />';
121
        }
122
        return $url;
123
    }
124
125
}