Test Failed
Push — master ( 98997f...e65f7f )
by Dominik
02:18
created

app/Controller/Course/CourseReadController.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Chubbyphp\ApiSkeleton\Controller\Course;
6
7
use Chubbyphp\ApiHttp\Manager\RequestManagerInterface;
8
use Chubbyphp\ApiHttp\Manager\ResponseManagerInterface;
9
use Chubbyphp\Model\RepositoryInterface;
10
use Psr\Http\Message\ServerRequestInterface as Request;
11
use Psr\Http\Message\ResponseInterface as Response;
12
use Chubbyphp\ApiSkeleton\Model\Course;
13
14 View Code Duplication
final class CourseReadController
0 ignored issues
show
This class seems to be duplicated in your project.

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.

Loading history...
15
{
16
    /**
17
     * @var RepositoryInterface
18
     */
19
    private $repository;
20
21
    /**
22
     * @var RequestManagerInterface
23
     */
24
    private $requestManager;
25
26
    /**
27
     * @var ResponseManagerInterface
28
     */
29
    private $responseManager;
30
31
    /**
32
     * @param RepositoryInterface      $repository
33
     * @param RequestManagerInterface  $requestManager
34
     * @param ResponseManagerInterface $responseManager
35
     */
36
    public function __construct(
37
        RepositoryInterface $repository,
38
        RequestManagerInterface $requestManager,
39
        ResponseManagerInterface $responseManager
40
    ) {
41
        $this->repository = $repository;
42
        $this->requestManager = $requestManager;
43
        $this->responseManager = $responseManager;
44
    }
45
46
    /**
47
     * @param Request $request
48
     *
49
     * @return Response
50
     */
51
    public function __invoke(Request $request): Response
52
    {
53
        if (null === $accept = $this->requestManager->getAccept($request)) {
54
            return $this->responseManager->createAcceptNotSupportedResponse($request);
55
        }
56
57
        $id = $request->getAttribute('id');
58
59
        /** @var Course $course */
60
        if (null === $course = $this->repository->find($id)) {
61
            return $this->responseManager->createResourceNotFoundResponse($request, $accept, 'course', ['id' => $id]);
62
        }
63
64
        return $this->responseManager->createResponse($request, 200, $accept, $course);
65
    }
66
}
67