Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
47.62% |
10 / 21 |
|
33.33% |
2 / 6 |
CRAP | |
0.00% |
0 / 1 |
| Fetch | |
47.62% |
10 / 21 |
|
33.33% |
2 / 6 |
20.64 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| get_url | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
3 | |||
| get_args | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
2 | |||
| should_cache | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| get_cache_timeout | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| get_md5_hash | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace NewfoldLabs\WP\Module\Data\WonderBlocks\Requests; |
| 4 | |
| 5 | use NewfoldLabs\WP\Module\Data\SiteClassification\PrimaryType; |
| 6 | use NewfoldLabs\WP\Module\Data\SiteClassification\SecondaryType; |
| 7 | |
| 8 | /** |
| 9 | * Class Fetch |
| 10 | * |
| 11 | * Defines the structure of a WonderBlock fetch request. |
| 12 | */ |
| 13 | class Fetch extends Request { |
| 14 | |
| 15 | /** |
| 16 | * The type of data to fetch. |
| 17 | * |
| 18 | * @var string |
| 19 | */ |
| 20 | private $type; |
| 21 | |
| 22 | /** |
| 23 | * A particular slug of data to fetch. |
| 24 | * |
| 25 | * @var string |
| 26 | */ |
| 27 | private $slug; |
| 28 | |
| 29 | /** |
| 30 | * The primary type query parameter. {@see PrimaryType} for information on primary types. |
| 31 | * |
| 32 | * @var string |
| 33 | */ |
| 34 | private $primary_type; |
| 35 | |
| 36 | /** |
| 37 | * The secondary type query parameter. {@see SecondaryType} for information on secondary types. |
| 38 | * |
| 39 | * @var string |
| 40 | */ |
| 41 | private $secondary_type; |
| 42 | |
| 43 | /** |
| 44 | * The category of data to fetch. |
| 45 | * |
| 46 | * @var string |
| 47 | */ |
| 48 | private $category; |
| 49 | |
| 50 | /** |
| 51 | * Defines whether or not the handler should cache the response data. |
| 52 | * |
| 53 | * @var bool |
| 54 | */ |
| 55 | private $should_cache = true; |
| 56 | |
| 57 | /** |
| 58 | * Defines the timeout for the cache. |
| 59 | * |
| 60 | * @var int |
| 61 | */ |
| 62 | private $cache_timeout; |
| 63 | |
| 64 | /** |
| 65 | * Constructor for the Fetch class. |
| 66 | * |
| 67 | * @param array{endpoint?:string,type?:string,slug?:string,primary_type?:string,secondary_string:string,category?:string,should_cache?:bool,cache_timeout?:int} $args An array of arguments that map to the class variables. |
| 68 | */ |
| 69 | public function __construct( $args = array() ) { |
| 70 | $this->cache_timeout = constant( 'DAY_IN_SECONDS' ); |
| 71 | foreach ( $args as $arg => $value ) { |
| 72 | $this->$arg = $value; |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Fetch the correct API URL. |
| 78 | * |
| 79 | * @return string |
| 80 | */ |
| 81 | public function get_url() { |
| 82 | $url = ''; |
| 83 | if ( isset( $this->endpoint ) ) { |
| 84 | $url = $this->get_base_url() . "/{$this->endpoint}"; |
| 85 | if ( isset( $this->slug ) ) { |
| 86 | $url .= "/{$this->slug}"; |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | return $url; |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Fetch all the valid arguments. |
| 95 | * |
| 96 | * @return array |
| 97 | */ |
| 98 | public function get_args() { |
| 99 | return array( |
| 100 | 'type' => $this->type, |
| 101 | 'primary_type' => $this->primary_type, |
| 102 | 'secondary_type' => $this->secondary_type, |
| 103 | 'category' => $this->category, |
| 104 | ); |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Determines whether the response for this request should be cached. |
| 109 | */ |
| 110 | public function should_cache(): bool { |
| 111 | return $this->should_cache; |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Get the cache timeout. |
| 116 | */ |
| 117 | public function get_cache_timeout(): int { |
| 118 | return $this->cache_timeout; |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Get the MD5 hash to easily identify a request. |
| 123 | */ |
| 124 | public function get_md5_hash(): string { |
| 125 | $args = $this->get_args(); |
| 126 | // Slug is not part of args as it becomes a part of the URL path. |
| 127 | $args['slug'] = $this->slug; |
| 128 | return md5( serialize( $args ) ); |
| 129 | } |
| 130 | } |