Generic   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 170
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 3
Bugs 1 Features 1
Metric Value
wmc 18
lcom 1
cbo 7
dl 0
loc 170
rs 10
c 3
b 1
f 1

13 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getGlobals() 0 5 1
A getFunctions() 0 19 1
A getFilters() 0 9 1
A getSettingsParameter() 0 14 2
A getRandomString() 0 11 2
A calculatePercent() 0 14 3
A getObfuscatedEmail() 0 8 2
A getContextValue() 0 6 1
A cleanWhiteSpace() 0 4 1
A match() 0 4 1
A dumpExit() 0 5 1
A getName() 0 4 1
1
<?php
2
3
namespace DoS\ResourceBundle\Twig\Extension;
4
5
use Sylius\Bundle\SettingsBundle\Templating\Helper\SettingsHelper;
6
use Symfony\Component\DependencyInjection\ContainerInterface;
7
use Symfony\Component\PropertyAccess\PropertyAccess;
8
9
class Generic extends \Twig_Extension
10
{
11
    /**
12
     * @var ContainerInterface
13
     */
14
    protected $container;
15
16
    /**
17
     * @var SettingsHelper
18
     */
19
    protected $settingsHelper;
20
21
    public function __construct(ContainerInterface $container)
22
    {
23
        $this->container = $container;
24
        $this->settingsHelper = $container->get('sylius.templating.helper.settings');
25
    }
26
27
    /**
28
     * @inheritdoc
29
     */
30
    public function getGlobals()
31
    {
32
        return array(
33
        );
34
    }
35
36
    /**
37
     * @inheritdoc
38
     */
39
    public function getFunctions()
40
    {
41
        $self = array('is_safe' => array('all'));
42
43
        return array(
44
            new \Twig_SimpleFunction('d', '\Kint::dump', $self),
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...
45
            new \Twig_SimpleFunction('e', array($this, 'dumpExit'), $self),
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...
46
            new \Twig_SimpleFunction('is_string', 'is_string'),
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...
47
            new \Twig_SimpleFunction('ui_random_string', array($this, 'getRandomString'), $self),
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...
48
            new \Twig_SimpleFunction('ui_percentage', array($this, 'calculatePercent')),
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...
49
            new \Twig_SimpleFunction('ui_setting', array($this, 'getSettingsParameter')),
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...
50
            new \Twig_SimpleFunction('ui_settings', array($this->settingsHelper, 'getSettings')),
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...
51
            new \Twig_SimpleFunction('ui_param', array($this->container, 'getParameter')),
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...
52
            new \Twig_SimpleFunction('ui_param_has', array($this->container, 'hasParameter')),
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...
53
            new \Twig_SimpleFunction('ui_has', array($this->container, 'has')),
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...
54
            new \Twig_SimpleFunction('ui_obfuscated_email', array($this, 'getObfuscatedEmail')),
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...
55
            new \Twig_SimpleFunction('ui_property', array($this, 'getContextValue')),
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...
56
        );
57
    }
58
59
    public function getFilters()
60
    {
61
        //$self = array('is_safe' => array('all'));
0 ignored issues
show
Unused Code Comprehensibility introduced by
63% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
62
63
        return array(
64
            new \Twig_SimpleFilter('is_match', array($this, 'match')),
0 ignored issues
show
Deprecated Code introduced by
The class Twig_SimpleFilter 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...
65
            new \Twig_SimpleFilter('ui_no_space', array($this, 'cleanWhiteSpace')),
0 ignored issues
show
Deprecated Code introduced by
The class Twig_SimpleFilter 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...
66
        );
67
    }
68
69
    /**
70
     * @param $key
71
     * @param string $default
72
     *
73
     * @return null|mixed
74
     */
75
    public function getSettingsParameter($key, $default = null)
76
    {
77
        list($alias, $key) = explode('.', $key);
78
79
        $settings = $this->container->get('sylius.templating.helper.settings')
80
            ->getSettings($alias)
81
        ;
82
83
        if (array_key_exists($key, $settings)) {
84
            return $settings[$key];
85
        }
86
87
        return $default;
88
    }
89
90
    public function getRandomString($length = 8)
91
    {
92
        $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
93
        $randomString = '';
94
95
        for ($i = 0; $i < $length; $i++) {
96
            $randomString .= $characters[rand(0, strlen($characters) - 1)];
97
        }
98
99
        return $randomString;
100
    }
101
102
    /**
103
     * @param $total
104
     * @param $pie
105
     *
106
     * @return string
107
     */
108
    public function calculatePercent($total, $pie)
109
    {
110
        if (empty($total)) {
111
            return '0%';
112
        }
113
114
        $total = ($pie / $total) * 100;
115
116
        if (is_float($total)) {
117
            return number_format($total, 1).'%';
118
        }
119
120
        return $total.'%';
121
    }
122
123
    /**
124
     * @param $email
125
     *
126
     * @return string
127
     */
128
    public function getObfuscatedEmail($email)
129
    {
130
        if (false !== $pos = strpos($email, '@')) {
131
            $email = '...'.substr($email, $pos);
132
        }
133
134
        return $email;
135
    }
136
137
    /**
138
     * @param $context
139
     * @param $path
140
     *
141
     * @return mixed
142
     */
143
    public function getContextValue($context, $path)
144
    {
145
        $accessor = PropertyAccess::createPropertyAccessor();
146
147
        return $accessor->getValue($context, $path);
148
    }
149
150
    /**
151
     * @param $string
152
     *
153
     * @return string
154
     */
155
    public function cleanWhiteSpace($string)
156
    {
157
        return preg_replace('/ /', '', $string);
158
    }
159
160
    public function match($subject, $pattern)
161
    {
162
        return preg_match($pattern, $subject);
163
    }
164
165
    public function dumpExit($val)
166
    {
167
        \Kint::dump($val);
168
        exit;
0 ignored issues
show
Coding Style Compatibility introduced by
The method dumpExit() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
169
    }
170
171
    /**
172
     * @api
173
     */
174
    public function getName()
175
    {
176
        return 'ui_generic';
177
    }
178
}
179