These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | namespace Azine\SocialBarBundle\Templating; |
||
3 | |||
4 | use Symfony\Component\DependencyInjection\ContainerInterface; |
||
5 | |||
6 | class SocialBarTwigExtension extends \Twig_Extension |
||
7 | { |
||
8 | protected $container; |
||
9 | |||
10 | /** |
||
11 | * Constructor. |
||
12 | * |
||
13 | * @param ContainerInterface $container |
||
14 | */ |
||
15 | 20 | public function __construct(ContainerInterface $container) |
|
16 | { |
||
17 | 20 | $this->container = $container; |
|
18 | 20 | } |
|
19 | |||
20 | 1 | public function getName() |
|
21 | { |
||
22 | 1 | return 'azine_social_bar'; |
|
23 | } |
||
24 | |||
25 | 1 | public function getFunctions() |
|
26 | { |
||
27 | 1 | $functions['socialButtons' ] = new \Twig_SimpleFunction('socialButtons' , array($this,'getSocialButtons'), array('is_safe' => array('html'))); |
|
0 ignored issues
–
show
|
|||
28 | 1 | $functions['facebookButton' ] = new \Twig_SimpleFunction('facebookButton' , array($this,'getFacebookButton'), array('is_safe' => array('html'))); |
|
29 | 1 | $functions['twitterButton' ] = new \Twig_SimpleFunction('twitterButton' , array($this,'getTwitterButton'), array('is_safe' => array('html'))); |
|
30 | 1 | $functions['googlePlusButton'] = new \Twig_SimpleFunction('googlePlusButton', array($this,'getGooglePlusButton'), array('is_safe' => array('html'))); |
|
31 | 1 | $functions['xingButton' ] = new \Twig_SimpleFunction('xingButton' , array($this,'getXingButton'), array('is_safe' => array('html'))); |
|
32 | 1 | $functions['linkedInButton' ] = new \Twig_SimpleFunction('linkedInButton' , array($this,'getLinkedInButton'), array('is_safe' => array('html'))); |
|
33 | 1 | return $functions; |
|
34 | } |
||
35 | |||
36 | /** |
||
37 | * Get all the buttons in one row |
||
38 | * @param array $parameters |
||
39 | * @param string $action |
||
40 | */ |
||
41 | 3 | public function getSocialButtons(array $parameters = array(), $action = "share") |
|
42 | { |
||
43 | 3 | $commonParams = $parameters; |
|
44 | 3 | $render_parameters = array(); |
|
45 | 3 | if(array_key_exists('facebook', $commonParams)) unset($commonParams['facebook']); |
|
46 | 3 | if(array_key_exists('twitter', $commonParams)) unset($commonParams['twitter']); |
|
47 | 3 | if(array_key_exists('googleplus', $commonParams)) unset($commonParams['googleplus']); |
|
48 | 3 | if(array_key_exists('xing', $commonParams)) unset($commonParams['xing']); |
|
49 | 3 | if(array_key_exists('linkedin', $commonParams)) unset($commonParams['linkedin']); |
|
50 | |||
51 | // no parameters were defined, keeps default values |
||
52 | 3 | View Code Duplication | if (!array_key_exists('facebook', $parameters)) { |
53 | 1 | $render_parameters['facebook'] = $parameters; |
|
54 | |||
55 | // parameters are defined, overrides default values |
||
56 | 3 | } elseif (is_array($parameters['facebook'])) { |
|
57 | 1 | $render_parameters['facebook'] = array_merge($commonParams, $parameters['facebook']); |
|
58 | |||
59 | // the button is not displayed |
||
60 | 1 | } else { |
|
61 | 1 | $render_parameters['facebook'] = false; |
|
62 | } |
||
63 | |||
64 | 3 | View Code Duplication | if (!array_key_exists('twitter', $parameters)) { |
65 | 1 | $render_parameters['twitter'] = $parameters; |
|
66 | |||
67 | 3 | } elseif (is_array($parameters['twitter'])) { |
|
68 | 1 | $render_parameters['twitter'] = array_merge($commonParams, $parameters['twitter']); |
|
69 | |||
70 | 1 | } else { |
|
71 | 1 | $render_parameters['twitter'] = false; |
|
72 | } |
||
73 | |||
74 | 3 | View Code Duplication | if (!array_key_exists('googleplus', $parameters)) { |
75 | 1 | $render_parameters['googleplus'] = $parameters; |
|
76 | |||
77 | 3 | } elseif (is_array($parameters['googleplus'])) { |
|
78 | 1 | $render_parameters['googleplus'] = array_merge($commonParams, $parameters['googleplus']); |
|
79 | |||
80 | 1 | } else { |
|
81 | 1 | $render_parameters['googleplus'] = false; |
|
82 | } |
||
83 | |||
84 | 3 | View Code Duplication | if (!array_key_exists('xing', $parameters)) { |
85 | 1 | $render_parameters['xing'] = $parameters; |
|
86 | |||
87 | 3 | } elseif (is_array($parameters['xing'])) { |
|
88 | 1 | $render_parameters['xing'] = array_merge($commonParams, $parameters['xing']); |
|
89 | |||
90 | 1 | } else { |
|
91 | 1 | $render_parameters['xing'] = false; |
|
92 | } |
||
93 | |||
94 | |||
95 | 3 | View Code Duplication | if (!array_key_exists('linkedin', $parameters)) { |
96 | 1 | $render_parameters['linkedin'] = $parameters; |
|
97 | |||
98 | 3 | } elseif (is_array($parameters['linkedin'])) { |
|
99 | 1 | $render_parameters['linkedin'] = array_merge($commonParams, $parameters['linkedin']); |
|
100 | |||
101 | 1 | } else { |
|
102 | 1 | $render_parameters['linkedin'] = false; |
|
103 | } |
||
104 | |||
105 | 3 | $render_parameters['action'] = $action; |
|
106 | 3 | $render_parameters['width'] = 130; |
|
107 | 3 | $render_parameters['height'] = 20; |
|
108 | |||
109 | |||
110 | // get the helper service and display the template |
||
111 | 3 | return $this->container->get('azine.socialBarHelper')->socialButtons($render_parameters); |
|
112 | } |
||
113 | |||
114 | /** |
||
115 | * Render the html for the facebook like button |
||
116 | * => https://developers.facebook.com/docs/reference/plugins/like/ |
||
117 | * @param array $parameters |
||
118 | */ |
||
119 | 3 | public function getFacebookButton($parameters = array(), $action = "share") |
|
120 | { |
||
121 | // default values, you can override the values by setting them |
||
122 | $parameters = $parameters + array( |
||
123 | 3 | 'locale' => 'en_US', |
|
124 | 3 | 'send' => false, |
|
125 | 3 | 'width' => 130, |
|
126 | 3 | 'showFaces' => false, |
|
127 | 3 | 'layout' => 'button_count', |
|
128 | 3 | ); |
|
129 | |||
130 | 3 | View Code Duplication | if ($action == "share") { |
131 | 1 | $parameters['url'] = array_key_exists('url', $parameters) ? $parameters['url'] : null; |
|
132 | 1 | $parameters['action'] = 'fb-like'; |
|
133 | |||
134 | 3 | } elseif ($action == "follow") { |
|
135 | 1 | $parameters['url'] = $this->container->getParameter("azine_social_bar_fb_profile_url"); |
|
136 | 1 | $parameters['action'] = "fb-follow"; |
|
137 | |||
138 | 1 | } else { |
|
139 | 1 | throw new \Exception("Unknown social action. Only 'share' and 'follow' are known at the moment."); |
|
140 | } |
||
141 | |||
142 | 2 | return $this->container->get('azine.socialBarHelper')->facebookButton($parameters); |
|
143 | } |
||
144 | |||
145 | /** |
||
146 | * Render the html for the twitter button |
||
147 | * => |
||
148 | * @param array $parameters |
||
149 | */ |
||
150 | 3 | public function getTwitterButton($parameters = array(), $action = "share") |
|
151 | { |
||
152 | $parameters = $parameters + array( |
||
153 | 3 | 'url' => array_key_exists('url', $parameters) ? $parameters['url'] : null, |
|
154 | 3 | 'locale' => 'en', |
|
155 | 3 | 'message' => 'I want to share that page with you', |
|
156 | 3 | 'text' => 'Tweet', |
|
157 | 3 | 'via' => $this->container->getParameter("azine_social_bar_twitter_username"), |
|
158 | 3 | 'tag' => array_key_exists('tag', $parameters) ? $parameters['tag'] : $this->container->getParameter("azine_social_bar_twitter_username"), |
|
159 | 3 | ); |
|
160 | 3 | if ($action == "share") { |
|
161 | 1 | $parameters['actionClass'] = "twitter-share-button"; |
|
162 | 1 | $parameters['action'] = "share"; |
|
163 | |||
164 | 3 | } elseif ($action == "follow") { |
|
165 | 1 | $parameters['actionClass'] = "twitter-follow-button"; |
|
166 | 1 | $parameters['action'] = $this->container->getParameter("azine_social_bar_twitter_username"); |
|
167 | |||
168 | 1 | } else { |
|
169 | 1 | throw new \Exception("Unknown social action. Only 'share' and 'follow' are known at the moment."); |
|
170 | } |
||
171 | |||
172 | 2 | return $this->container->get('azine.socialBarHelper')->twitterButton($parameters); |
|
173 | } |
||
174 | |||
175 | /** |
||
176 | * Render the html for the Google+ button |
||
177 | * => |
||
178 | * @param array $parameters |
||
179 | */ |
||
180 | 3 | public function getGooglePlusButton($parameters = array(), $action = "share") |
|
181 | { |
||
182 | $parameters = $parameters + array( |
||
183 | 3 | 'locale' => 'en', |
|
184 | 3 | 'size' => 'medium', |
|
185 | 3 | 'annotation' => 'bubble', |
|
186 | 3 | 'width' => 130, |
|
187 | 3 | 'height' => 20, |
|
188 | 3 | ); |
|
189 | |||
190 | 3 | if ($action == "share") { |
|
191 | 1 | $parameters['url'] = array_key_exists('url', $parameters) ? $parameters['url'] : null; |
|
192 | 1 | $parameters['rel'] = 'author'; |
|
193 | 1 | $parameters['action'] = 'g-plusone'; |
|
194 | |||
195 | 3 | } elseif ($action == "follow") { |
|
196 | 1 | $parameters['url'] = $this->container->getParameter("azine_social_bar_google_plus_profile_url"); |
|
197 | 1 | $parameters['rel'] = "publisher"; |
|
198 | 1 | $parameters['action'] = "g-follow"; |
|
199 | |||
200 | 1 | } else { |
|
201 | 1 | throw new \Exception("Unknown social action. Only 'share' and 'follow' are known at the moment."); |
|
202 | } |
||
203 | |||
204 | 2 | return $this->container->get('azine.socialBarHelper')->googlePlusButton($parameters); |
|
205 | } |
||
206 | |||
207 | 3 | public function getLinkedInButton($parameters = array(), $action = "share") |
|
208 | { |
||
209 | $parameters = $parameters + array( |
||
210 | 3 | 'locale' => 'en', |
|
211 | 3 | 'counterLocation' => 'right', |
|
212 | 3 | ); |
|
213 | |||
214 | 3 | View Code Duplication | if ($action == "share") { |
215 | 1 | $parameters['action'] = 'IN/Share'; |
|
216 | 1 | $parameters['url'] = array_key_exists('url', $parameters) ? $parameters['url'] : null; |
|
217 | |||
218 | 3 | } elseif ($action == "follow") { |
|
219 | 1 | $parameters['action'] = 'IN/FollowCompany'; |
|
220 | 1 | $parameters['companyId'] = $this->container->getParameter("azine_social_bar_linked_in_company_id"); |
|
221 | |||
222 | 1 | } else { |
|
223 | 1 | throw new \Exception("Unknown social action. Only 'share' and 'follow' are known at the moment."); |
|
224 | } |
||
225 | |||
226 | 2 | return $this->container->get('azine.socialBarHelper')->linkedInButton($parameters); |
|
227 | } |
||
228 | |||
229 | 3 | public function getXingButton($parameters = array(), $action = "share") |
|
230 | { |
||
231 | $parameters = $parameters + array( |
||
232 | 3 | 'locale' => 'en', |
|
233 | 3 | 'action' => 'XING/Share', |
|
234 | 3 | 'counterLocation' => 'right', |
|
235 | 3 | ); |
|
236 | |||
237 | 3 | if ($action == "share") { |
|
238 | 1 | $parameters['url'] = array_key_exists('url', $parameters) ? $parameters['url'] : null; |
|
239 | 3 | } elseif ($action == "follow") { |
|
240 | 1 | $parameters['url'] = $this->container->getParameter("azine_social_bar_xing_profile_url"); |
|
241 | 1 | } else { |
|
242 | 1 | throw new \Exception("Unknown social action. Only 'share' and 'follow' are known at the moment."); |
|
243 | } |
||
244 | |||
245 | 2 | return $this->container->get('azine.socialBarHelper')->xingButton($parameters); |
|
246 | } |
||
247 | } |
||
248 |
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.