ConfluenceClient::content()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
declare(strict_types=1);
3
4
namespace CloudPlayDev\ConfluenceClient;
5
6
use CloudPlayDev\ConfluenceClient\Api\Content;
7
use CloudPlayDev\ConfluenceClient\HttpClient\Builder;
8
use Http\Client\Common\HttpMethodsClientInterface;
9
use Http\Client\Common\Plugin\AddHostPlugin;
10
use Http\Client\Common\Plugin\AddPathPlugin;
11
use Http\Client\Common\Plugin\AuthenticationPlugin;
12
use Http\Client\Common\Plugin\HeaderDefaultsPlugin;
13
use Http\Message\Authentication\BasicAuth;
14
use Http\Message\Authentication\Bearer;
15
16
class ConfluenceClient
17
{
18
    /**
19
     * Default User-Agent send with every request
20
     * Use protected visibility to facilitate extension
21
     * @see http://fabien.potencier.org/pragmatism-over-theory-protected-vs-private.html
22
     */
23
    protected const USER_AGENT = 'cloudplaydev-confluence-php-client/0.1.0';
24
25
    /**
26
     * Use protected visibility to facilitate extension
27
     * @see http://fabien.potencier.org/pragmatism-over-theory-protected-vs-private.html
28
     */
29
    protected Builder $httpClientBuilder;
30
31
    public function __construct(string $confluenceHostUrl, Builder $httpClientBuilder = null)
32
    {
33
        $this->httpClientBuilder = $httpClientBuilder ?: new Builder();
34
        $this->httpClientBuilder->addPlugin(new HeaderDefaultsPlugin([
35
            'User-Agent' => self::USER_AGENT,
36
        ]));
37
38
        $this->setUrl($confluenceHostUrl);
39
    }
40
41
    public function authenticate(string $token): ConfluenceClient
42
    {
43
        $this->httpClientBuilder->removePlugin(AuthenticationPlugin::class);
44
        $this->httpClientBuilder->addPlugin(new AuthenticationPlugin(new Bearer($token)));
45
46
        return $this;
47
    }
48
49
    public function authenticateBasicAuth(string $username, string $password): ConfluenceClient
50
    {
51
        $this->httpClientBuilder->removePlugin(AuthenticationPlugin::class);
52
        $this->httpClientBuilder->addPlugin(new AuthenticationPlugin(new BasicAuth($username, $password)));
53
54
        return $this;
55
    }
56
57
    public function content(): Content
58
    {
59
        return new Content($this);
60
    }
61
62
    /**
63
     * Get the HTTP client.
64
     *
65
     * @return HttpMethodsClientInterface
66
     */
67
    public function getHttpClient(): HttpMethodsClientInterface
68
    {
69
        return $this->httpClientBuilder->getHttpClient();
70
    }
71
72
    /**
73
     * Register basic properties about original URI in order to
74
     * restore them on every request
75
     *
76
     * @param string $url
77
     * @return void
78
     */
79
    public function setUrl(string $url): void
80
    {
81
        $uri = $this->httpClientBuilder->getUriFactory()->createUri($url);
82
83
        $this->httpClientBuilder->removePlugin(AddHostPlugin::class);
84
        $this->httpClientBuilder->addPlugin(new AddHostPlugin($uri));
85
86
        // Prepend path if any
87
        if ($uri->getPath()) {
88
            $this->httpClientBuilder->removePlugin(AddPathPlugin::class);
89
            $this->httpClientBuilder->addPlugin(new AddPathPlugin($uri));
90
        }
91
92
        // Report userInfo as Basic authentication
93
        $userInfo = $uri->getUserInfo();
94
        if (!empty($userInfo) && str_contains($userInfo, ':')) {
95
            $this->httpClientBuilder->addPlugin(new AuthenticationPlugin(new BasicAuth(...explode(':', $userInfo, 2))));
96
        }
97
    }
98
}
99