summaryrefslogtreecommitdiff
path: root/app/Core/User/Avatar/AvatarManager.php
blob: 5b61cbdb5813134af7fd742d4b5997a96e80b9da (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
<?php

namespace Kanboard\Core\User\Avatar;

/**
 * Avatar Manager
 *
 * @package  avatar
 * @author   Frederic Guillot
 */
class AvatarManager
{
    /**
     * Providers
     *
     * @access private
     * @var AvatarProviderInterface[]
     */
    private $providers = array();

    /**
     * Register a new Avatar provider
     *
     * @access public
     * @param  AvatarProviderInterface $provider
     * @return $this
     */
    public function register(AvatarProviderInterface $provider)
    {
        $this->providers[] = $provider;
        return $this;
    }

    /**
     * Render avatar HTML element
     *
     * @access public
     * @param  string   $user_id
     * @param  string   $username
     * @param  string   $name
     * @param  string   $email
     * @param  string   $avatar_path
     * @param  int      $size
     * @return string
     */
    public function render($user_id, $username, $name, $email, $avatar_path, $size)
    {
        $user = array(
            'id' => $user_id,
            'username' => $username,
            'name' => $name,
            'email' => $email,
            'avatar_path' => $avatar_path,
        );

        krsort($this->providers);

        foreach ($this->providers as $provider) {
            if ($provider->isActive($user)) {
                return $provider->render($user, $size);
            }
        }

        return '';
    }

    /**
     * Render default provider for unknown users (first provider registered)
     *
     * @access public
     * @param  integer $size
     * @return string
     */
    public function renderDefault($size)
    {
        if (count($this->providers) > 0) {
            ksort($this->providers);
            $provider = current($this->providers);

            $user = array(
                'id' => 0,
                'username' => '',
                'name' => '?',
                'email' => '',
                'avatar_path' => '',
            );

            return $provider->render($user, $size);
        }

        return '';
    }
}