GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Passed
Pull Request — master (#156)
by
unknown
01:44
created

defineRequestTimeMiddleware()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 10
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
eloc 7
nc 1
nop 3
dl 10
loc 10
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace EightPoints\Bundle\GuzzleBundle\DependencyInjection;
4
5
use EightPoints\Bundle\GuzzleBundle\EightPointsGuzzleBundlePlugin;
6
use Symfony\Component\DependencyInjection\ContainerBuilder;
7
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
8
use Symfony\Component\DependencyInjection\Reference;
9
use Symfony\Component\DependencyInjection\Definition;
10
use Symfony\Component\Config\FileLocator;
11
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
12
use Symfony\Component\ExpressionLanguage\Expression;
13
use GuzzleHttp\HandlerStack;
14
15
/**
16
 * @version   1.0
17
 * @since     2013-10
18
 */
19
class EightPointsGuzzleExtension extends Extension
20
{
21
    /** @var EightPointsGuzzleBundlePlugin[] */
22
    protected $plugins;
23
24
    /**
25
     * @param EightPointsGuzzleBundlePlugin[] $plugins
26
     */
27
    public function __construct(array $plugins = [])
28
    {
29
        $this->plugins = $plugins;
30
    }
31
32
    /**
33
     * {@inheritdoc}
34
     */
35
    public function getConfiguration(array $config, ContainerBuilder $container) : Configuration
36
    {
37
        return new Configuration($this->getAlias(), $container->getParameter('kernel.debug'), $this->plugins);
38
    }
39
40
    /**
41
     * Loads the Guzzle configuration.
42
     *
43
     * @version 1.0
44
     * @since   2013-10
45
     *
46
     * @param   array            $configs   an array of configuration settings
47
     * @param   ContainerBuilder $container a ContainerBuilder instance
48
     *
49
     * @throws  \InvalidArgumentException
50
     * @throws  \Symfony\Component\DependencyInjection\Exception\BadMethodCallException
51
     * @throws  \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
52
     * @throws  \Exception
53
     */
54
    public function load(array $configs, ContainerBuilder $container)
55
    {
56
        $configPath = implode(DIRECTORY_SEPARATOR, [__DIR__, '..', 'Resources', 'config']);
57
        $loader     = new XmlFileLoader($container, new FileLocator($configPath));
58
59
        $loader->load('services.xml');
60
61
        $configuration = new Configuration($this->getAlias(), $container->getParameter('kernel.debug'), $this->plugins);
62
        $config        = $this->processConfiguration($configuration, $configs);
63
        $logging       = $config['logging'] === true;
64
65
        foreach ($this->plugins as $plugin) {
66
            $container->addObjectResource(new \ReflectionClass(get_class($plugin)));
67
            $plugin->load($config, $container);
68
        }
69
70
        if ($logging) {
71
            $this->defineLogger($container);
72
            $this->defineDataCollector($container);
73
            $this->defineFormatter($container);
74
        }
75
76
        foreach ($config['clients'] as $name => $options) {
77
            $argument = [
78
                'base_uri' => $options['base_url'],
79
                'handler'  => $this->createHandler($container, $name, $options, $logging)
80
            ];
81
82
            // if present, add default options to the constructor argument for the Guzzle client
83
            if (isset($options['options']) && is_array($options['options'])) {
84
                foreach ($options['options'] as $key => $value) {
85
                    if ($value === null || (is_array($value) && count($value) === 0)) {
86
                        continue;
87
                    }
88
89
                    $argument[$key] = $value;
90
                }
91
            }
92
93
            $client = new Definition($options['class']);
94
            $client->addArgument($argument);
95
            $client->setPublic(true);
96
97
            // set service name based on client name
98
            $serviceName = sprintf('%s.client.%s', $this->getAlias(), $name);
99
            $container->setDefinition($serviceName, $client);
100
        }
101
    }
102
103
    /**
104
     * @since  2015-07
105
     *
106
     * @param  ContainerBuilder $container
107
     * @param  string           $clientName
108
     * @param  array            $options
109
     * @param  bool             $logging
110
     *
111
     * @return Definition
112
     * @throws \Symfony\Component\DependencyInjection\Exception\BadMethodCallException
113
     * @throws \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
114
     */
115
    protected function createHandler(ContainerBuilder $container, string $clientName, array $options, bool $logging) : Definition
116
    {
117
        // Event Dispatching service
118
        $eventServiceName = sprintf('eight_points_guzzle.middleware.event_dispatch.%s', $clientName);
119
        $eventService = $this->createEventMiddleware($clientName);
120
        $container->setDefinition($eventServiceName, $eventService);
121
122
        // Create the event Dispatch Middleware
123
        $eventExpression  = new Expression(sprintf("service('%s').dispatchEvent()", $eventServiceName));
124
125
        $handler = new Definition(HandlerStack::class);
126
        $handler->setFactory([HandlerStack::class, 'create']);
127
        $handler->setPublic(true);
128
129
        if ($logging) {
130
            $this->defineLogMiddleware($container, $handler, $clientName);
131
            $this->defineRequestTimeMiddleware($container, $handler, $clientName);
132
        }
133
134
        foreach ($this->plugins as $plugin) {
135
            $plugin->loadForClient($options['plugin'][$plugin->getPluginName()], $container, $clientName, $handler);
136
        }
137
138
        // goes on the end of the stack.
139
        $handler->addMethodCall('unshift', [$eventExpression, 'events']);
140
141
        return $handler;
142
    }
143
144
    /**
145
     * Define Logger
146
     *
147
     * @since  2015-07
148
     *
149
     * @param  ContainerBuilder $container
150
     *
151
     * @throws \Symfony\Component\DependencyInjection\Exception\BadMethodCallException
152
     */
153
    protected function defineLogger(ContainerBuilder $container)
154
    {
155
        $loggerDefinition = new Definition('%eight_points_guzzle.logger.class%');
156
        $loggerDefinition->setPublic(true);
157
        $container->setDefinition('eight_points_guzzle.logger', $loggerDefinition);
158
    }
159
160
    /**
161
     * Define Data Collector
162
     *
163
     * @since  2015-07
164
     *
165
     * @param  ContainerBuilder $container
166
     *
167
     * @throws \Symfony\Component\DependencyInjection\Exception\BadMethodCallException
168
     */
169
    protected function defineDataCollector(ContainerBuilder $container)
170
    {
171
        $dataCollectorDefinition = new Definition('%eight_points_guzzle.data_collector.class%');
172
        $dataCollectorDefinition->addArgument(new Reference('eight_points_guzzle.logger'));
173
        $dataCollectorDefinition->setPublic(false);
174
        $dataCollectorDefinition->addTag('data_collector', [
175
            'id' => 'eight_points_guzzle',
176
            'template' => '@EightPointsGuzzle/debug.html.twig',
177
        ]);
178
        $container->setDefinition('eight_points_guzzle.data_collector', $dataCollectorDefinition);
179
    }
180
181
    /**
182
     * Define Formatter
183
     *
184
     * @param  ContainerBuilder $container
185
     *
186
     * @throws \Symfony\Component\DependencyInjection\Exception\BadMethodCallException
187
     */
188
    protected function defineFormatter(ContainerBuilder $container)
189
    {
190
        $formatterDefinition = new Definition('%eight_points_guzzle.formatter.class%');
191
        $formatterDefinition->setPublic(true);
192
        $container->setDefinition('eight_points_guzzle.formatter', $formatterDefinition);
193
    }
194
195
    /**
196
     * Define Request Time Middleware
197
     *
198
     * @param \Symfony\Component\DependencyInjection\ContainerBuilder $container
199
     * @param \Symfony\Component\DependencyInjection\Definition $handler
200
     * @param string $clientName
201
     */
202 View Code Duplication
    protected function defineRequestTimeMiddleware(ContainerBuilder $container, Definition $handler, string $clientName)
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...
203
    {
204
        $requestTimeMiddlewareDefinitionName = sprintf('eight_points_guzzle.middleware.request_time.%s', $clientName);
205
        $requestTimeMiddlewareDefinition = new Definition('%eight_points_guzzle.middleware.request_time.class%');
206
        $requestTimeMiddlewareDefinition->addArgument(new Reference('eight_points_guzzle.data_collector'));
207
        $requestTimeMiddlewareDefinition->setPublic(false);
208
        $container->setDefinition($requestTimeMiddlewareDefinitionName, $requestTimeMiddlewareDefinition);
209
210
        $requestTimeExpression = new Expression(sprintf("service('%s')", $requestTimeMiddlewareDefinitionName));
211
        $handler->addMethodCall('push', [$requestTimeExpression, 'request_time']);
212
    }
213
214
    /**
215
     * Define Log Middleware for client
216
     *
217
     * @param ContainerBuilder $container
218
     * @param Definition $handler
219
     * @param string $clientName
220
     */
221 View Code Duplication
    protected function defineLogMiddleware(ContainerBuilder $container, Definition $handler, string $clientName)
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...
222
    {
223
        $logMiddlewareDefinitionName = sprintf('eight_points_guzzle.middleware.log.%s', $clientName);
224
        $logMiddlewareDefinition = new Definition('%eight_points_guzzle.middleware.log.class%');
225
        $logMiddlewareDefinition->addArgument(new Reference('eight_points_guzzle.logger'));
226
        $logMiddlewareDefinition->addArgument(new Reference('eight_points_guzzle.formatter'));
227
        $logMiddlewareDefinition->setPublic(true);
228
        $container->setDefinition($logMiddlewareDefinitionName, $logMiddlewareDefinition);
229
230
        $logExpression = new Expression(sprintf("service('%s').log()", $logMiddlewareDefinitionName));
231
        $handler->addMethodCall('push', [$logExpression, 'log']);
232
    }
233
234
    /**
235
     * Create Middleware For dispatching events
236
     *
237
     * @since  2015-09
238
     *
239
     * @param  string $name
240
     *
241
     * @return Definition
242
     */
243
    protected function createEventMiddleware(string $name) : Definition
244
    {
245
        $eventMiddleWare = new Definition('%eight_points_guzzle.middleware.event_dispatcher.class%');
246
        $eventMiddleWare->addArgument(new Reference('event_dispatcher'));
247
        $eventMiddleWare->addArgument($name);
248
        $eventMiddleWare->setPublic(true);
249
250
        return $eventMiddleWare;
251
    }
252
253
    /**
254
     * Returns alias of extension
255
     *
256
     * @version 1.1
257
     * @since   2013-12
258
     *
259
     * @return  string
260
     */
261
    public function getAlias() : string
262
    {
263
        return 'eight_points_guzzle';
264
    }
265
}
266