Character::extractIdsFromResponseBasedOnKey()   B
last analyzed

Complexity

Conditions 6
Paths 24

Size

Total Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 26
rs 8.8817
c 0
b 0
f 0
cc 6
nc 24
nop 2
1
<?php
2
3
namespace Zmaglica\RickAndMortyApiWrapper\Model;
4
5
class Character extends AbstractModel
6
{
7
    /**
8
     * Get all locations without running API call. Useful if you want to perform additional filtering.
9
     *
10
     * @param bool $removeDuplicates
11
     * @return array|\Zmaglica\RickAndMortyApiWrapper\Api\AbstractApi
12
     */
13
    public function locations($removeDuplicates = false)
14
    {
15
        $ids = $this->extractIdsFromResponseBasedOnKey('location', $removeDuplicates);
16
17
        return $this->api->location()->whereId($ids);
18
    }
19
20
    /**
21
     * Get all locations.
22
     * @param bool $removeDuplicates
23
     * @return mixed
24
     */
25
    public function getLocations($removeDuplicates = false)
26
    {
27
        return $this->locations($removeDuplicates)->get();
28
    }
29
30
    /**
31
     * Get all origin locations. Useful if you want to perform additional filtering.
32
     *
33
     * @param bool $removeDuplicates
34
     * @return array|\Zmaglica\RickAndMortyApiWrapper\Api\AbstractApi
35
     */
36
    public function origins($removeDuplicates = false)
37
    {
38
        $ids = $this->extractIdsFromResponseBasedOnKey('origin', $removeDuplicates);
39
40
        return $this->api->location()->whereId($ids);
41
    }
42
43
    /**
44
     * Get all origin locations.
45
     *
46
     * @param bool $removeDuplicates
47
     * @return mixed
48
     */
49
    public function getOrigins($removeDuplicates = false)
50
    {
51
        return $this->origins($removeDuplicates)->get();
52
    }
53
54
    /**
55
     * Get all episodes. Useful if you want to perform additional filtering.
56
     *
57
     * @param bool $removeDuplicates
58
     * @return \Zmaglica\RickAndMortyApiWrapper\Api\AbstractApi
59
     */
60
    public function episodes($removeDuplicates = false)
61
    {
62
        $ids = $this->extractIdsFromResponseBasedOnKey('episode', $removeDuplicates);
63
64
        return $this->api->episode()->whereId($ids);
65
    }
66
67
    /**
68
     * Get all episodes
69
     *
70
     * @param bool $removeDuplicates
71
     * @return mixed
72
     */
73
    public function getEpisodes($removeDuplicates = false)
74
    {
75
        return $this->episodes($removeDuplicates)->get();
76
    }
77
78
    /**
79
     * This helper function will extract all URL ids by key. Key can be location, residence or episode
80
     * @param $key
81
     * @param $removeDuplicates
82
     * @return array
83
     */
84
    private function extractIdsFromResponseBasedOnKey($key, $removeDuplicates)
0 ignored issues
show
Unused Code introduced by
The parameter $key is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
85
    {
86
        $ids = [];
87
        $urls = [];
88
        if ($this->info) {
89
            $urls = array_column($this->data['results'], 'location');
90
        } elseif (isset($this->data[0])) {
91
            $urls = array_column($this->data, 'location');
92
        } else {
93
            $urls[] = $this->data['location'];
94
        }
95
        $urls = array_column($urls, 'url');
96
        if ($removeDuplicates) {
97
            $urls = array_unique($urls);
98
        }
99
100
        foreach ($urls as $url) {
101
            $ids[] = substr(strrchr($url, '/'), 1);
102
            ;
103
        }
104
        if (!$ids) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $ids of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
105
            return [];
106
        }
107
108
        return $ids;
109
    }
110
}
111