Completed
Push — master ( ea5582...dd6b15 )
by Cedric
06:36
created

ClientBuilder::setSerializer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
/*
3
 * This file is part of the Adlogix package.
4
 *
5
 * (c) Allan Segebarth <[email protected]>
6
 * (c) Jean-Jacques Courtens <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Adlogix\ConfluenceClient;
13
14
use Adlogix\ConfluenceClient\HttpClient\HttpClient;
15
use Adlogix\GuzzleAtlassianConnect\Middleware\ConnectMiddleware;
16
use Adlogix\GuzzleAtlassianConnect\Security\AuthenticationInterface;
17
use GuzzleHttp\HandlerStack;
18
use JMS\Serializer\SerializerBuilder;
19
20
21
/**
22
 * Class ClientBuilder
23
 * @package Adlogix\ConfluenceClient
24
 * @author  Cedric Michaux <[email protected]>
25
 */
26
class ClientBuilder
27
{
28
29
    /**
30
     * @var string
31
     */
32
    private $baseUri;
33
34
    /**
35
     * @var AuthenticationInterface
36
     */
37
    private $authentication;
38
39
    /**
40
     * @var SerializerInterface
41
     */
42
    private $serializer;
43
44
    /**
45
     * @var boolean
46
     */
47
    private $debug;
48
49
50
    /**
51
     * @param string                  $baseUri
52
     * @param AuthenticationInterface $authenticationMethod
53
     *
54
     * @return ClientBuilder
55
     */
56
    public static function create($baseUri, AuthenticationInterface $authenticationMethod)
57
    {
58
        return new static($baseUri, $authenticationMethod);
59
    }
60
61
    /**
62
     * ClientBuilder constructor.
63
     *
64
     * @param                                                        $baseUri
65
     * @param AuthenticationInterface                                $authentication
66
     */
67
    private function __construct($baseUri, AuthenticationInterface $authentication)
68
    {
69
        $this->authentication = $authentication;
70
71
        if (empty($baseUri)) {
72
            throw new \InvalidArgumentException("The baseUri cannot be empty");
73
        }
74
        $this->baseUri = $baseUri;
75
    }
76
77
    /**
78
     * @return Client
79
     */
80
    public function build()
81
    {
82
        $this->serializer = $this->serializer ?: $this->buildDefaultSerializer();
83
        $stack = HandlerStack::create();
84
        $stack->push(new ConnectMiddleware($this->authentication, $this->baseUri));
85
        
86
87
        return new Client(
88
            new HttpClient([
89
                'base_uri' => $this->baseUri,
90
                'handler' => $stack,
91
                'debug' => $this->debug
92
            ]),
93
            $this->serializer,
94
            $this->authentication
0 ignored issues
show
Unused Code introduced by
The call to Client::__construct() has too many arguments starting with $this->authentication.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
95
        );
96
    }
97
98
    /**
99
     * @param SerializerInterface $serializer
100
     */
101
    public function setSerializer(SerializerInterface $serializer)
102
    {
103
        $this->serializer = $serializer;
104
    }
105
106
107
    /**
108
     * @param boolean $debug
109
     *
110
     * @return $this
111
     */
112
    public function setDebug($debug)
113
    {
114
        $this->debug = (bool)$debug;
115
        return $this;
116
    }
117
118
    private function buildDefaultSerializer()
119
    {
120
        return SerializerBuilder::create()
121
            ->addMetadataDir(__DIR__ . '/Resources/serializer', __NAMESPACE__)
122
            ->addDefaultHandlers()
123
            ->build();
124
    }
125
}
126