UserProfile::getFunctions()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
c 0
b 0
f 0
rs 9.4285
cc 1
eloc 3
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')),
0 ignored issues
show
Deprecated Code introduced by
The class Twig_SimpleFunction has been deprecated with message: to be removed in 3.0

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
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