ProfileRuntime::determineProfileLink()   C
last analyzed

Complexity

Conditions 12
Paths 99

Size

Total Lines 48
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 26
dl 0
loc 48
rs 6.9666
c 0
b 0
f 0
cc 12
nc 99
nop 6

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Zikula package.
7
 *
8
 * Copyright Zikula - https://ziku.la/
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Zikula\UsersBundle\Twig\Runtime;
15
16
use InvalidArgumentException;
17
use Symfony\Contracts\Translation\TranslatorInterface;
18
use Twig\Extension\RuntimeExtensionInterface;
19
use Zikula\UsersBundle\Entity\User;
20
use Zikula\UsersBundle\Helper\ProfileHelper;
21
use Zikula\UsersBundle\Repository\UserRepositoryInterface;
22
use function Symfony\Component\String\s;
23
24
class ProfileRuntime implements RuntimeExtensionInterface
25
{
26
    public function __construct(
27
        private readonly ProfileHelper $profileHelper,
28
        private readonly UserRepositoryInterface $userRepository,
29
        private readonly TranslatorInterface $translator,
30
        private readonly bool $displayRegistrationDate = false
31
    ) {
32
    }
33
34
    /**
35
     * Whether to show the users registration date or not.
36
     */
37
    public function displayRegistrationDate(): bool
38
    {
39
        return $this->displayRegistrationDate;
40
    }
41
42
    /**
43
     * Displays the avatar of a given user.
44
     *
45
     * @param int|string $userId The user's id or name
46
     */
47
    public function getUserAvatar($userId = 0, array $parameters = []): string
48
    {
49
        return $this->profileHelper->getAvatar($userId, $parameters);
50
    }
51
52
    /**
53
     * Create a link to a users profile from the UID.
54
     *
55
     * Examples
56
     *
57
     *   Simple version, shows the username
58
     *   {{ uid|profileLinkByUserId() }}
59
     *   Simple version, shows username, using class="classname"
60
     *   {{ uid|profileLinkByUserId(class='classname') }}
61
     *   Using profile.gif instead of username, no class
62
     *   {{ uid|profileLinkByUserId(image='images/profile.gif') }}
63
     */
64
    public function profileLinkByUserId(
65
        int $userId,
66
        string $class = '',
67
        string $image = '',
68
        int $maxLength = 0,
69
        string $title = ''
70
    ): string {
71
        if (empty($userId) || 1 > $userId) {
72
            return (string) $userId;
73
        }
74
75
        return $this->determineProfileLink($userId, null, $class, $image, $maxLength, $title);
76
    }
77
78
    /**
79
     * Create a link to a users profile from the username.
80
     *
81
     * Examples
82
     *
83
     *   Simple version, shows the username
84
     *   {{ username|profileLinkByUserName() }}
85
     *   Simple version, shows username, using class="classname"
86
     *   {{ username|profileLinkByUserName(class='classname') }}
87
     *   Using profile.gif instead of username, no class
88
     *   {{ username|profileLinkByUserName('image'='images/profile.gif') }}
89
     */
90
    public function profileLinkByUserName(
91
        string $userName,
92
        string $class = '',
93
        string $image = '',
94
        int $maxLength = 0,
95
        string $title = ''
96
    ): string {
97
        if (empty($userName)) {
98
            return $userName;
99
        }
100
101
        return $this->determineProfileLink(null, $userName, $class, $image, $maxLength, $title);
102
    }
103
104
    /**
105
     * Internal function used by profileLinkByUserId() and profileLinkByUserName().
106
     */
107
    private function determineProfileLink(
108
        int $userId = null,
109
        string $userName = null,
110
        string $class = '',
111
        string $imagePath = '',
112
        int $maxLength = 0,
113
        string $title = ''
114
    ): string {
115
        if (!isset($userId) && !isset($userName)) {
116
            throw new InvalidArgumentException();
117
        }
118
        /** @var User $user */
119
        if (null !== $userId) {
120
            $user = $this->userRepository->find($userId);
121
        } else {
122
            $user = $this->userRepository->findOneBy(['uname' => $userName]);
123
        }
124
        if (!$user) {
125
            return $userId . $userName; // one or the other is empty
126
        }
127
128
        $userDisplayName = $this->profileHelper->getDisplayName($user->getUid());
129
        if (!$userDisplayName) {
130
            $userDisplayName = $user->getUname();
131
        }
132
133
        $class = !empty($class) ? ' class="' . htmlspecialchars($class, ENT_QUOTES) . '"' : '';
134
135
        if (!empty($imagePath)) {
136
            $show = '<img src="' . htmlspecialchars($imagePath, ENT_QUOTES) . '" alt="' . htmlspecialchars($userDisplayName, ENT_QUOTES) . '" />';
137
        } elseif (0 < $maxLength) {
138
            // truncate the user name to $maxLength chars
139
            $length = mb_strlen($userDisplayName);
140
            $truncEnd = ($maxLength > $length) ? $length : $maxLength;
141
            $show = htmlspecialchars(s($userDisplayName)->slice(0, $truncEnd)->toString(), ENT_QUOTES);
142
        } else {
143
            $show = htmlspecialchars($userDisplayName, ENT_QUOTES);
144
        }
145
        $href = $this->profileHelper->getProfileUrl($user->getUid());
146
        if ('#' === $href) {
147
            return $userDisplayName;
148
        }
149
150
        if (empty($title)) {
151
            $title = $this->translator->trans('Profile') . ': ' . $userDisplayName;
152
        }
153
154
        return '<a' . $class . ' title="' . htmlspecialchars($title, ENT_QUOTES) . '" href="' . $href . '">' . $show . '</a>';
155
    }
156
}
157