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

Client::__call()   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 2
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
15
use Adlogix\ConfluenceClient\Exception\ExceptionWrapper;
16
use Adlogix\ConfluenceClient\HttpClient\HttpClientInterface;
17
use Adlogix\ConfluenceClient\Security\Authentication\AuthenticationInterface;
18
use Adlogix\ConfluenceClient\Service\AuthenticationService;
19
use Adlogix\ConfluenceClient\Service\ContentService;
20
use Adlogix\ConfluenceClient\Service\DescriptorService;
21
use Adlogix\ConfluenceClient\Service\ServiceInterface;
22
use Adlogix\ConfluenceClient\Service\SpaceService;
23
use GuzzleHttp\Exception\RequestException;
24
use JMS\Serializer\SerializerInterface;
25
26
/**
27
 * Class Client
28
 * @package Adlogix\ConfluenceClient
29
 * @author  Cedric Michaux <[email protected]>
30
 *
31
 * @method SpaceService spaces
32
 * @method ContentService pages
33
 * @method DescriptorService descriptor
34
 * @method AuthenticationService authentication
35
 */
36
class Client
37
{
38
39
    /**
40
     * @var HttpClientInterface
41
     */
42
    private $httpClient;
43
44
    /**
45
     * @var SerializerInterface
46
     */
47
    private $serializer;
48
49
50
51
    /**
52
     * Client constructor.
53
     *
54
     * @param HttpClientInterface     $httpClient
55
     * @param SerializerInterface     $serializer
56
     */
57
    public function __construct(
58
        HttpClientInterface $httpClient,
59
        SerializerInterface $serializer
60
    ) {
61
        $this->httpClient = $httpClient;
62
        $this->serializer = $serializer;
63
    }
64
65
66
    /**
67
     * @param $name
68
     *
69
     * @return ServiceInterface
70
     */
71
    public function service($name)
72
    {
73
        switch ($name) {
74
            case 'space':
75
            case 'spaces':
76
                $service = new SpaceService($this->serializer, $this->httpClient);
77
                break;
78
79
            case 'content':
80
            case 'contents':
81
                $service = new ContentService($this->serializer, $this->httpClient);
82
                break;
83
84
            case 'content':
85
            case 'contents':
86
                $service = new ContentService($this->serializer, $this->httpClient);
87
                break;
88
89
            default:
90
                throw new \InvalidArgumentException(sprintf('Undefined service instance called: %s', $name));
91
        }
92
93
        return $service;
94
    }
95
96
    /**
97
     * @param string $name
98
     * @param        $args
99
     *
100
     * @return ServiceInterface
101
     */
102
    public function __call($name, $args)
103
    {
104
        return $this->service($name);
105
    }
106
107
108
    /**
109
     * @param string $method
110
     * @param string $uri
111
     * @param null   $json
112
     * @param array  $options
113
     *
114
     * @return \GuzzleHttp\Psr7\Response
115
     */
116
    public function sendRawApiRequest($method, $uri, $json = null, array $options = [])
117
    {
118
        try {
119
            return $this->httpClient->apiRequest($method, $uri, $json, $options);
120
121
        } catch (RequestException $exception) {
122
            throw ExceptionWrapper::wrap($exception, $this->serializer);
123
        }
124
    }
125
126
127
    /**
128
     * @param string $url
129
     *
130
     * @return \GuzzleHttp\Psr7\Response
131
     */
132
    public function downloadAttachment($url)
133
    {
134
        try {
135
            $url = str_replace(" ", "%20", $url);
136
            return $this->httpClient->attachmentRequest($url);
137
        } catch (RequestException $exception) {
138
            throw ExceptionWrapper::wrap($exception, $this->serializer);
139
        }
140
    }
141
}
142