Test Failed
Push — master ( f562c0...ed7d3a )
by Dominik
02:11
created

CourseSearchController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 13

Duplication

Lines 15
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
eloc 13
nc 1
nop 6
dl 15
loc 15
rs 9.4285
c 0
b 0
f 0
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 Chubbyphp\Validation\ValidatorInterface;
11
use Psr\Http\Message\ServerRequestInterface as Request;
12
use Psr\Http\Message\ResponseInterface as Response;
13
use Chubbyphp\ApiSkeleton\Error\Error;
14
use Chubbyphp\ApiSkeleton\Error\ErrorManager;
15
use Chubbyphp\ApiSkeleton\Model\Course;
16
use Chubbyphp\ApiSkeleton\Search\CourseSearch;
17
18
final class CourseSearchController
19
{
20
    /**
21
     * @var string
22
     */
23
    private $defaultLanguage;
24
25
    /**
26
     * @var ErrorManager
27
     */
28
    private $errorManager;
29
30
    /**
31
     * @var RepositoryInterface
32
     */
33
    private $repository;
34
35
    /**
36
     * @var RequestManagerInterface
37
     */
38
    private $requestManager;
39
40
    /**
41
     * @var ResponseManagerInterface
42
     */
43
    private $responseManager;
44
45
    /**
46
     * @var ValidatorInterface
47
     */
48
    private $validator;
49
50
    /**
51
     * @param string                   $defaultLanguage
52
     * @param ErrorManager             $errorManager
53
     * @param RepositoryInterface      $repository
54
     * @param RequestManagerInterface  $requestManager
55
     * @param ResponseManagerInterface $responseManager
56
     * @param ValidatorInterface       $validator
57
     */
58 View Code Duplication
    public function __construct(
0 ignored issues
show
Duplication introduced by
This method 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...
59
        string $defaultLanguage,
60
        ErrorManager $errorManager,
61
        RepositoryInterface $repository,
62
        RequestManagerInterface $requestManager,
63
        ResponseManagerInterface $responseManager,
64
        ValidatorInterface $validator
65
    ) {
66
        $this->defaultLanguage = $defaultLanguage;
67
        $this->errorManager = $errorManager;
68
        $this->repository = $repository;
69
        $this->requestManager = $requestManager;
70
        $this->responseManager = $responseManager;
71
        $this->validator = $validator;
72
    }
73
74
    /**
75
     * @param Request $request
76
     *
77
     * @return Response
78
     */
79
    public function __invoke(Request $request): Response
80
    {
81
        /** @var CourseSearch $courseSearch */
82
        $courseSearch = $this->requestManager->getDataFromRequestQuery($request, CourseSearch::class);
83
84 View Code Duplication
        if ([] !== $errors = $this->validator->validateObject($courseSearch)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
85
            return $this->responseManager->createResponse(
86
                $request,
87
                400,
88
                $this->errorManager->createByValidationErrors(
89
                    $errors,
90
                    $this->requestManager->getAcceptLanguage($request, $this->defaultLanguage),
91
                    Error::SCOPE_BODY,
92
                    Course::class
93
                )
94
            );
95
        }
96
97
        /** @var CourseSearch $courseSearch */
98
        $courseSearch = $this->repository->search($courseSearch);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Chubbyphp\Model\RepositoryInterface as the method search() does only exist in the following implementations of said interface: Chubbyphp\ApiSkeleton\Repository\CourseRepository.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
99
100
        return $this->responseManager->createResponse($request, 200, $courseSearch);
101
    }
102
}
103