Completed
Pull Request — master (#602)
by Tom
09:02
created

YumlController   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 3
c 0
b 0
f 0
lcom 1
cbo 5
dl 0
loc 40
ccs 12
cts 12
cp 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace DoctrineORMModule\Yuml;
6
7
use Laminas\Http\Client;
8
use Laminas\Http\Request;
9
use Laminas\Http\Response;
10
use Laminas\Mvc\Controller\AbstractActionController;
11
use Laminas\Mvc\Controller\Plugin\Redirect;
12
use UnexpectedValueException;
13
use function assert;
14
15
/**
16
 * Utility to generate Yuml compatible strings from metadata graphs
17
 *
18
 * @link    http://www.doctrine-project.org/
19
 */
20
class YumlController extends AbstractActionController
21
{
22
    protected Client $httpClient;
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_STRING, expecting T_FUNCTION or T_CONST
Loading history...
23
24
    public function __construct(Client $httpClient)
25
    {
26 1
        $this->httpClient = $httpClient;
27
    }
28 1
29 1
    /**
30
     * Redirects the user to a YUML graph drawn with the provided `dsl_text`
31
     *
32
     * @throws UnexpectedValueException if the YUML service answered incorrectly
33
     */
34
    public function indexAction() : Response
35
    {
36
        $request = $this->getRequest();
37
        assert($request instanceof Request);
38 2
        $this->httpClient->setMethod(Request::METHOD_POST);
39
        $this->httpClient->setParameterPost(['dsl_text' => $request->getPost('dsl_text')]);
40
        $response = $this->httpClient->send();
41 2
42 2
        if (! $response->isSuccess()) {
43 2
            throw new UnexpectedValueException('HTTP Request failed');
44 2
        }
45
46 2
        $redirect = $this->plugin('redirect');
47 1
        assert($redirect instanceof Redirect);
48
49
        return $redirect->toUrl('https://yuml.me/' . $response->getBody());
50
    }
51
}
52