Factory::createPublisher()   A
last analyzed

Complexity

Conditions 4
Paths 8

Size

Total Lines 38
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 4.0039

Importance

Changes 5
Bugs 1 Features 0
Metric Value
cc 4
eloc 17
c 5
b 1
f 0
nc 8
nop 8
dl 0
loc 38
ccs 15
cts 16
cp 0.9375
crap 4.0039
rs 9.7

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
namespace Facile\CrossbarHTTPPublisherBundle\Publisher;
4
5
use GuzzleHttp\Client as GuzzleClient;
6
7
/**
8
 * Class Factory
9
 * @package Facile\CrossbarHTTPPublisherBundle\Publisher
10
 */
11
class Factory
12
{
13
    /**
14
     * @param bool|string $hostname
15
     */
16
    public function createPublisher(
17
        string $protocol,
18
        string $host,
19
        int $port,
20
        string $path,
21
        ?string $key,
22
        ?string $secret,
23
        $hostname,
24 3
        bool $ignoreSsl = false
25
    ): Publisher {
26 3
        $config = [];
27
28 3
        $config['base_url'] = sprintf(
29 3
            '%s://%s:%s%s',
30 3
            $protocol,
31 3
            $host,
32 3
            $port,
33 3
            $path
34
        );
35 3
        $config['base_uri'] = $config['base_url']; // Guzzle 6 compat
36
37 3
        $config['headers']['Content-Type'] = 'application/json';
38
39 3
        if (null !== $hostname) {
0 ignored issues
show
introduced by
The condition null !== $hostname is always true.
Loading history...
40
            $config['headers']['Host'] = $hostname;
41
        }
42
43 3
        if ($ignoreSsl) {
44 3
            $config['verify'] = false;
45
        }
46
47 3
        if (defined('GuzzleHttp\ClientInterface::VERSION')) {
48
            @trigger_error('Guzzle versions before 7 are deprecated.', E_USER_DEPRECATED);
49 3
        }
50
51
        $guzzleClient = new GuzzleClient($config);
52
53
        return new Publisher($guzzleClient, $key, $secret);
54
    }
55
}
56