Completed
Push — master ( 507629...0e3516 )
by Dominik
02:42
created

Templating/SocialBarTwigExtension.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Azine\SocialBarBundle\Templating;
4
5
use Symfony\Component\DependencyInjection\ContainerInterface;
6
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;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $functions; (array<string,Twig_SimpleFunction>) is incompatible with the return type declared by the interface Twig_ExtensionInterface::getFunctions of type Twig_Function[].

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
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 View Code Duplication
        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 View Code Duplication
        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 View Code Duplication
        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 View Code Duplication
        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 View Code Duplication
        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 View Code Duplication
        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')
154
    {
155
        $parameters = $parameters + array(
156
            'url' => array_key_exists('url', $parameters) ? $parameters['url'] : null,
157
            'locale' => 'en',
158
            'message' => 'I want to share that page with you',
159
            'text' => 'Tweet',
160
            'via' => $this->container->getParameter('azine_social_bar_twitter_username'),
161
            'tag' => array_key_exists('tag', $parameters) ? $parameters['tag'] : $this->container->getParameter('azine_social_bar_twitter_username'),
162
            );
163
        if ('share' == $action) {
164
            $parameters['actionClass'] = 'twitter-share-button';
165
            $parameters['action'] = 'share';
166
        } elseif ('follow' == $action) {
167
            $parameters['actionClass'] = 'twitter-follow-button';
168
            $parameters['action'] = $this->container->getParameter('azine_social_bar_twitter_username');
169
        } else {
170
            throw new \Exception("Unknown social action. Only 'share' and 'follow' are known at the moment.");
171
        }
172
173
        return $this->container->get('azine.socialBarHelper')->twitterButton($parameters);
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')
183
    {
184
        $parameters = $parameters + array(
185
            'locale' => 'en',
186
            'size' => 'medium',
187
            'annotation' => 'bubble',
188
            'width' => 130,
189
            'height' => 20,
190
        );
191
192
        if ('share' == $action) {
193
            $parameters['url'] = array_key_exists('url', $parameters) ? $parameters['url'] : null;
194
            $parameters['rel'] = 'author';
195
            $parameters['action'] = 'g-plusone';
196
        } elseif ('follow' == $action) {
197
            $parameters['url'] = $this->container->getParameter('azine_social_bar_google_plus_profile_url');
198
            $parameters['rel'] = 'publisher';
199
            $parameters['action'] = 'g-follow';
200
        } else {
201
            throw new \Exception("Unknown social action. Only 'share' and 'follow' are known at the moment.");
202
        }
203
204
        return $this->container->get('azine.socialBarHelper')->googlePlusButton($parameters);
205
    }
206
207
    public function getLinkedInButton($parameters = array(), $action = 'share')
208
    {
209
        $parameters = $parameters + array(
210
            'locale' => 'en',
211
            'counterLocation' => 'right',
212
        );
213
214 View Code Duplication
        if ('share' == $action) {
215
            $parameters['action'] = 'IN/Share';
216
            $parameters['url'] = array_key_exists('url', $parameters) ? $parameters['url'] : null;
217
        } elseif ('follow' == $action) {
218
            $parameters['action'] = 'IN/FollowCompany';
219
            $parameters['companyId'] = $this->container->getParameter('azine_social_bar_linked_in_company_id');
220
        } else {
221
            throw new \Exception("Unknown social action. Only 'share' and 'follow' are known at the moment.");
222
        }
223
224
        return $this->container->get('azine.socialBarHelper')->linkedInButton($parameters);
225
    }
226
227
    public function getXingButton($parameters = array(), $action = 'share')
228
    {
229
        $parameters = $parameters + array(
230
            'locale' => 'en',
231
            'action' => 'XING/Share',
232
            'counterLocation' => 'right',
233
        );
234
235
        if ('share' == $action) {
236
            $parameters['url'] = array_key_exists('url', $parameters) ? $parameters['url'] : null;
237
        } elseif ('follow' == $action) {
238
            $parameters['url'] = $this->container->getParameter('azine_social_bar_xing_profile_url');
239
        } else {
240
            throw new \Exception("Unknown social action. Only 'share' and 'follow' are known at the moment.");
241
        }
242
243
        return $this->container->get('azine.socialBarHelper')->xingButton($parameters);
244
    }
245
}
246