Passed
Pull Request — master (#13)
by
unknown
14:24
created

HalProvider   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 124
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 55
c 2
b 0
f 0
dl 0
loc 124
rs 10
wmc 17

4 Methods

Rating   Name   Duplication   Size   Complexity  
B getValue() 0 16 8
A getPagination() 0 32 1
A supportPagination() 0 14 5
A __construct() 0 7 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace ElevenLabs\Api\Service\Pagination\Provider;
6
7
use ElevenLabs\Api\Definition\ResponseDefinition;
8
use ElevenLabs\Api\Service\Pagination\Pagination;
9
use ElevenLabs\Api\Service\Pagination\PaginationLinks;
10
use Psr\Http\Message\ResponseInterface;
11
12
/**
13
 * Class HalProvider.
14
 */
15
class HalProvider implements PaginationProviderInterface
16
{
17
    /**
18
     * @var array
19
     */
20
    const DEFAULT_PAGINATION_VALUE = [
21
        // current page
22
        'page' => '_links.self.href.page',
23
24
        // number of items per page
25
        'perPage' => 'itemsPerPage',
26
27
        // number of items available in total
28
        'totalPages' => '_links.last.href.page',
29
30
        // number of pages available
31
        'totalItems' => 'totalItems',
32
    ];
33
34
    /**
35
     * @var array
36
     */
37
    private $paginationName;
38
39
    /**
40
     * @param array $config
41
     */
42
    public function __construct(array $config = [])
43
    {
44
        foreach (self::DEFAULT_PAGINATION_VALUE as $name => $value) {
45
            if (isset($config[$name])) {
46
                $value = $config[$name];
47
            }
48
            $this->paginationName[$name] = explode('.', $value);
49
        }
50
    }
51
52
    /**
53
     * @param array              $data
54
     * @param ResponseInterface  $response
55
     * @param ResponseDefinition $responseDefinition
56
     *
57
     * @return bool
58
     */
59
    public function supportPagination(array $data, ResponseInterface $response, ResponseDefinition $responseDefinition): bool
60
    {
61
        $totalItems = $this->getValue($data, $this->paginationName['totalItems']);
62
        $perPage = $this->getValue($data, $this->paginationName['perPage']);
63
        foreach ($this->paginationName as $key => $value) {
64
            if ('totalPages' === $key && $totalItems < $perPage) {
65
                continue;
66
            }
67
            if (null === $this->getValue($data, $value)) {
68
                return false;
69
            }
70
        }
71
72
        return isset($data['_embedded']['item']);
73
    }
74
75
    /**
76
     * @param array              $data
77
     * @param ResponseInterface  $response
78
     * @param ResponseDefinition $responseDefinition
79
     *
80
     * @return Pagination
81
     */
82
    public function getPagination(array &$data, ResponseInterface $response, ResponseDefinition $responseDefinition): Pagination
83
    {
84
        $paginationLinks = null;
0 ignored issues
show
Unused Code introduced by
The assignment to $paginationLinks is dead and can be removed.
Loading history...
85
        $links = $data['_links'];
86
        $paginationLinks = new PaginationLinks(
87
            $links['first']['href'] ?? $links['self']['href'],
88
            $links['last']['href'] ?? $links['self']['href'],
89
            $links['next']['href'] ?? null,
90
            $links['prev']['href'] ?? null
91
        );
92
        $pagination = [
93
            'page' => $this->getValue($data, $this->paginationName['page']),
94
            'perPage' => $this->getValue($data, $this->paginationName['perPage']),
95
            'totalItems' => $this->getValue($data, $this->paginationName['totalItems']),
96
        ];
97
        $data = array_map(function ($data) {
98
            $relations = array_map(function ($item) {
99
                unset($item['_links']);
100
101
                return $item;
102
            }, $data['_embedded'] ?? []);
103
            unset($data['_links'], $data['_embedded']);
104
105
            return array_merge($relations, $data);
106
        }, $data['_embedded']['item']);
107
108
        return new Pagination(
109
            $pagination['page'],
0 ignored issues
show
Bug introduced by
It seems like $pagination['page'] can also be of type array and null; however, parameter $page of ElevenLabs\Api\Service\P...gination::__construct() 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

109
            /** @scrutinizer ignore-type */ $pagination['page'],
Loading history...
110
            $pagination['perPage'],
0 ignored issues
show
Bug introduced by
It seems like $pagination['perPage'] can also be of type array and null; however, parameter $perPage of ElevenLabs\Api\Service\P...gination::__construct() 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

110
            /** @scrutinizer ignore-type */ $pagination['perPage'],
Loading history...
111
            $pagination['totalItems'],
0 ignored issues
show
Bug introduced by
It seems like $pagination['totalItems'] can also be of type array and null; however, parameter $totalItems of ElevenLabs\Api\Service\P...gination::__construct() 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

111
            /** @scrutinizer ignore-type */ $pagination['totalItems'],
Loading history...
112
            (int) ceil($pagination['totalItems'] / $pagination['perPage']),
113
            $paginationLinks
114
        );
115
    }
116
117
    /**
118
     * @param array $items
119
     * @param array $values
120
     *
121
     * @return array|int|mixed|null
122
     */
123
    private function getValue(array $items, array $values)
124
    {
125
        $value = $items;
126
        foreach ($values as $item) {
127
            if (isset($value[$item])) {
128
                $value = $value[$item];
129
            } elseif (is_string($value) && 1 === preg_match('#'.$item.'=([^&]*)#', $value, $tab)) {
130
                $value = isset($tab[1]) ? (int) $tab[1] : null;
131
            } elseif (is_string($value) && 0 === preg_match('#'.$item.'=([^&]*)#', $value)) {
132
                return 1;
133
            } else {
134
                return null;
135
            }
136
        }
137
138
        return $value;
139
    }
140
}
141