AnimeListTransformer   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 76
rs 10
c 0
b 0
f 0
wmc 9
lcom 0
cbo 1

2 Methods

Rating   Name   Duplication   Size   Complexity  
A transform() 0 16 2
C untransform() 0 42 7
1
<?php declare(strict_types=1);
2
/**
3
 * Anime List Client
4
 *
5
 * An API client for Kitsu and MyAnimeList to manage anime and manga watch lists
6
 *
7
 * PHP version 7
8
 *
9
 * @package     AnimeListClient
10
 * @author      Timothy J. Warren <[email protected]>
11
 * @copyright   2015 - 2017  Timothy J. Warren
12
 * @license     http://www.opensource.org/licenses/mit-license.html  MIT License
13
 * @version     4.0
14
 * @link        https://github.com/timw4mail/HummingBirdAnimeClient
15
 */
16
17
namespace Aviat\AnimeClient\API\MAL\Transformer;
18
19
use Aviat\AnimeClient\API\Kitsu\Enum\AnimeWatchingStatus;
20
use Aviat\Ion\Transformer\AbstractTransformer;
21
22
/**
23
 * Transformer for updating MAL List
24
 */
25
class AnimeListTransformer extends AbstractTransformer {
26
	
27
	const statusMap = [
0 ignored issues
show
Coding Style introduced by
This class constant is not uppercase (expected STATUSMAP).
Loading history...
28
		AnimeWatchingStatus::WATCHING => '1',
29
		AnimeWatchingStatus::COMPLETED => '2',
30
		AnimeWatchingStatus::ON_HOLD => '3',
31
		AnimeWatchingStatus::DROPPED => '4',
32
		AnimeWatchingStatus::PLAN_TO_WATCH => '6'
33
	];
34
35
	public function transform($item)
36
	{
37
		$rewatching = (array_key_exists('rewatching', $item) && $item['rewatching']);
38
39
		return [
40
			'id' => $item['mal_id'],
41
			'data' => [
42
				'status' => self::statusMap[$item['watching_status']],
43
				'rating' => $item['user_rating'],
44
				'rewatch_value' => (int) $rewatching,
45
				'times_rewatched' => $item['rewatched'],
46
				'comments' => $item['notes'],
47
				'episode' => $item['episodes_watched']
48
			]
49
		];
50
	}
51
	
52
	/**
53
	 * Transform Kitsu episode data to MAL episode data
54
	 *
55
	 * @param array $item
56
	 * @return array 
57
	 */
58
	public function untransform(array $item): array
59
	{
60
		$map = [
61
			'id' => $item['mal_id'],
62
			'data' => [
63
				'episode' => $item['data']['progress']
64
			]
65
		];
66
		
67
		$data =& $item['data'];
68
		
69
		foreach($item['data'] as $key => $value)
70
		{
71
			switch($key) 
72
			{
73
				case 'notes':
74
					$map['data']['comments'] = $value;
75
				break;
76
					
77
				case 'rating':
78
					$map['data']['score'] = $value * 2;
79
				break;
80
					
81
				case 'reconsuming':
82
					$map['data']['enable_rewatching'] = (bool) $value;
83
				break;
84
					
85
				case 'reconsumeCount':
86
					$map['data']['times_rewatched'] = $value;
87
				break;
88
					
89
				case 'status':
90
					$map['data']['status'] = self::statusMap[$value];
91
				break;
92
					
93
				default:
94
				break;
95
			}
96
		}
97
98
		return $map;
99
	}
100
}