Passed
Branch feature/refactoring (13cbf0)
by Alexey
03:46
created

CategoriesScraper   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Test Coverage

Coverage 92.59%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
eloc 29
c 1
b 0
f 0
dl 0
loc 59
ccs 25
cts 27
cp 0.9259
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B __invoke() 0 47 9
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * @author   Ne-Lexa
7
 * @license  MIT
8
 *
9
 * @see      https://github.com/Ne-Lexa/google-play-scraper
10
 */
11
12
namespace Nelexa\GPlay\Scraper;
13
14
use Nelexa\GPlay\Exception\GooglePlayException;
15
use Nelexa\GPlay\Model\Category;
16
use Nelexa\GPlay\Util\ScraperUtil;
17
use Nelexa\HttpClient\ResponseHandlerInterface;
18
use Psr\Http\Message\RequestInterface;
19
use Psr\Http\Message\ResponseInterface;
20
21
/**
22
 * @internal
23
 */
24
class CategoriesScraper implements ResponseHandlerInterface
25
{
26
    private const CATEGORY_URL_PREFIX = '/store/apps/category/';
27
28
    /**
29
     * @param RequestInterface  $request
30
     * @param ResponseInterface $response
31
     *
32
     * @throws GooglePlayException
33
     *
34
     * @return mixed
35
     */
36 3
    public function __invoke(RequestInterface $request, ResponseInterface $response)
37
    {
38 3
        $scriptData = ScraperUtil::extractScriptData($response->getBody()->getContents());
39
40 3
        $dataCategories = null;
41
42 3
        foreach ($scriptData as $key => $data) {
43 3
            if (isset($data[0][1][0][3][0]) && \is_array($data[0][1][0][3][0])) {
44 3
                $dataCategories = $data;
45 3
                break;
46
            }
47
        }
48
49 3
        if ($dataCategories === null) {
50
            throw (new GooglePlayException('Failed to get the list of categories.'))
51
                ->setUrl($request->getUri()->__toString())
52
            ;
53
        }
54
55
        $parseCategories = static function (array $items) use (&$parseCategories): array {
56 3
            return array_reduce(
57 3
                $items,
58
                static function ($results, $item) use (&$parseCategories) {
59 3
                    if (\is_array($item)) {
60
                        if (
61 3
                            \count($item) === 6 &&
62 3
                            strpos($item[0], self::CATEGORY_URL_PREFIX) === 0 &&
63 3
                            strpos($item[0], '?age=') === false
64
                        ) {
65 3
                            $id = basename($item[0]);
66 3
                            $categoryName = $item[1];
67 3
                            $results[] = new Category(
68 3
                                $id,
69 3
                                $categoryName
70
                            );
71
                        } else {
72 3
                            $results = array_merge($results, $parseCategories($item));
73
                        }
74
                    }
75
76 3
                    return $results;
77 3
                },
78 3
                []
79
            );
80 3
        };
81
82 3
        return $parseCategories($dataCategories[0][1][0][3]);
83
    }
84
}
85