Issues (7)

src/Service/Hateoas/Util.php (2 issues)

Labels
Severity
1
<?php
2
3
namespace Phprest\Service\Hateoas;
4
5
use Hateoas\Hateoas;
6
use JMS\Serializer\DeserializationContext;
7
use JMS\Serializer\SerializationContext;
8
use Negotiation\FormatNegotiator;
9
use Phprest\Exception;
10
use Phprest\Util\Mime;
11
use Symfony\Component\HttpFoundation\Request;
12
use Symfony\Component\HttpFoundation\Response;
13
14
trait Util
15
{
16
    use Mime;
17
18
    /**
19
     * @param mixed $value
20
     * @param Request $request
21
     * @param Response $response
22
     *
23
     * @throws Exception\NotAcceptable
24
     *
25
     * @return Response
26
     */
27 8
    protected function serialize($value, Request $request, Response $response)
28
    {
29 8
        $mimeProcResult = $this->processMime(
30 8
            (new FormatNegotiator())->getBest($request->headers->get('Accept', '*/*'))->getValue()
31
        );
32
33 8
        if ($mimeProcResult->mime === '*/*') {
34 4
            $mimeProcResult->mime = 'application/vnd.' . $mimeProcResult->vendor .
35 4
                '+json; version=' . $mimeProcResult->apiVersion;
36 4
            $mimeProcResult->format = 'json';
37
        }
38
39 8
        if (in_array($mimeProcResult->format, ['json', 'xml'])) {
40 6
            $response->setContent(
41 6
                $this->serviceHateoas()->serialize(
42 6
                    $value,
43 6
                    $mimeProcResult->format,
44 6
                    SerializationContext::create()->setVersion($mimeProcResult->apiVersion)
0 ignored issues
show
It seems like $mimeProcResult->apiVersion can also be of type string; however, parameter $version of JMS\Serializer\Context::setVersion() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

44
                    SerializationContext::create()->setVersion(/** @scrutinizer ignore-type */ $mimeProcResult->apiVersion)
Loading history...
45
                )
46
            );
47
48 6
            $response->headers->set('Content-Type', $mimeProcResult->mime);
49
50 6
            return $response;
51
        }
52
53 2
        throw new Exception\NotAcceptable(0, [$mimeProcResult->mime . ' is not supported']);
54
    }
55
56
    /**
57
     * @param string $type
58
     * @param Request $request
59
     *
60
     * @throws Exception\UnsupportedMediaType
61
     *
62
     * @return mixed
63
     */
64 2
    protected function deserialize($type, Request $request)
65
    {
66 2
        $mimeProcResult = $this->processMime($request->headers->get('Content-Type'));
67
68 2
        if (is_null($mimeProcResult->format)) {
69 1
            throw new Exception\UnsupportedMediaType();
70
        }
71
72 1
        return $this->serviceHateoas()->getSerializer()->deserialize(
73 1
            $request->getContent(),
74
            $type,
75 1
            $mimeProcResult->format,
76 1
            DeserializationContext::create()->setVersion($mimeProcResult->apiVersion)
0 ignored issues
show
It seems like $mimeProcResult->apiVersion can also be of type string; however, parameter $version of JMS\Serializer\Context::setVersion() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

76
            DeserializationContext::create()->setVersion(/** @scrutinizer ignore-type */ $mimeProcResult->apiVersion)
Loading history...
77
        );
78
    }
79
80
    /**
81
     * @return Hateoas
82
     */
83
    abstract protected function serviceHateoas();
84
}
85