Completed
Push — master ( 11b317...37df4d )
by Lucas
09:27
created

SwaggerController::swaggerAction()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 19
ccs 0
cts 17
cp 0
rs 9.4285
c 1
b 0
f 0
cc 3
eloc 10
nc 3
nop 0
crap 12
1
<?php
2
/**
3
 * controller for rendering our swagger spec
4
 */
5
6
namespace Graviton\SwaggerBundle\Controller;
7
8
use Symfony\Component\Finder\Finder;
9
use Symfony\Component\HttpKernel\Exception\FatalErrorException;
10
use Symfony\Component\HttpFoundation\Response;
11
12
/**
13
 * SwaggerController
14
 *
15
 * @author   List of contributors <https://github.com/libgraviton/graviton/graphs/contributors>
16
 * @license  http://opensource.org/licenses/gpl-license.php GNU Public License
17
 * @link     http://swisscom.ch
18
 */
19
class SwaggerController
20
{
21
    /*
22
     * @var Finder
23
     */
24
    private $finder;
25
26
    /**
27
     * @var string
28
     */
29
    private $rootDir;
30
31
    /**
32
     * @param Finder $finder  symfony/finder instance
33
     * @param string $rootDir symfomy root dir
34
     */
35
    public function __construct(Finder $finder, $rootDir)
36
    {
37
        $this->finder = $finder;
38
        $this->rootDir = $rootDir;
39
    }
40
41
    /**
42
     * @return JsonResponse Response with result or error
0 ignored issues
show
Documentation introduced by
Should the return type not be Response|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
43
     */
44
    public function swaggerAction()
45
    {
46
        $this->finder->files()->in($this->rootDir . '/cache')->name('swagger.json');
47
48
        if ($this->finder->count() != 1) {
49
            throw new FatalErrorException('Failed to find a generated swagger file');
0 ignored issues
show
Deprecated Code introduced by
The class Symfony\Component\HttpKe...ion\FatalErrorException has been deprecated with message: Deprecated in 2.3, to be removed in 3.0. Use the same class from the Debug component instead.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
50
        }
51
52
        foreach ($this->finder as $file) {
53
            return new Response(
54
                $file->getContents(),
55
                Response::HTTP_OK,
56
                [
57
                    'Content-Type' => 'application/json'
58
                ],
59
                false
0 ignored issues
show
Unused Code introduced by
The call to Response::__construct() has too many arguments starting with false.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
60
            );
61
        }
62
    }
63
}
64