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.
Completed
Push — master ( 996e86...eeda13 )
by
unknown
13s
created

EightPointsGuzzleExtension::defineFormatter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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