Completed
Branch master (1afb45)
by Timothy
04:13
created

Collection::get_genre_list()   B

Complexity

Conditions 5
Paths 8

Size

Total Lines 41
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 41
rs 8.439
cc 5
eloc 20
nc 8
nop 1
1
<?php
2
/**
3
 * Hummingbird Anime Client
4
 *
5
 * An API client for Hummingbird to manage anime and manga watch lists
6
 *
7
 * @package     HummingbirdAnimeClient
8
 * @author      Timothy J. Warren
9
 * @copyright   Copyright (c) 2015 - 2016
10
 * @link        https://github.com/timw4mail/HummingBirdAnimeClient
11
 * @license     MIT
12
 */
13
14
namespace Aviat\AnimeClient\Model;
15
16
use Aviat\Ion\Di\ContainerInterface;
17
18
/**
19
 * Base model for anime and manga collections
20
 */
21
class Collection extends DB {
22
23
	/**
24
	 * Anime API Model
25
	 * @var object $anime_model
26
	 */
27
	protected $anime_model;
28
29
	/**
30
	 * Whether the database is valid for querying
31
	 * @var bool
32
	 */
33
	protected $valid_database = FALSE;
34
35
	/**
36
	 * Create a new collection object
37
	 *
38
	 * @param ContainerInterface $container
39
	 * @return boolean
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
40
	 */
41
	public function __construct(ContainerInterface $container)
42
	{
43
		parent::__construct($container);
44
45
		try
46
		{
47
			$this->db = \Query($this->db_config['collection']);
48
		}
49
		catch (\PDOException $e)
50
		{
51
			$this->valid_database = FALSE;
52
			return FALSE;
53
		}
54
		$this->anime_model = $container->get('anime-model');
55
56
		// Is database valid? If not, set a flag so the
57
		// app can be run without a valid database
58
		if ($this->db_config['collection']['type'] === 'sqlite')
59
		{
60
			$db_file_name = $this->db_config['collection']['file'];
61
62
			if ($db_file_name !== ':memory:' && file_exists($db_file_name))
63
			{
64
				$db_file = file_get_contents($db_file_name);
65
				$this->valid_database = (strpos($db_file, 'SQLite format 3') === 0);
66
			}
67
			else
68
			{
69
				$this->valid_database = FALSE;
70
			}
71
		}
72
		else
73
		{
74
			$this->valid_database = TRUE;
75
		}
76
	}
77
78
	/**
79
	 * Get genres for anime collection items
80
	 *
81
	 * @param array $filter
82
	 * @return array
83
	 */
84
	public function get_genre_list($filter = [])
85
	{
86
		$this->db->select('hummingbird_id, genre')
87
			->from('genre_anime_set_link gl')
88
			->join('genres g', 'g.id=gl.genre_id', 'left');
89
90
91
		if ( ! empty($filter))
92
		{
93
			$this->db->where_in('hummingbird_id', $filter);
94
		}
95
96
		$query = $this->db->order_by('hummingbird_id')
97
			->order_by('genre')
98
			->get();
99
100
		$output = [];
101
102
		foreach ($query->fetchAll(\PDO::FETCH_ASSOC) as $row)
103
		{
104
			$id = $row['hummingbird_id'];
105
			$genre = $row['genre'];
106
107
			// Empty genre names aren't useful
108
			if (empty($genre))
109
			{
110
				continue;
111
			}
112
113
			if (array_key_exists($id, $output))
114
			{
115
				array_push($output[$id], $genre);
116
			}
117
			else
118
			{
119
				$output[$id] = [$genre];
120
			}
121
		}
122
123
		return $output;
124
	}
125
126
}
127
// End of Collection.php