UserRecommendationTransformer::process()   B
last analyzed

Complexity

Conditions 7
Paths 25

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 7

Importance

Changes 0
Metric Value
dl 0
loc 24
ccs 13
cts 13
cp 1
rs 8.6026
c 0
b 0
f 0
cc 7
nc 25
nop 1
crap 7
1
<?php
2
3
namespace App\Movies\Transformer;
4
5
use App\Transformer\Transformer;
6
7
class UserRecommendationTransformer implements Transformer
8
{
9
    private $hiddenFields = [];
10
    /**
11
     * [int movie id => int how many times this movie were recommended].
12
     *
13
     * @var array
14
     */
15
    private $idToTimesRecommendedMap = [];
16
17 2
    public static function list(array $ids): self
18
    {
19 2
        $transformer = new self();
20 2
        $transformer->hiddenFields = [
21
            'budget',
22
            'genres',
23
            'imdbId',
24
            'overview',
25
            'runtime',
26
            'tmdb.id',
27
            'tmdb.voteAverage',
28
            'tmdb.voteCount',
29
        ];
30
31 2
        foreach ($ids as $data) {
32 2
            $transformer->idToTimesRecommendedMap[reset($data)] = $data['rate'];
33
        }
34
35 2
        return $transformer;
36
    }
37
38 2
    public function process(array $data): array
39
    {
40 2
        if (!isset($data['id'])) {
41 2
            return $data;
42
        }
43
44 2
        $data['isWatched'] = isset($data['userWatchedMovie']) && $data['userWatchedMovie'] ? true : false;
45
46 2
        if (isset($data['tmdb.id'])) {
47 2
            $data['tmdb']['id'] = $data['tmdb.id'];
48 2
            $data['tmdb']['voteAverage'] = $data['tmdb.voteAverage'];
49 2
            $data['tmdb']['voteCount'] = $data['tmdb.voteCount'];
50
51 2
            $data['rate'] = $this->idToTimesRecommendedMap[$data['id']] ?? 0;
52
        }
53
54 2
        foreach ($this->hiddenFields as $hiddenField) {
55 2
            if (isset($data[$hiddenField])) {
56 2
                unset($data[$hiddenField]);
57
            }
58
        }
59
60 2
        return $data;
61
    }
62
}
63