Completed
Push — master ( c34cac...3c68a7 )
by Pieter
04:47
created

StatusCheckRetriever::iterate()   B

Complexity

Conditions 8
Paths 11

Size

Total Lines 22
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 16
c 0
b 0
f 0
nc 11
nop 0
dl 0
loc 22
rs 8.4444
1
<?php
2
3
namespace W2w\Lib\Apie\Plugins\StatusCheck\DataLayers;
4
5
use CallbackFilterIterator;
6
use Generator;
7
use LimitIterator;
8
use RewindableGenerator;
9
use W2w\Lib\Apie\Core\Models\ApiResourceClassMetadata;
10
use W2w\Lib\Apie\Core\SearchFilters\PhpPrimitive;
11
use W2w\Lib\Apie\Core\SearchFilters\SearchFilter;
12
use W2w\Lib\Apie\Core\SearchFilters\SearchFilterRequest;
13
use W2w\Lib\Apie\Exceptions\InvalidClassTypeException;
14
use W2w\Lib\Apie\Exceptions\ResourceNotFoundException;
15
use W2w\Lib\Apie\Interfaces\ApiResourceRetrieverInterface;
16
use W2w\Lib\Apie\Interfaces\SearchFilterProviderInterface;
17
use W2w\Lib\Apie\Plugins\StatusCheck\ApiResources\Status;
18
use W2w\Lib\Apie\Plugins\StatusCheck\StatusChecks\StatusCheckInterface;
19
use W2w\Lib\Apie\Plugins\StatusCheck\StatusChecks\StatusCheckListInterface;
20
21
/**
22
 * Status check retriever retrieves instances of Status. A status check needs to implement StatusCheckInterface
23
 * or StatusCheckListInterface and sent in the constructor of this method.
24
 */
25
class StatusCheckRetriever implements ApiResourceRetrieverInterface, SearchFilterProviderInterface
26
{
27
    private $statusChecks;
28
29
    /**
30
     * @param (StatusCheckInterface|StatusCheckListInterface)[] $statusChecks
31
     */
32
    public function __construct(iterable $statusChecks)
33
    {
34
        $this->statusChecks = $statusChecks;
35
    }
36
37
    /**
38
     * Iterates over all status checks and creates a generator for it.
39
     *
40
     * @return Generator
41
     */
42
    private function iterate(): Generator
43
    {
44
        foreach ($this->statusChecks as $statusCheck) {
45
            $check = false;
46
            if ($statusCheck instanceof StatusCheckInterface) {
47
                $check = true;
48
                yield $statusCheck->getStatus();
49
            }
50
            if ($statusCheck instanceof StatusCheckListInterface) {
51
                $check = true;
52
                foreach ($statusCheck as $check) {
53
                    if ($check instanceof Status) {
54
                        yield $check;
55
                    } else if ($check instanceof StatusCheckInterface) {
56
                        yield $check->getStatus();
57
                    } else {
58
                        throw new InvalidClassTypeException(get_class($check), 'StatusCheckInterface or Status');
59
                    }
60
                }
61
            }
62
            if (!$check) {
63
                throw new InvalidClassTypeException(get_class($statusCheck), 'StatusCheckInterface or StatusCheckListInterface');
64
            }
65
        }
66
    }
67
68
    /**
69
     * Finds the correct status check or throw a 404 if it could not be found.
70
     *
71
     * @param string $resourceClass
72
     * @param mixed $id
73
     * @param array $context
74
     * @return Status
75
     */
76
    public function retrieve(string $resourceClass, $id, array $context)
77
    {
78
        foreach ($this->iterate() as $statusCheck) {
79
            if ($statusCheck->getId() === $id) {
80
                return $statusCheck;
81
            }
82
        }
83
        throw new ResourceNotFoundException($id);
84
    }
85
86
    /**
87
     * Return all status check results.
88
     *
89
     * @param string $resourceClass
90
     * @param array $context
91
     * @param SearchFilterRequest $searchFilterRequest
92
     * @return iterable<Status>
93
     */
94
    public function retrieveAll(string $resourceClass, array $context, SearchFilterRequest $searchFilterRequest): iterable
95
    {
96
        $offset = $searchFilterRequest->getOffset();
97
        $numberOfItems = $searchFilterRequest->getNumberOfItems();
98
        $filter = (array_key_exists('status', $searchFilterRequest->getSearches()))
99
            ? function (Status $status) use ($searchFilterRequest) { return $status->getStatus() === $searchFilterRequest->getSearches()['status']; }
100
            : function () { return true; };
101
        return new LimitIterator(
102
            new CallbackFilterIterator(
103
                new RewindableGenerator(function () {
104
                    return $this->iterate();
105
                }),
106
                $filter
107
            ),
108
            $offset,
109
            $numberOfItems
110
        );
111
    }
112
113
    /**
114
     * Retrieves search filter for an api resource.
115
     *
116
     * @param ApiResourceClassMetadata $classMetadata
117
     * @return SearchFilter
118
     */
119
    public function getSearchFilter(ApiResourceClassMetadata $classMetadata): SearchFilter
120
    {
121
        $res = new SearchFilter();
122
        $res->addPrimitiveSearchFilter('status', PhpPrimitive::STRING);
123
        return $res;
124
    }
125
}
126