TypeaheadController::getTypeaheadSource()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 22
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 3
eloc 9
c 2
b 1
f 0
nc 3
nop 2
dl 0
loc 22
rs 9.9666
1
<?php
2
3
use \yiilazyimage\components\LazyImage as LazyImage;
0 ignored issues
show
Bug introduced by
The type \yiilazyimage\components\LazyImage was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
4
5
/**
6
 * Handles AJAX requests for typeahead related data
7
 *
8
 * @author Sam Stenvall <[email protected]>
9
 * @copyright Copyright &copy; Sam Stenvall 2015-
10
 * @license https://www.gnu.org/licenses/gpl.html The GNU General Public License v3.0
11
 */
12
class TypeaheadController extends Controller
13
{
14
15
	/**
16
	 * Returns the typeahead data for the actor fields.
17
	 * @param string $mediaType filter by movies or TV shows
18
	 */
19
	public function actionGetActorNames($mediaType)
20
	{
21
		$cacheId = 'MovieFilterActorNameTypeahead_'.$mediaType;
22
23
		$this->renderJson($this->getTypeaheadSource($cacheId, function() use($mediaType)
24
		{
25
			return $this->getTypeaheadData(VideoLibrary::getActors($mediaType));
26
		}));
27
	}
28
	
29
	/**
30
	 * Returns the typeahead data for the movie name field
31
	 */
32
	public function actionGetMovieNames()
33
	{
34
		$cacheId = 'MovieFilterMovieNameTypeahead';
35
36
		$this->renderJson($this->getTypeaheadSource($cacheId, function()
37
		{
38
			$movies = VideoLibrary::getMovies(array('properties'=>array(
39
				'year', 'genre', 'thumbnail'
40
			)));
41
			
42
			// Modify some of the raw data so it's ready for rendering
43
			foreach ($movies as $movie)
44
			{
45
				$thumbnail = ThumbnailFactory::create($movie->thumbnail, Thumbnail::SIZE_VERY_SMALL);
46
				
47
				$movie->thumbnail = LazyImage::image($thumbnail->getUrl());
48
				$movie->genre = $movie->getGenreString();
0 ignored issues
show
Documentation Bug introduced by
It seems like $movie->getGenreString() of type string is incompatible with the declared type array of property $genre.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
49
			}
50
			
51
			return $movies;
52
		}));
53
	}
54
	
55
	/**
56
	 * Returns the typeahead data for the TV Show name field
57
	 */
58
	public function actionGetTVShowNames()
59
	{
60
		$cacheId = 'MovieFilterTVShowNameTypeahead';
61
62
		$this->renderJson($this->getTypeaheadSource($cacheId, function()
63
		{
64
			return $this->getTypeaheadData(VideoLibrary::getTVShows(array('properties'=>array())));
65
		}));
66
	}
67
	
68
	/**
69
	 * Returns the typeahead data for the director field
70
	 */
71
	public function actionGetDirectorNames()
72
	{
73
		$this->renderJson(json_encode(VideoLibrary::getDirectors()));
74
	}
75
76
	/**
77
	 * Encodes the return value of the callable as JavaScript and returns that. 
78
	 * If cacheApiCalls is enabled, the result will be fetched from cache 
79
	 * whenever possible.
80
	 * @param string $cacheId the cache ID
81
	 * @param callable $callable a closure that returns the typeahead source
82
	 * @return string JavaScript encoded string representing the data
83
	 */
84
	private function getTypeaheadSource($cacheId, callable $callable)
85
	{
86
		// Bust caches that were made when the data stored was still encoded 
87
		// with CJavaScript::encode()
88
		$cacheId .= '_v2';
89
		
90
		// Cache the encoded JavaScript if the "cache API calls" setting is enabled
91
		if (Setting::getBoolean('cacheApiCalls'))
92
		{
93
			$typeaheadData = Yii::app()->apiCallCache->get($cacheId);
94
95
			if ($typeaheadData === false)
96
			{
97
				$typeaheadData = json_encode($callable());
98
99
				Yii::app()->apiCallCache->set($cacheId, $typeaheadData);
100
			}
101
		}
102
		else
103
			$typeaheadData = json_encode($callable());
104
105
		return $typeaheadData;
106
	}
107
108
	/**
109
	 * Converts the specified array of objects to an array of data that can 
110
	 * be serialized to JSON
111
	 * @param ITypeaheadData[] $sourceData the source data
112
	 * @return array the typeahead data
113
	 */
114
	private function getTypeaheadData($sourceData)
115
	{
116
		$typeaheadData = array();
117
118
		foreach ($sourceData as $media)
119
			$typeaheadData[] = $media->getName();
120
121
		return $typeaheadData;
122
	}
123
124
	/**
125
	 * Serves the specified JSON data
126
	 * @param string $json
127
	 */
128
	private function renderJson($json)
129
	{
130
		$this->layout = false;
131
132
		header('Content-Type: application/json');
133
		echo $json;
134
	}
135
136
}
137