Completed
Push — master ( 9a342a...a162ae )
by Andrii
13:06
created

RunEndpointBusMiddleware::transformResult()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 9.6666
c 0
b 0
f 0
cc 4
nc 4
nop 1
1
<?php
2
3
namespace hiapi\Core\Http\Psr15\Middleware;
4
5
use Doctrine\Common\Collections\ArrayCollection;
6
use hiapi\Core\Endpoint\Endpoint;
7
use hiapi\Core\Endpoint\EndpointProcessor;
8
use hiapi\Core\Http\Psr7\Response\FatResponse;
9
use Psr\Http\Message\ResponseInterface;
10
use Psr\Http\Message\ServerRequestInterface;
11
use Psr\Http\Server\MiddlewareInterface;
12
use Psr\Http\Server\RequestHandlerInterface;
13
use yii\base\Arrayable;
14
use yii\base\Model;
15
16
class RunEndpointBusMiddleware implements MiddlewareInterface
17
{
18
    /**
19
     * @var EndpointProcessor
20
     */
21
    private $endpointProcessor;
22
23
    public function __construct(EndpointProcessor $endpointProcessor)
24
    {
25
        $this->endpointProcessor = $endpointProcessor;
26
    }
27
28
    /**
29
     * @inheritDoc
30
     */
31
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
32
    {
33
        /** @var Model $command */
34
        $command = $request->getAttribute(CommandForEndpointMiddleware::class);
35
        /** @var Endpoint $endpoint */
36
        $endpoint = $request->getAttribute(ResolveEndpointMiddleware::class);
37
38
        $result = $this->endpointProcessor->__invoke($command, $endpoint);
39
40
        $transformedResult = $this->transformResult($result);
41
        return FatResponse::create($transformedResult, $request);
42
    }
43
44
    /**
45
     * TODO: Re-implement as a ResourceTransformer, that can serialize Result to an Http\Response
46
     * TODO: It should be useful to serialize file to StreamResponse, or search result to Reponse with
47
     * TODO: an appropriate headers.
48
     *
49
     * @param array|ArrayCollection|object|mixed $result
50
     * @return array|string|null|boolean
51
     */
52
    private function transformResult($result)
53
    {
54
        if ($result instanceof ResponseInterface) {
55
            return $result;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $result; (Psr\Http\Message\ResponseInterface) is incompatible with the return type documented by hiapi\Core\Http\Psr15\Mi...leware::transformResult of type array|string|null|boolean.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
56
        }
57
58
        if ($result instanceof ArrayCollection) {
59
            return array_map(function ($item) {
60
                return $this->transformResult($item);
61
            }, $result->toArray());
62
        }
63
64
        if ($result instanceof Arrayable) {
0 ignored issues
show
Bug introduced by
The class yii\base\Arrayable does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
65
            return $result->toArray();
66
        }
67
68
        return $result;
69
    }
70
}
71