PointUrlExtension   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 94
Duplicated Lines 25.53 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 12
c 0
b 0
f 0
lcom 1
cbo 3
dl 24
loc 94
rs 10

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getFunctions() 12 12 1
A getFilters() 12 12 1
A avatarSmallFunction() 0 4 1
A avatarMediumFunction() 0 4 1
A avatarLargeFunction() 0 4 1
A avatarFunction() 0 4 1
A userUrl() 0 4 1
A userBlogUrl() 0 4 1
A postUrl() 0 4 1
A getAvatarUrlByLogin() 0 8 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Skobkin\Bundle\PointToolsBundle\Twig;
4
5
use Skobkin\Bundle\PointToolsBundle\Entity\User;
6
7
class PointUrlExtension extends \Twig_Extension
8
{
9
    /**
10
     * @var string
11
     */
12
    private $pointDomain;
13
14
    /**
15
     * @var string
16
     */
17
    private $pointScheme;
18
19
    /**
20
     * @var string
21
     */
22
    private $pointBaseUrl;
23
24
    public function __construct(string $pointDomain, string $pointScheme, string $pointBaseUrl)
25
    {
26
        $this->pointDomain = $pointDomain;
27
        $this->pointScheme = $pointScheme;
28
        $this->pointBaseUrl = $pointBaseUrl;
29
    }
30
31 View Code Duplication
    public function getFunctions()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
32
    {
33
        return [
34
            new \Twig_SimpleFunction('point_avatar', [$this, 'avatarFunction']),
35
            new \Twig_SimpleFunction('point_avatar_small', [$this, 'avatarSmallFunction']),
36
            new \Twig_SimpleFunction('point_avatar_medium', [$this, 'avatarMediumFunction']),
37
            new \Twig_SimpleFunction('point_avatar_large', [$this, 'avatarLargeFunction']),
38
            new \Twig_SimpleFunction('point_user_url', [$this, 'userUrl']),
39
            new \Twig_SimpleFunction('point_user_blog_url', [$this, 'userBlogUrl']),
40
            new \Twig_SimpleFunction('point_post_url', [$this, 'postUrl']),
41
        ];
42
    }
43
44 View Code Duplication
    public function getFilters()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
45
    {
46
        return [
47
            new \Twig_SimpleFilter('point_avatar', [$this, 'avatarFunction']),
48
            new \Twig_SimpleFilter('point_avatar_small', [$this, 'avatarSmallFunction']),
49
            new \Twig_SimpleFilter('point_avatar_medium', [$this, 'avatarMediumFunction']),
50
            new \Twig_SimpleFilter('point_avatar_large', [$this, 'avatarLargeFunction']),
51
            new \Twig_SimpleFilter('point_user_url', [$this, 'userUrl']),
52
            new \Twig_SimpleFilter('point_user_blog_url', [$this, 'userBlogUrl']),
53
            new \Twig_SimpleFilter('point_post_url', [$this, 'postUrl']),
54
        ];
55
    }
56
57
    public function avatarSmallFunction(string $login): string
58
    {
59
        return $this->avatarFunction($login, User::AVATAR_SIZE_SMALL);
60
    }
61
62
    public function avatarMediumFunction(string $login): string
63
    {
64
        return $this->avatarFunction($login, User::AVATAR_SIZE_MEDIUM);
65
    }
66
67
    public function avatarLargeFunction(string $login): string
68
    {
69
        return $this->avatarFunction($login, User::AVATAR_SIZE_LARGE);
70
    }
71
72
    public function avatarFunction(string $login, $size): string
73
    {
74
        return $this->getAvatarUrlByLogin($login, $size);
75
    }
76
77
    public function userUrl(string $login): string
78
    {
79
        return sprintf('%s://%s.%s/', $this->pointScheme, $login, $this->pointDomain);
80
    }
81
82
    public function userBlogUrl(string $login): string
83
    {
84
        return sprintf('%s://%s.%s/blog/', $this->pointScheme, $login, $this->pointDomain);
85
    }
86
87
    public function postUrl(string $postId): string
88
    {
89
        return sprintf('%s://%s/%s', $this->pointScheme, $this->pointDomain, $postId);
90
    }
91
92
    private function getAvatarUrlByLogin(string $login, string $size): string
93
    {
94
        if (!in_array($size, [User::AVATAR_SIZE_SMALL, User::AVATAR_SIZE_MEDIUM, User::AVATAR_SIZE_LARGE], true)) {
95
            throw new \InvalidArgumentException('Avatar size must be one of restricted variants. See User::AVATAR_SIZE_* constants.');
96
        }
97
98
        return sprintf('%s://%s/avatar/%s/%s', $this->pointScheme, $this->pointDomain, urlencode($login), $size);
99
    }
100
}