Completed
Branch master (205409)
by Timothy
02:36
created

ListItem::delete()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 5
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\API\Kitsu;
18
19
use Aviat\AnimeClient\API\AbstractListItem;
20
use Aviat\Ion\Di\ContainerAware;
21
use Aviat\Ion\Json;
22
use GuzzleHttp\Exception\ClientException;
23
use GuzzleHttp\Psr7\Response;
24
use RuntimeException;
25
26
/**
27
 * CRUD operations for Kitsu list items
28
 */
29
class ListItem extends AbstractListItem {
30
	use ContainerAware;
31
	use KitsuTrait;
32
33
	public function __construct()
34
	{
35
		$this->init();
36
	}
37
38
	public function create(array $data): bool
39
	{
40
		$response = $this->getResponse('POST', 'library-entries', [
41
			'body' => Json::encode([
42
				'data' => [
43
					'type' => 'libraryEntries',
44
					'attributes' => [
45
						'status' => $data['status'],
46
						'progress' => $data['progress'] ?? 0
47
					],
48
					'relationships' => [
49
						'user' => [
50
							'data' => [
51
								'id' => $data['user_id'],
52
								'type' => 'users'
53
							]
54
						],
55
						'media' => [
56
							'data' => [
57
								'id' => $data['id'],
58
								'type' => $data['type']
59
							]
60
						]
61
					]
62
				]
63
			])
64
		]);
65
66
		return ($response->getStatusCode() === 201);
67
	}
68
69
	public function delete(string $id): bool
70
	{
71
		$response = $this->getResponse('DELETE', "library-entries/{$id}");
72
		return ($response->getStatusCode() === 204);
73
	}
74
75
	public function get(string $id): array
76
	{
77
		return $this->getRequest("library-entries/{$id}", [
78
			'query' => [
79
				'include' => 'media,media.genres,media.mappings'
80
			]
81
		]);
82
	}
83
84
	public function update(string $id, array $data): Response
85
	{
86
		$requestData = [
87
			'data' => [
88
				'id' => $id,
89
				'type' => 'libraryEntries',
90
				'attributes' => $data
91
			]
92
		];
93
94
		$response = $this->getResponse('PATCH', "library-entries/{$id}", [
95
			'body' => JSON::encode($requestData)
96
		]);
97
98
		return $response;
99
	}
100
}