These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | use Adlogix\ConfluenceClient\ClientBuilder; |
||
3 | use Adlogix\ConfluenceClient\Security\QueryParamAuthentication; |
||
4 | use Silex\Application; |
||
5 | use Symfony\Component\HttpFoundation\Request; |
||
6 | use Symfony\Component\HttpFoundation\Response; |
||
7 | |||
8 | require_once 'vendor/autoload.php'; |
||
9 | |||
10 | |||
11 | /** |
||
12 | * See the 'installed' webhook on how to recover this payload. |
||
13 | * |
||
14 | * The sharedSecret is given by the application we installed the add-on to, |
||
15 | * this is needed to sign our request and to validate the requests from the application. |
||
16 | */ |
||
17 | $sharedSecret = ''; |
||
18 | $baseUrl = ''; |
||
19 | View Code Duplication | if (file_exists('payload.json')) { |
|
0 ignored issues
–
show
|
|||
20 | $payload = json_decode(file_get_contents('payload.json')); |
||
21 | $sharedSecret = $payload->sharedSecret; |
||
22 | $baseUrl = $payload->baseUrl . '/'; |
||
23 | } |
||
24 | |||
25 | |||
26 | $authenticationMethod = new QueryParamAuthentication('eu.adlogix.confluence-client', $sharedSecret); |
||
27 | $client = ClientBuilder::create($baseUrl, $authenticationMethod) |
||
28 | ->build(); |
||
29 | |||
30 | |||
31 | /** |
||
32 | * Since Confluence needs to reach our application to post some information, like the sharedSecret, we have to define some routes. |
||
33 | * At time of writing Confluence refuses to contact us if the route contains .php so we need to prettify our URLS. |
||
34 | * Our sample is not the best way to do it, but it's just for the demo. |
||
35 | */ |
||
36 | |||
37 | |||
38 | $app = new Application(); |
||
39 | $app['debug'] = true; |
||
40 | $app->register(new Silex\Provider\TwigServiceProvider(), array( |
||
41 | 'twig.path' => __DIR__ . '/views', |
||
42 | )); |
||
43 | |||
44 | /** |
||
45 | * Our sample descriptor is available at http://confluence-client.dev/descriptor.json |
||
46 | * |
||
47 | * This is the bare minimal descriptor to be defined. |
||
48 | * |
||
49 | * You can validate your descriptor |
||
50 | * @see https://atlassian-connect-validator.herokuapp.com/validate |
||
51 | */ |
||
52 | $app->get('/descriptor.json', function (Request $request) { |
||
53 | |||
54 | /* |
||
55 | * We have to construct the correct URL in order to confluence be able to contact us |
||
56 | * And the scheme MUST be https in order to confluence accept it. |
||
57 | */ |
||
58 | $host = $request->getHttpHost(); |
||
59 | $scheme = $request->getScheme(); |
||
60 | |||
61 | if (preg_match('/\.ngrok\.io/', $host)) { |
||
62 | $scheme = 'https'; |
||
63 | } |
||
64 | |||
65 | |||
66 | return json_encode([ |
||
67 | 'authentication' => [ |
||
68 | 'type' => 'jwt' |
||
69 | ], |
||
70 | 'baseUrl' => $scheme . '://' . $host, |
||
71 | 'scopes' => [ |
||
72 | 'read' |
||
73 | ], |
||
74 | 'key' => 'eu.adlogix.confluence-client', |
||
75 | 'lifecycle' => [ |
||
76 | 'installed' => '/installed', |
||
77 | 'enabled' => '/enabled' |
||
78 | ], |
||
79 | ]); |
||
80 | }); |
||
81 | |||
82 | /** |
||
83 | * When we install our add-on into any atlassian app, they will contact us at the URL we define in the 'installed' lifecycle. |
||
84 | * They will give us a payload containing the sharedSecret we'll need to use to sign our request. |
||
85 | * For the demo we just save the content to a file. |
||
86 | */ |
||
87 | $app->post('/installed', function (Request $request) { |
||
88 | |||
89 | $payload = $request->getContent(); |
||
90 | file_put_contents('payload.json', $payload); |
||
91 | |||
92 | /** |
||
93 | * Be sure to send a 200 OK response, or the app will tell you that your plugin can't be installed. |
||
94 | */ |
||
95 | return new Response('OK', 200); |
||
96 | }); |
||
97 | |||
98 | |||
99 | /** |
||
100 | * Even if the documentation tell's you the only needed webhook is the installed one, |
||
101 | * they won't let you enable the add-on unless you define the route to you 'enabled' webhook. |
||
102 | */ |
||
103 | $app->post('/enabled', function () { |
||
104 | /** |
||
105 | * Be sure to send a 200 OK response, or the app will tell you that your plugin can't be enabled. |
||
106 | */ |
||
107 | return new Response('OK', 200); |
||
108 | }); |
||
109 | |||
110 | //Catch all route to run our test code |
||
111 | $app->match('/api/{url}', function ($url) use ($client) { |
||
112 | $response = $client->sendRawApiRequest('GET', $url); |
||
113 | $content = $response->getBody()->getContents(); |
||
114 | return new Response($content, $response->getStatusCode()); |
||
115 | |||
116 | })->assert('url', '.+'); |
||
117 | |||
118 | $app->match('/image/{url}', function ($url) use ($client) { |
||
119 | $response = $client->downloadAttachment($url); |
||
120 | $content = $response->getBody()->getContents(); |
||
121 | return new Response($content, $response->getStatusCode(), $response->getHeaders()); |
||
122 | })->assert('url', '.+'); |
||
123 | |||
124 | $app->match('/', function (Application $app) { |
||
125 | return $app['twig']->render("index.html.twig"); |
||
126 | }); |
||
127 | |||
128 | $app->run(); |
||
129 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.