Collection   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 109
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 1
Bugs 0 Features 1
Metric Value
dl 0
loc 109
rs 10
c 1
b 0
f 1
wmc 10
lcom 1
cbo 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 38 5
B get_genre_list() 0 41 5
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\Model;
18
19
20
use Aviat\Ion\Di\ContainerAware;
21
use Aviat\Ion\Di\ContainerInterface;
22
use Aviat\Ion\Model\DB;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Aviat\AnimeClient\Model\DB.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
23
use PDO;
24
use PDOException;
25
26
/**
27
 * Base model for anime and manga collections
28
 */
29
class Collection extends DB {
30
	
31
	use ContainerAware;
32
33
	/**
34
	 * Anime API Model
35
	 * @var object $anime_model
36
	 */
37
	protected $anime_model;
38
39
	/**
40
	 * Whether the database is valid for querying
41
	 * @var boolean
42
	 */
43
	protected $valid_database = FALSE;
44
45
	/**
46
	 * Create a new collection object
47
	 *
48
	 * @param ContainerInterface $container
49
	 */
50
	public function __construct(ContainerInterface $container)
51
	{
52
		$this->container = $container;
53
		
54
		parent::__construct($container->get('config'));
55
56
		try
57
		{
58
			$this->db = \Query($this->db_config['collection']);
59
		}
60
		catch (PDOException $e)
61
		{
62
			//$this->valid_database = FALSE;
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% 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...
63
			//return FALSE;
64
		}
65
		$this->anime_model = $container->get('anime-model');
66
67
		// Is database valid? If not, set a flag so the
68
		// app can be run without a valid database
69
		if ($this->db_config['collection']['type'] === 'sqlite')
70
		{
71
			$db_file_name = $this->db_config['collection']['file'];
72
73
			if ($db_file_name !== ':memory:' && file_exists($db_file_name))
74
			{
75
				$db_file = file_get_contents($db_file_name);
76
				$this->valid_database = (strpos($db_file, 'SQLite format 3') === 0);
77
			}
78
			else
79
			{
80
				$this->valid_database = FALSE;
81
			}
82
		}
83
		else
84
		{
85
			$this->valid_database = TRUE;
86
		}
87
	}
88
89
	/**
90
	 * Get genres for anime collection items
91
	 *
92
	 * @param array $filter
93
	 * @return array
94
	 */
95
	public function get_genre_list($filter = [])
96
	{
97
		$this->db->select('hummingbird_id, genre')
98
			->from('genre_anime_set_link gl')
99
			->join('genres g', 'g.id=gl.genre_id', 'left');
100
101
102
		if ( ! empty($filter))
103
		{
104
			$this->db->where_in('hummingbird_id', $filter);
105
		}
106
107
		$query = $this->db->order_by('hummingbird_id')
108
			->order_by('genre')
109
			->get();
110
111
		$output = [];
112
113
		foreach ($query->fetchAll(PDO::FETCH_ASSOC) as $row)
114
		{
115
			$id = $row['hummingbird_id'];
116
			$genre = $row['genre'];
117
118
			// Empty genre names aren't useful
119
			if (empty($genre))
120
			{
121
				continue;
122
			}
123
124
			if (array_key_exists($id, $output))
125
			{
126
				array_push($output[$id], $genre);
127
			}
128
			else
129
			{
130
				$output[$id] = [$genre];
131
			}
132
		}
133
134
		return $output;
135
	}
136
137
}
138
// End of Collection.php