Completed
Branch develop (9c1dc5)
by Timothy
07:15
created

ListItem   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 117
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
dl 0
loc 117
rs 10
c 0
b 0
f 0
wmc 10
lcom 1
cbo 6

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getAuthHeader() 0 14 2
B create() 0 40 2
A delete() 0 14 2
A get() 0 19 2
A update() 0 21 2
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\Kitsu;
18
19
use const Aviat\AnimeClient\SESSION_SEGMENT;
20
21
use Amp\Artax\Request;
22
use Aviat\AnimeClient\API\AbstractListItem;
23
use Aviat\Ion\Di\ContainerAware;
24
use Aviat\Ion\Json;
25
use RuntimeException;
26
27
/**
28
 * CRUD operations for Kitsu list items
29
 */
30
class ListItem extends AbstractListItem {
31
	use ContainerAware;
32
	use KitsuTrait;
33
34
	private function getAuthHeader()
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...
35
	{
36
		$sessionSegment = $this->getContainer()
37
			->get('session')
38
			->getSegment(SESSION_SEGMENT);
39
40
		if ($sessionSegment->get('auth_token') !== null)
41
		{
42
			$token = $sessionSegment->get('auth_token');
43
			return "bearer {$token}";
44
		}
45
46
		return FALSE;
47
	}
48
49
	public function create(array $data): Request
50
	{
51
		$body = [
52
			'data' => [
53
				'type' => 'libraryEntries',
54
				'attributes' => [
55
					'status' => $data['status'],
56
					'progress' => $data['progress'] ?? 0
57
				],
58
				'relationships' => [
59
					'user' => [
60
						'data' => [
61
							'id' => $data['user_id'],
62
							'type' => 'users'
63
						]
64
					],
65
					'media' => [
66
						'data' => [
67
							'id' => $data['id'],
68
							'type' => $data['type']
69
						]
70
					]
71
				]
72
			]
73
		];
74
75
		$authHeader = $this->getAuthHeader();
76
77
		$request = $this->requestBuilder->newRequest('POST', 'library-entries');
78
79
		if ($authHeader !== FALSE)
80
		{
81
			$request = $request->setHeader('Authorization', $authHeader);
82
		}
83
84
		return $request->setJsonBody($body)
85
			->getFullRequest();
86
87
		// return ($response->getStatus() === 201);
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...
88
	}
89
90
	public function delete(string $id): Request
91
	{
92
		$authHeader = $this->getAuthHeader();
93
		$request = $this->requestBuilder->newRequest('DELETE', "library-entries/{$id}");
94
95
		if ($authHeader !== FALSE)
96
		{
97
			$request = $request->setHeader('Authorization', $authHeader);
98
		}
99
100
		return $request->getFullRequest();
101
102
		// return ($response->getStatus() === 204);
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...
103
	}
104
105
	public function get(string $id): array
106
	{
107
		$authHeader = $this->getAuthHeader();
108
		
109
		$request = $this->requestBuilder->newRequest('GET', "library-entries/{$id}")
110
			->setQuery([
111
				'include' => 'media,media.genres,media.mappings'
112
			]);
113
		
114
		if ($authHeader !== FALSE)
115
		{
116
			$request = $request->setHeader('Authorization', $authHeader);
117
		}
118
		
119
		$request = $request->getFullRequest();
120
121
		$response = \Amp\wait((new \Amp\Artax\Client)->request($request));
122
		return Json::decode($response->getBody());
123
	}
124
125
	public function update(string $id, array $data): Request
126
	{
127
		$authHeader = $this->getAuthHeader();
128
		$requestData = [
129
			'data' => [
130
				'id' => $id,
131
				'type' => 'libraryEntries',
132
				'attributes' => $data
133
			]
134
		];
135
		
136
		$request = $this->requestBuilder->newRequest('PATCH', "library-entries/{$id}")
137
			->setJsonBody($requestData);
138
		
139
		if ($authHeader !== FALSE)
140
		{
141
			$request = $request->setHeader('Authorization', $authHeader);
142
		}
143
		
144
		return $request->getFullRequest();
145
	}
146
}