SyncKitsuWithMal::diffLists()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 16
rs 9.4285
c 0
b 0
f 0
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\Command;
18
19
use Amp\Artax;
20
use Aviat\AnimeClient\API\Kitsu;
21
22
/**
23
 * Clears the API Cache
24
 */
25
class SyncKitsuWithMal extends BaseCommand {
26
27
	protected $kitsuModel;
28
	protected $malModel;
29
30
	/**
31
	 * Run the image conversion script
32
	 *
33
	 * @param array $args
34
	 * @param array $options
35
	 * @return void
36
	 * @throws \ConsoleKit\ConsoleException
37
	 */
38
	public function execute(array $args, array $options = [])
39
	{
40
		$this->setContainer($this->setupContainer());
41
		$this->setCache($this->container->get('cache'));
42
		$this->kitsuModel = $this->container->get('kitsu-model');
43
		$this->malModel = $this->container->get('mal-model');
44
45
		//$kitsuCount = $this->getKitsuAnimeListPageCount();
0 ignored issues
show
Unused Code Comprehensibility introduced by
60% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
46
		//$this->echoBox("List item count: {$kitsuCount}");
0 ignored issues
show
Unused Code Comprehensibility introduced by
86% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
47
		$this->MALItemCreate();
48
49
		//echo json_encode($this->getMALList(), \JSON_PRETTY_PRINT);
0 ignored issues
show
Unused Code Comprehensibility introduced by
65% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
50
	}
51
52
53
	public function getMALList()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
54
	{
55
		return $this->malModel->getFullList();
56
	}
57
58
	public function getKitsuAnimeListPageCount()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
59
	{
60
		$cacheItem = $this->cache->getItem(Kitsu::AUTH_TOKEN_CACHE_KEY);
61
62
		$query = http_build_query([
63
			'filter' => [
64
				'user_id' => $this->kitsuModel->getUserIdByUsername(),
65
				'media_type' => 'Anime'
66
			],
67
			// 'include' => 'anime,anime.genres,anime.mappings,anime.streamingLinks',
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
68
			'page' => [
69
				'limit' => 1
70
			],
71
			'sort' => '-updated_at'
72
		]);
73
		$request = (new Artax\Request)
74
			->setUri("https://kitsu.io/api/edge/library-entries?{$query}")
75
			->setProtocol('1.1')
76
			->setAllHeaders([
77
				'Accept' => 'application/vnd.api+json',
78
				'Content-Type' => 'application/vnd.api+json',
79
				'User-Agent' => "Tim's Anime Client/4.0"
80
			]);
81
82
		if ($cacheItem->isHit())
83
		{
84
			$token = $cacheItem->get();
85
			$request->setHeader('Authorization', "bearer {$token}");
86
		}
87
		else
88
		{
89
			$this->echoBox("WARNING: NOT LOGGED IN\nSome data might be missing");
90
		}
91
92
		$response = \Amp\wait((new Artax\Client)->request($request));
93
94
		$body = json_decode($response->getBody(), TRUE);
95
		return $body['meta']['count'];
96
	}
97
98
	public function MALItemCreate()
99
	{
100
		$input = json_decode('{
0 ignored issues
show
Unused Code introduced by
$input is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
101
			"watching_status": "current",
102
			"user_rating": "",
103
			"episodes_watched": "4",
104
			"rewatched": "0",
105
			"notes": "",
106
			"id": "15794526",
107
			"mal_id": "33731",
108
			"edit": "true"
109
		}', TRUE);
110
111
		$response = $this->malModel->createListItem([
0 ignored issues
show
Unused Code introduced by
$response is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
112
			'id' => 12255,
113
			'status' => 'planned',
114
			'type' => 'anime'
115
		]);
116
117
		//$response = $this->malModel->updateListItem($input);
0 ignored issues
show
Unused Code Comprehensibility introduced by
62% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
118
		//print_r($response);
119
		//echo $response->getBody();
0 ignored issues
show
Unused Code Comprehensibility introduced by
75% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
120
121
	}
122
123
	public function diffLists()
124
	{
125
		// Get libraryEntries with media.mappings from Kitsu
126
		// Organize mappings, and ignore entries without mappings
127
128
		// Get MAL list data
129
130
		// Compare each list entry
131
			// If a list item exists only on MAL, create it on Kitsu with the existing data from MAL
132
			// If a list item exists only on Kitsu, create it on MAL with the existing data from Kitsu
133
			// If an item already exists on both APIS:
134
				// Compare last updated dates, and use the later one
135
				// Otherwise, use rewatch count, then episode progress as critera for selecting the more up
136
				// to date entry
137
				// Based on the 'newer' entry, update the other api list item
138
	}
139
140
141
}