Completed
Push — develop ( 1d4633...83eb6a )
by David
02:35 queued 10s
created

Cached_Image_License_Service   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 52
rs 10
c 0
b 0
f 0
wmc 3
lcom 1
cbo 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 1
A get_non_public_domain_images() 0 15 2
1
<?php
2
/**
3
 *
4
 */
5
6
namespace Wordlift\Images_Licenses;
7
8
use Wordlift\Api\Api_Service;
9
use Wordlift\Cache\Ttl_Cache;
10
11
class Cached_Image_License_Service extends Image_License_Service {
12
13
	const GET_NON_PUBLIC_DOMAIN_IMAGES = 'get_non_public_domain_images';
14
15
	/**
16
	 * @var Ttl_Cache
17
	 */
18
	private $cache_service;
19
20
	/**
21
	 * @var Image_License_Service
22
	 */
23
	private $image_license_service;
24
25
	/**
26
	 * Images_Licenses_Service constructor.
27
	 *
28
	 * @param Image_License_Service $image_license_service
29
	 * @param Ttl_Cache $cache_service
30
	 */
31
	public function __construct( $image_license_service, $cache_service ) {
32
33
		$this->image_license_service = $image_license_service;
34
		$this->cache_service         = $cache_service;
35
36
		$that = $this;
37
		add_action( 'wp_ajax_wl_cached_image_license_service__get_non_public_domain_images', function () use ( $that ) {
38
			wp_send_json_success( $that->get_non_public_domain_images() );
39
		} );
40
41
	}
42
43
	/**
44
	 * @return array
45
	 */
46
	public function get_non_public_domain_images() {
47
48
		// Return the cached data if available.
49
		$cache = $this->cache_service->get( self::GET_NON_PUBLIC_DOMAIN_IMAGES );
50
		if ( ! is_null( $cache ) ) {
51
			return $cache;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $cache; (object|integer|double|string|array|boolean) is incompatible with the return type of the parent method Wordlift\Images_Licenses...on_public_domain_images of type array.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
52
		}
53
54
		$data = $this->image_license_service->get_non_public_domain_images();
55
56
		// Store the cached data.
57
		$this->cache_service->put( self::GET_NON_PUBLIC_DOMAIN_IMAGES, $data );
58
59
		return $data;
60
	}
61
62
}
63