Completed
Pull Request — master (#4522)
by Craig
13:45
created

MessageRuntime::messageSendLink()   A

Complexity

Conditions 5
Paths 7

Size

Total Lines 21
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 5
eloc 10
nc 7
nop 4
dl 0
loc 21
rs 9.6111
c 1
b 0
f 1
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\UsersModule\Twig\Runtime;
15
16
use Symfony\Contracts\Translation\TranslatorInterface;
17
use Twig\Extension\RuntimeExtensionInterface;
18
use Zikula\UsersModule\Collector\MessageModuleCollector;
19
use Zikula\UsersModule\Entity\RepositoryInterface\UserRepositoryInterface;
20
use Zikula\UsersModule\Entity\UserEntity;
21
22
class MessageRuntime implements RuntimeExtensionInterface
23
{
24
    /**
25
     * @var UserRepositoryInterface
26
     */
27
    private $userRepository;
28
29
    /**
30
     * @var MessageModuleCollector
31
     */
32
    private $messageModuleCollector;
33
34
    /**
35
     * @var TranslatorInterface
36
     */
37
    private $translator;
38
39
    public function __construct(
40
        UserRepositoryInterface $userRepository,
41
        MessageModuleCollector $messageModuleCollector,
42
        TranslatorInterface $translator
43
    ) {
44
        $this->userRepository = $userRepository;
45
        $this->messageModuleCollector = $messageModuleCollector;
46
        $this->translator = $translator;
47
    }
48
49
    /**
50
     * Display a link to a user's inbox.
51
     *
52
     * @param int|string $userId The user's id or name
53
     */
54
    public function messageInboxLink(
55
        $userId = null,
56
        bool $urlOnly = false,
57
        string $text = '',
58
        string $class = ''
59
    ): string {
60
        $url = $this->messageModuleCollector->getSelected()->getInboxUrl($userId);
61
        if ($urlOnly) {
62
            return $url;
63
        }
64
        $class = !empty($class) ? ' class="' . htmlspecialchars($class, ENT_QUOTES) . '"' : '';
65
        $text = !empty($text) ? htmlspecialchars($text, ENT_QUOTES) : $this->translator->trans('Inbox');
66
67
        return '<a' . $class . ' title="' . $this->translator->trans('Messages inbox') . '" href="' . $url . '">' . $text . '</a>';
68
    }
69
70
    /**
71
     * Display a link to send a message to the given user.
72
     *
73
     * @param int|string $userId The user's id or name
74
     */
75
    public function messageSendLink(
76
        $userId = null,
77
        bool $urlOnly = false,
78
        string $text = '',
79
        string $class = ''
80
    ): string {
81
        $url = $this->messageModuleCollector->getSelected()->getSendMessageUrl($userId);
82
        if ($urlOnly) {
83
            return $url;
84
        }
85
        $class = !empty($class) ? ' class="' . htmlspecialchars($class, ENT_QUOTES) . '"' : '';
86
87
        if (!empty($text)) {
88
            $text = htmlspecialchars($text, ENT_QUOTES);
89
        } else {
90
            /** @var UserEntity $user */
91
            $user = $this->userRepository->find($userId);
92
            $text = null !== $user ? $user->getUname() : '';
93
        }
94
95
        return '<a' . $class . ' title="' . $this->translator->trans('Send a message to this user') . '" href="' . $url . '">' . $text . '</a>';
96
    }
97
98
    /**
99
     * Retrieve the total or unread message count for the given user.
100
     *
101
     * @param int|string $userId The user's id or name
102
     */
103
    public function messageCount(
104
        $userId = null,
105
        bool $unreadOnly = false
106
    ): int {
107
        return $this->messageModuleCollector->getSelected()->getMessageCount($userId, $unreadOnly);
108
    }
109
}
110