Completed
Push — master ( 52e56e...044348 )
by Cedric
03:57
created

Client::service()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 19
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

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