Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
85.00% covered (warning)
85.00%
17 / 20
50.00% covered (danger)
50.00%
2 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
avatar
85.00% covered (warning)
85.00%
17 / 20
50.00% covered (danger)
50.00%
2 / 4
7.17
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getName
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getFunctions
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 get_avatar
86.67% covered (warning)
86.67%
13 / 15
0.00% covered (danger)
0.00%
0 / 1
4.04
1<?php
2/**
3 *
4 * This file is part of the phpBB Forum Software package.
5 *
6 * @copyright (c) phpBB Limited <https://www.phpbb.com>
7 * @license GNU General Public License, version 2 (GPL-2.0)
8 *
9 * For full copyright and license information, please see
10 * the docs/CREDITS.txt file.
11 *
12 */
13
14namespace phpbb\template\twig\extension;
15
16use phpbb\avatar\helper;
17use phpbb\avatar\manager;
18use phpbb\template\twig\environment;
19use Twig\Error\Error;
20use Twig\Extension\AbstractExtension;
21use Twig\TwigFunction;
22
23class avatar extends AbstractExtension
24{
25    /**
26     * @var helper
27     */
28    private $avatar_helper;
29
30    /**
31     * Constructor for avatar extension
32     *
33     * @param helper $avatar_helper
34     */
35    public function __construct(helper $avatar_helper)
36    {
37        $this->avatar_helper = $avatar_helper;
38    }
39
40    /**
41     * Get the name of this extension
42     *
43     * @return string
44     */
45    public function getName(): string
46    {
47        return 'avatar';
48    }
49
50    /**
51     * Returns a list of global functions to add to the existing list.
52     *
53     * @return TwigFunction[] An array of global functions
54     */
55    public function getFunctions(): array
56    {
57        return [
58            new TwigFunction('avatar', [$this, 'get_avatar'], ['needs_environment' => true]),
59        ];
60    }
61
62    /**
63     * Get avatar for placing into templates.
64     *
65     * How to use in a template:
66     * - {{ avatar('mode', row, alt, ignore_config, lazy) }}
67     *
68     * The mode and row (group_row or user_row) are required.
69     * The other fields (alt|ignore_config|lazy) are optional.
70     *
71     * @return string    The avatar HTML for the specified mode
72     */
73    public function get_avatar(environment $environment, string $mode, array $row, ?string $alt, ?bool $ignore_config, ?bool $lazy): string
74    {
75        $alt = $alt ?? false;
76        $ignore_config = $ignore_config ?? false;
77        $lazy = $lazy ?? false;
78        $row = manager::clean_row($row, $mode);
79        $avatar = $this->avatar_helper->get_avatar($row, $alt, $ignore_config, $lazy);
80
81        try
82        {
83            return $environment->render('macros/avatar.twig', [
84                'SRC'        => $avatar['lazy'] ? $this->avatar_helper->get_no_avatar_source() : $avatar['src'],
85                'DATA_SRC'    => $avatar['lazy'] ? $avatar['src'] : '',
86                'WIDTH'        => $avatar['width'],
87                'HEIGHT'    => $avatar['height'],
88                'TITLE'        => $avatar['title'],
89                'LAZY'        => $avatar['lazy'],
90            ]);
91        }
92        catch (Error $e)
93        {
94            return '';
95        }
96    }
97}