Passed
Push — main ( 674e66...68245a )
by Axel
04:12
created

ProfileRuntime::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 0
c 0
b 0
f 0
dl 0
loc 5
rs 10
cc 1
nc 1
nop 3
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\ProfileBundle\Twig\Runtime;
15
16
use InvalidArgumentException;
17
use Symfony\Contracts\Translation\TranslatorInterface;
18
use Twig\Extension\RuntimeExtensionInterface;
19
use Zikula\ProfileBundle\Bridge\ProfileBundleBridge;
20
use Zikula\UsersBundle\Entity\User;
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 ProfileBundleBridge $profile,
28
        private readonly UserRepositoryInterface $userRepository,
29
        private readonly TranslatorInterface $translator
30
    ) {
31
    }
32
33
    /**
34
     * Displays the avatar of a given user.
35
     *
36
     * @param int|string $userId The user's id or name
37
     */
38
    public function getUserAvatar($userId = 0, array $parameters = []): string
39
    {
40
        return $this->profile->getAvatar($userId, $parameters);
41
    }
42
43
    /**
44
     * Create a link to a users profile from the UID.
45
     *
46
     * Examples
47
     *
48
     *   Simple version, shows the username
49
     *   {{ uid|profileLinkByUserId() }}
50
     *   Simple version, shows username, using class="classname"
51
     *   {{ uid|profileLinkByUserId(class='classname') }}
52
     *   Using profile.gif instead of username, no class
53
     *   {{ uid|profileLinkByUserId(image='images/profile.gif') }}
54
     */
55
    public function profileLinkByUserId(
56
        int $userId,
57
        string $class = '',
58
        string $image = '',
59
        int $maxLength = 0,
60
        string $title = ''
61
    ): string {
62
        if (empty($userId) || 1 > $userId) {
63
            return (string) $userId;
64
        }
65
66
        return $this->determineProfileLink($userId, null, $class, $image, $maxLength, $title);
67
    }
68
69
    /**
70
     * Create a link to a users profile from the username.
71
     *
72
     * Examples
73
     *
74
     *   Simple version, shows the username
75
     *   {{ username|profileLinkByUserName() }}
76
     *   Simple version, shows username, using class="classname"
77
     *   {{ username|profileLinkByUserName(class='classname') }}
78
     *   Using profile.gif instead of username, no class
79
     *   {{ username|profileLinkByUserName('image'='images/profile.gif') }}
80
     */
81
    public function profileLinkByUserName(
82
        string $userName,
83
        string $class = '',
84
        string $image = '',
85
        int $maxLength = 0,
86
        string $title = ''
87
    ): string {
88
        if (empty($userName)) {
89
            return $userName;
90
        }
91
92
        return $this->determineProfileLink(null, $userName, $class, $image, $maxLength, $title);
93
    }
94
95
    /**
96
     * Internal function used by profileLinkByUserId() and profileLinkByUserName().
97
     */
98
    private function determineProfileLink(
99
        int $userId = null,
100
        string $userName = null,
101
        string $class = '',
102
        string $imagePath = '',
103
        int $maxLength = 0,
104
        string $title = ''
105
    ): string {
106
        if (!isset($userId) && !isset($userName)) {
107
            throw new InvalidArgumentException();
108
        }
109
        /** @var User $user */
110
        if (null !== $userId) {
111
            $user = $this->userRepository->find($userId);
112
        } else {
113
            $user = $this->userRepository->findOneBy(['uname' => $userName]);
114
        }
115
        if (!$user) {
116
            return $userId . $userName; // one or the other is empty
117
        }
118
119
        $userDisplayName = $this->profile->getDisplayName($user->getUid());
120
        if (!$userDisplayName) {
121
            $userDisplayName = $user->getUname();
122
        }
123
124
        $class = !empty($class) ? ' class="' . htmlspecialchars($class, ENT_QUOTES) . '"' : '';
125
126
        if (!empty($imagePath)) {
127
            $show = '<img src="' . htmlspecialchars($imagePath, ENT_QUOTES) . '" alt="' . htmlspecialchars($userDisplayName, ENT_QUOTES) . '" />';
128
        } elseif (0 < $maxLength) {
129
            // truncate the user name to $maxLength chars
130
            $length = mb_strlen($userDisplayName);
131
            $truncEnd = ($maxLength > $length) ? $length : $maxLength;
132
            $show = htmlspecialchars(s($userDisplayName)->slice(0, $truncEnd)->toString(), ENT_QUOTES);
133
        } else {
134
            $show = htmlspecialchars($userDisplayName, ENT_QUOTES);
135
        }
136
        $href = $this->profile->getProfileUrl($user->getUid());
137
        if ('#' === $href) {
138
            return $userDisplayName;
139
        }
140
141
        if (empty($title)) {
142
            $title = $this->translator->trans('Profile') . ': ' . $userDisplayName;
143
        }
144
145
        return '<a' . $class . ' title="' . htmlspecialchars($title, ENT_QUOTES) . '" href="' . $href . '">' . $show . '</a>';
146
    }
147
}
148