Completed
Push — master ( 563bb9...105072 )
by
04:11
created

UserProfile::getName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace DoS\UserBundle\Twig;
4
5
use DoS\UserBundle\Model\UserInterface;
6
use Liip\ImagineBundle\Imagine\Cache\CacheManager;
7
8
class UserProfile extends \Twig_Extension
9
{
10
    /**
11
     * @var CacheManager
12
     */
13
    private $cacheManager;
14
15
    public function __construct(CacheManager $cacheManager)
16
    {
17
        $this->cacheManager = $cacheManager;
18
    }
19
20
    /**
21
     * @inheritdoc
22
     */
23
    public function getFunctions()
24
    {
25
        return array(
26
            new \Twig_SimpleFunction('ui_user_avatar', array($this, 'getUserAvartar')),
27
        );
28
    }
29
30
    /**
31
     * @param UserInterface $user
32
     * @param string        $filter
33
     * @param array         $runtimeConfig
34
     *
35
     * @return null|string
36
     */
37
    public function getUserAvartar(UserInterface $user = null, $filter = '70x70', array $runtimeConfig = array())
38
    {
39
        if (!$user) {
40
            return;
41
        }
42
43
        if ($avatar = $user->getProfilePicture()) {
44
            if (preg_match('/\/\//', $avatar)) {
45
                return $avatar;
46
            }
47
48
            if (empty($runtimeConfig)) {
49
                $runtimeConfig = array(
50
                    'thumbnail' => array(
51
                        "size" => explode('x', $filter),
52
                        "mode" => 'inset',
53
                    )
54
                );
55
            }
56
57
            /**
58
             * We must to define `sizing` filter first!
59
             * eg.
60
             *
61
             *   liip_imagine:
62
             *       filter_sets:
63
             *           sizing:
64
             *               data_loader: cmf_media_doctrine_phpcr
65
             *           filters:
66
             *               thumbnail: { size: [200, 200], mode: inset }
67
             */
68
            return $this->cacheManager->getBrowserPath($avatar, 'sizing', $runtimeConfig);
69
        }
70
71
        return;
72
    }
73
74
    /**
75
     * @inheritdoc
76
     */
77
    public function getName()
78
    {
79
        return 'ui_user_profile';
80
    }
81
}
82