Total Complexity | 40 |
Total Lines | 237 |
Duplicated Lines | 0 % |
Changes | 11 | ||
Bugs | 3 | Features | 2 |
Complex classes like SocialBarTwigExtension often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use SocialBarTwigExtension, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
7 | class SocialBarTwigExtension extends \Twig_Extension |
||
|
|||
8 | { |
||
9 | protected $container; |
||
10 | |||
11 | /** |
||
12 | * Constructor. |
||
13 | * |
||
14 | * @param ContainerInterface $container |
||
15 | */ |
||
16 | public function __construct(ContainerInterface $container) |
||
17 | { |
||
18 | $this->container = $container; |
||
19 | } |
||
20 | |||
21 | public function getName() |
||
22 | { |
||
23 | return 'azine_social_bar'; |
||
24 | } |
||
25 | |||
26 | public function getFunctions() |
||
27 | { |
||
28 | $functions['socialButtons'] = new \Twig_SimpleFunction('socialButtons', array($this, 'getSocialButtons'), array('is_safe' => array('html'))); |
||
29 | $functions['facebookButton'] = new \Twig_SimpleFunction('facebookButton', array($this, 'getFacebookButton'), array('is_safe' => array('html'))); |
||
30 | $functions['twitterButton'] = new \Twig_SimpleFunction('twitterButton', array($this, 'getTwitterButton'), array('is_safe' => array('html'))); |
||
31 | $functions['googlePlusButton'] = new \Twig_SimpleFunction('googlePlusButton', array($this, 'getGooglePlusButton'), array('is_safe' => array('html'))); |
||
32 | $functions['xingButton'] = new \Twig_SimpleFunction('xingButton', array($this, 'getXingButton'), array('is_safe' => array('html'))); |
||
33 | $functions['linkedInButton'] = new \Twig_SimpleFunction('linkedInButton', array($this, 'getLinkedInButton'), array('is_safe' => array('html'))); |
||
34 | |||
35 | return $functions; |
||
36 | } |
||
37 | |||
38 | /** |
||
39 | * Get all the buttons in one row. |
||
40 | * |
||
41 | * @param array $parameters |
||
42 | * @param string $action |
||
43 | */ |
||
44 | public function getSocialButtons(array $parameters = array(), $action = 'share') |
||
45 | { |
||
46 | $commonParams = $parameters; |
||
47 | $render_parameters = array(); |
||
48 | if (array_key_exists('facebook', $commonParams)) { |
||
49 | unset($commonParams['facebook']); |
||
50 | } |
||
51 | if (array_key_exists('twitter', $commonParams)) { |
||
52 | unset($commonParams['twitter']); |
||
53 | } |
||
54 | if (array_key_exists('googleplus', $commonParams)) { |
||
55 | unset($commonParams['googleplus']); |
||
56 | } |
||
57 | if (array_key_exists('xing', $commonParams)) { |
||
58 | unset($commonParams['xing']); |
||
59 | } |
||
60 | if (array_key_exists('linkedin', $commonParams)) { |
||
61 | unset($commonParams['linkedin']); |
||
62 | } |
||
63 | |||
64 | // no parameters were defined, keeps default values |
||
65 | if (!array_key_exists('facebook', $parameters)) { |
||
66 | $render_parameters['facebook'] = $parameters; |
||
67 | |||
68 | // parameters are defined, overrides default values |
||
69 | } elseif (is_array($parameters['facebook'])) { |
||
70 | $render_parameters['facebook'] = array_merge($commonParams, $parameters['facebook']); |
||
71 | |||
72 | // the button is not displayed |
||
73 | } else { |
||
74 | $render_parameters['facebook'] = false; |
||
75 | } |
||
76 | |||
77 | if (!array_key_exists('twitter', $parameters)) { |
||
78 | $render_parameters['twitter'] = $parameters; |
||
79 | } elseif (is_array($parameters['twitter'])) { |
||
80 | $render_parameters['twitter'] = array_merge($commonParams, $parameters['twitter']); |
||
81 | } else { |
||
82 | $render_parameters['twitter'] = false; |
||
83 | } |
||
84 | |||
85 | if (!array_key_exists('googleplus', $parameters)) { |
||
86 | $render_parameters['googleplus'] = $parameters; |
||
87 | } elseif (is_array($parameters['googleplus'])) { |
||
88 | $render_parameters['googleplus'] = array_merge($commonParams, $parameters['googleplus']); |
||
89 | } else { |
||
90 | $render_parameters['googleplus'] = false; |
||
91 | } |
||
92 | |||
93 | if (!array_key_exists('xing', $parameters)) { |
||
94 | $render_parameters['xing'] = $parameters; |
||
95 | } elseif (is_array($parameters['xing'])) { |
||
96 | $render_parameters['xing'] = array_merge($commonParams, $parameters['xing']); |
||
97 | } else { |
||
98 | $render_parameters['xing'] = false; |
||
99 | } |
||
100 | |||
101 | if (!array_key_exists('linkedin', $parameters)) { |
||
102 | $render_parameters['linkedin'] = $parameters; |
||
103 | } elseif (is_array($parameters['linkedin'])) { |
||
104 | $render_parameters['linkedin'] = array_merge($commonParams, $parameters['linkedin']); |
||
105 | } else { |
||
106 | $render_parameters['linkedin'] = false; |
||
107 | } |
||
108 | |||
109 | $render_parameters['action'] = $action; |
||
110 | $render_parameters['width'] = 130; |
||
111 | $render_parameters['height'] = 20; |
||
112 | |||
113 | // get the helper service and display the template |
||
114 | return $this->container->get('azine.socialBarHelper')->socialButtons($render_parameters); |
||
115 | } |
||
116 | |||
117 | /** |
||
118 | * Render the html for the facebook like button |
||
119 | * => https://developers.facebook.com/docs/reference/plugins/like/. |
||
120 | * |
||
121 | * @param array $parameters |
||
122 | */ |
||
123 | public function getFacebookButton($parameters = array(), $action = 'share') |
||
124 | { |
||
125 | // default values, you can override the values by setting them |
||
126 | $parameters = $parameters + array( |
||
127 | 'locale' => 'en_US', |
||
128 | 'send' => false, |
||
129 | 'width' => 130, |
||
130 | 'showFaces' => false, |
||
131 | 'layout' => 'button_count', |
||
132 | ); |
||
133 | |||
134 | if ('share' == $action) { |
||
135 | $parameters['url'] = array_key_exists('url', $parameters) ? $parameters['url'] : null; |
||
136 | $parameters['action'] = 'fb-like'; |
||
137 | } elseif ('follow' == $action) { |
||
138 | $parameters['url'] = $this->container->getParameter('azine_social_bar_fb_profile_url'); |
||
139 | $parameters['action'] = 'fb-follow'; |
||
140 | } else { |
||
141 | throw new \Exception("Unknown social action. Only 'share' and 'follow' are known at the moment."); |
||
142 | } |
||
143 | |||
144 | return $this->container->get('azine.socialBarHelper')->facebookButton($parameters); |
||
145 | } |
||
146 | |||
147 | /** |
||
148 | * Render the html for the twitter button |
||
149 | * =>. |
||
150 | * |
||
151 | * @param array $parameters |
||
152 | */ |
||
153 | public function getTwitterButton($parameters = array(), $action = 'share') |
||
174 | } |
||
175 | |||
176 | /** |
||
177 | * Render the html for the Google+ button |
||
178 | * =>. |
||
179 | * |
||
180 | * @param array $parameters |
||
181 | */ |
||
182 | public function getGooglePlusButton($parameters = array(), $action = 'share') |
||
205 | } |
||
206 | |||
207 | public function getLinkedInButton($parameters = array(), $action = 'share') |
||
225 | } |
||
226 | |||
227 | public function getXingButton($parameters = array(), $action = 'share') |
||
246 |