Completed
Pull Request — master (#13)
by
unknown
03:54
created

HalProvider::__construct()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 4
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 7
ccs 5
cts 5
cp 1
crap 3
rs 10
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 11
    public function __construct(array $config = [])
43
    {
44 11
        foreach (self::DEFAULT_PAGINATION_VALUE as $name => $value) {
45 11
            if (isset($config[$name])) {
46 11
                $value = $config[$name];
47
            }
48 11
            $this->paginationName[$name] = explode('.', $value);
49
        }
50 11
    }
51
52
    /**
53
     * @param array              $data
54
     * @param ResponseInterface  $response
55
     * @param ResponseDefinition $responseDefinition
56
     *
57
     * @return bool
58
     */
59 7
    public function supportPagination(array $data, ResponseInterface $response, ResponseDefinition $responseDefinition): bool
60
    {
61 7
        foreach ($this->paginationName as $key => $value) {
62 7
            if (null === $this->getValue($data, $value)) {
63 5
                return false;
64
            }
65
        }
66
67 2
        return isset($data['_embedded']['item']);
68
    }
69
70
    /**
71
     * @param array              $data
72
     * @param ResponseInterface  $response
73
     * @param ResponseDefinition $responseDefinition
74
     *
75
     * @return Pagination
76
     */
77 4
    public function getPagination(array &$data, ResponseInterface $response, ResponseDefinition $responseDefinition): Pagination
78
    {
79 4
        $paginationLinks = null;
0 ignored issues
show
Unused Code introduced by
The assignment to $paginationLinks is dead and can be removed.
Loading history...
80 4
        $links = $data['_links'];
81 4
        $paginationLinks = new PaginationLinks(
82 4
            $links['first']['href'] ?? $links['self']['href'],
83 4
            $links['last']['href'] ?? $links['self']['href'],
84 4
            $links['next']['href'] ?? null,
85 4
            $links['prev']['href'] ?? null
86
        );
87
        $pagination = [
88 4
            'page' => $this->getValue($data, $this->paginationName['page']),
89 4
            'perPage' => $this->getValue($data, $this->paginationName['perPage']),
90 4
            'totalItems' => $this->getValue($data, $this->paginationName['totalItems']),
91
        ];
92
        $data = array_map(function ($data) {
93
            $relations = array_map(function ($item) {
94 4
                unset($item['_links']);
95
96 4
                return $item;
97 4
            }, $data['_embedded'] ?? []);
98 4
            unset($data['_links'], $data['_embedded']);
99
100 4
            return array_merge($relations, $data);
101 4
        }, $data['_embedded']['item']);
102
103 4
        return new Pagination(
104 4
            $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

104
            /** @scrutinizer ignore-type */ $pagination['page'],
Loading history...
105 4
            $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

105
            /** @scrutinizer ignore-type */ $pagination['perPage'],
Loading history...
106 4
            $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

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