Failed Conditions
Push — future/ConfuenceWrapper ( 28d061...f8e71e )
by
unknown
22:28
created

Confluence::createNewPage()   A

Complexity

Conditions 4
Paths 9

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 21
rs 9.0534
cc 4
eloc 12
nc 9
nop 4
1
<?php
2
3
namespace CodeMine\ConfluenceImporter\Service;
4
5
use CodeMine\ConfluenceImporter\Documentation\PageInterface;
6
use CodeMine\ConfluenceImporter\Service\Confluence\InstanceInterface;
7
use GuzzleHttp\ClientInterface;
8
use GuzzleHttp\Exception\ClientException;
9
use GuzzleHttp\Psr7\Request;
10
use GuzzleHttp\Psr7\Stream;
11
12
/**
13
 * Class Confluence
14
 *
15
 * @package CodeMine\ConfluenceImporter\Service
16
 */
17
class Confluence
18
{
19
    const ADD_PAGE = 'rest/api/content';
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 4 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
20
    const SEARCH_PAGE = 'rest/api/content/search';
21
22
    /**
23
     * @var \GuzzleHttp\ClientInterface
24
     */
25
    private $client;
26
    /**
27
     * @var \CodeMine\ConfluenceImporter\Service\Confluence\InstanceInterface
28
     */
29
    private $confluenceInstance;
30
31
    /**
32
     * Confluence constructor.
33
     *
34
     * blabdlabsdaldbalsbdals
35
     * aldnalsdlasdn
36
     * alndlasndsdijfpgaijfawl
37
     *
38
     * @param \GuzzleHttp\ClientInterface $client
39
     * @param \CodeMine\ConfluenceImporter\Service\Confluence\InstanceInterface $confluenceInstance
40
     */
41
    public function __construct(ClientInterface $client, InstanceInterface $confluenceInstance)
42
    {
43
        $this->client = $client;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 13 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
44
        $this->confluenceInstance = $confluenceInstance;
45
    }
46
47
    /**
48
     * @param $key
49
     * @param PageInterface $page
50
     * @param PageInterface|NULL $parentPage
51
     * @param null $id
52
     *
53
     * @docblock bla bla bla bla bla bla bla
54
     */
55
    public function createNewPage($key, PageInterface $page, PageInterface $parentPage = NULL, $id = NULL)
56
    {
57
        $body = $this->getBody($key, $page, $parentPage, $id);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 4 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
58
        $headers = $this->getHeaders();
59
60
        try {
61
            $request = $this->getRequest($headers, $body);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 6 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
62
            $response = $this->client->send($request); //TODO: find solution for exception on same page name
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 5 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
63
            $pageChildren = $page->children();
64
65
            $newPageId = json_decode($response->getBody()->getContents())->id;
66
67
            if (null !== $pageChildren) {
68
                foreach ($pageChildren as $pageChild) {
69
                    $this->createNewPage($key, $pageChild, $page, $newPageId);
70
                }
71
            }
72
        } catch (ClientException $e) {
73
            //silent
74
        }
75
    }
76
77
    public function getPageId($key, PageInterface $page) //TODO::Change for private method
78
    {
79
        $headers = [
80
            'Authorization' => 'Basic ' . $this->getBase64Credentials(),
81
        ];
82
83
        try {
84
85
            $response = $this->client->request('GET', self::SEARCH_PAGE, [
86
                'headers' => $headers,
87
                'query' => [
88
                    'cql' => sprintf('title="%s" and space="%s"', $page->title(), $key)
89
90
                ],
91
                'debug' => TRUE
92
            ]);
93
94
        } catch (\Exception $e) {
95
            //silent
96
        }
97
98
        /** @var Stream $returnBody */
99
        $returnBody = $response->getBody();
100
101
        $stdBody = json_decode($returnBody->getContents());
102
        if ($response->getStatusCode() == 200 && count($stdBody->results) == 1) {
103
            $id = (string)$stdBody->results[0]->id;
104
105
            return $id;
106
107
        }
108
        throw new \Exception('ERROR!!!!');
109
    }
110
111
    /**
112
     * @return string
113
     */
114
    private function getBase64Credentials()
115
    {
116
        $base64Credentials = base64_encode(
117
            $this->confluenceInstance->username() .
118
            ':' .
119
            $this->confluenceInstance->password()
120
        );
121
122
        return $base64Credentials;
123
    }
124
125
    /**
126
     * @return array
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use array<string,string>.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
127
     */
128
    private function getHeaders()
129
    {
130
        $headers = [
131
            'Authorization' => 'Basic ' . $this->getBase64Credentials(),
132
            'Content-Type' => 'application/json'
133
        ];
134
135
        return $headers;
136
    }
137
138
    /**
139
     * @param string $key
140
     * @param PageInterface $page
141
     * @param PageInterface|NULL $parentPage
142
     * @return string<json>
0 ignored issues
show
Documentation introduced by
The doc-type string<json> could not be parsed: Expected "|" or "end of type", but got "<" at position 6. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
143
     * @throws \Exception
144
     */
145
    private function getBody($key, PageInterface $page, PageInterface $parentPage = NULL, $id = NULL)
146
    {
147
        $bodyArray = [
148
            'type' => 'page',
149
            'title' => $page->title(),
150
            'space' => [
151
                'key' => $key
152
            ],
153
            'body' => [
154
                'storage' => [
155
                    'value' => $page->content(),
156
                    'representation' => 'storage'
157
                ]
158
            ]
159
        ];
160
161
        if (isset($parentPage)) {
162
            if (isset($id)){
163
                $bodyArray['ancestors'] = [
164
                    ['id' => $id]
165
                ];
166
            }else {
167
                $pageId = $this->getPageId($key, $parentPage);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 17 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
168
                $bodyArray['ancestors'] = [
169
                    ['id' => $pageId]
170
                ];
171
            }
172
        }
173
174
        $body = json_encode($bodyArray);
175
176
        return $body;
177
    }
178
179
    /**
180
     * @param $headers
181
     * @param $body
182
     * @return Request
183
     */
184
    private function getRequest($headers, $body)
185
    {
186
187
        $request = new Request('POST', Confluence::ADD_PAGE, $headers, $body);
0 ignored issues
show
Coding Style introduced by
As per coding style, self should be used for accessing local static members.

This check looks for accesses to local static members using the fully qualified name instead of self::.

<?php

class Certificate {
    const TRIPLEDES_CBC = 'ASDFGHJKL';

    private $key;

    public function __construct()
    {
        $this->key = Certificate::TRIPLEDES_CBC;
    }
}

While this is perfectly valid, the fully qualified name of Certificate::TRIPLEDES_CBC could just as well be replaced by self::TRIPLEDES_CBC. Referencing local members with self:: assured the access will still work when the class is renamed, makes it perfectly clear that the member is in fact local and will usually be shorter.

Loading history...
188
189
        return $request;
190
    }
191
}