Completed
Branch v4 (4e54dd)
by Pieter
03:26
created

ApiResourceListFacadeResponse   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 12
dl 0
loc 46
rs 10
c 1
b 0
f 1
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getResponse() 0 10 2
A __construct() 0 11 1
1
<?php
2
3
namespace W2w\Lib\Apie\Core\Models;
4
5
use Psr\Http\Message\ResponseInterface;
6
use W2w\Lib\Apie\Core\SearchFilters\SearchFilterRequest;
7
use W2w\Lib\Apie\Events\ResponseAllEvent;
8
use W2w\Lib\Apie\Interfaces\ResourceSerializerInterface;
9
use W2w\Lib\Apie\PluginInterfaces\ResourceLifeCycleInterface;
10
11
/**
12
 * Data class returned by ApiResourceFacade for the GET resources.
13
 */
14
class ApiResourceListFacadeResponse extends ApiResourceFacadeResponse
15
{
16
    /**
17
     * @var string
18
     */
19
    private $resourceClass;
20
21
    /**
22
     * @var SearchFilterRequest
23
     */
24
    private $filterRequest;
25
26
    /**
27
     * @param ResourceSerializerInterface $serializer
28
     * @param mixed $resourceList
29
     * @param string $resourceClass
30
     * @param SearchFilterRequest $filterRequest
31
     * @param string|null $acceptHeader
32
     * @param ResourceLifeCycleInterface[]
33
     */
34
    public function __construct(
35
        ResourceSerializerInterface $serializer,
36
        iterable $resourceList,
37
        string $resourceClass,
38
        SearchFilterRequest $filterRequest,
39
        ?string $acceptHeader,
40
        iterable $resourceLifeCycles = []
41
    ) {
42
        parent::__construct($serializer, $resourceList, $acceptHeader, $resourceLifeCycles);
43
        $this->resourceClass = $resourceClass;
44
        $this->filterRequest = $filterRequest;
45
    }
46
47
    /**
48
     * @return ResponseInterface
49
     */
50
    public function getResponse(): ResponseInterface
51
    {
52
        $event = new ResponseAllEvent($this->resourceClass, $this->filterRequest, $this->getResource(), $this->getAcceptHeader());
53
        $this->runLifeCycleEvent('onPreCreateResponse', $event);
54
        if (!$event->getResponse()) {
55
            $event->setResponse($this->getSerializer()->toResponse($this->getResource(), $this->getAcceptHeader()));
56
        }
57
        $this->runLifeCycleEvent('onPostCreateResponse', $event);
58
59
        return $event->getResponse();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $event->getResponse() could return the type null which is incompatible with the type-hinted return Psr\Http\Message\ResponseInterface. Consider adding an additional type-check to rule them out.
Loading history...
60
    }
61
}
62