Completed
Push — master ( 83fca8...0d9c6e )
by Andrii
10:43
created

src/middlewares/JsonApiMiddleware.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
4
namespace hiapi\middlewares;
5
6
use hiapi\commands\BaseCommand;
7
use hiapi\commands\error\CommandError;
8
use hiapi\commands\SearchCommand;
9
use hiapi\jsonApi\SearchCountDocument;
10
use League\Tactician\Middleware;
11
use Psr\Container\ContainerInterface;
12
use WoohooLabs\Yin\JsonApi\Document\AbstractSuccessfulDocument;
13
use WoohooLabs\Yin\JsonApi\Document\ErrorDocument;
14
use WoohooLabs\Yin\JsonApi\JsonApi;
15
use WoohooLabs\Yin\JsonApi\Schema\Error;
16
17
/**
18
 * Class JsonApiMiddleware
19
 *
20
 * @author Dmytro Naumenko <[email protected]>
21
 */
22
class JsonApiMiddleware implements JsonApiMiddlewareInterface, Middleware
23
{
24
    /**
25
     * @var array
26
     */
27
    private $commandToDocumentMap = [];
28
    /**
29
     * @var ContainerInterface
30
     */
31
    private $di;
32
    /**
33
     * @var JsonApi
34
     */
35
    private $jsonApi;
36
37
    /**
38
     * JsonApiMiddleware constructor.
39
     *
40
     * @param array $commandToDocumentMap
41
     * @param ContainerInterface $di
42
     * @param JsonApi $jsonApi
43
     */
44
    public function __construct(array $commandToDocumentMap, ContainerInterface $di, JsonApi $jsonApi)
45
    {
46
        $this->commandToDocumentMap = $commandToDocumentMap;
47
        $this->di = $di;
48
        $this->jsonApi = $jsonApi;
49
    }
50
51
    public function execute($command, callable $next)
52
    {
53
        $response = $this->jsonApi->respond();
54
        $result = $next($command);
55
56
        if ($result instanceof CommandError) {
57
            return $response->genericError(new ErrorDocument(), [
58
                Error::create()
59
                    ->setTitle($result->getException()->getMessage())
60
                    ->setMeta($result->getCommand()->getAttributes())
61
                ,
62
            ], $result->getStatusCode());
63
        }
64
65
        return $response->ok($this->getSuccessDocumentFor($command), $result);
66
    }
67
68
    /**
69
     * @param BaseCommand $command
70
     * @return AbstractSuccessfulDocument|\WoohooLabs\Yin\JsonApi\Schema\Document\AbstractSuccessfulDocument
71
     */
72
    public function getSuccessDocumentFor($command)
73
    {
74
        $className = get_class($command);
75
        if (!isset($this->commandToDocumentMap[$className])) {
76
            throw new \OutOfRangeException('Document map for "' . $className . "' does not exist");
77
        }
78
        if ($command instanceof SearchCommand && $command->count) {
0 ignored issues
show
The class hiapi\commands\SearchCommand 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...
79
            return $this->di->get(SearchCountDocument::class);
80
        }
81
82
        return $this->di->get($this->commandToDocumentMap[$className]);
83
    }
84
}
85