Skip to content

Commit

Permalink
Remove magic method and use setter method for user data
Browse files Browse the repository at this point in the history
  • Loading branch information
E2E Environment committed Dec 9, 2024
1 parent e863752 commit bbeb3f9
Showing 1 changed file with 14 additions and 40 deletions.
54 changes: 14 additions & 40 deletions classes/User/User.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<?php
namespace Imagify\User;

use BadMethodCallException;
use Imagify_Data;
use WP_Error;

Expand Down Expand Up @@ -119,39 +118,32 @@ class User {

/**
* Cache key.
*
* @var string
*/
*/
private $cache_key = 'imagify_user_data';

Check failure on line 124 in classes/User/User.php

View workflow job for this annotation

GitHub Actions / WPRocket lint with PHP Stan. PHP 8.2 on ubuntu-latest.

Property Imagify\User\User::$cache_key is never read, only written.

Check warning on line 124 in classes/User/User.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

classes/User/User.php#L124

Avoid unused private fields such as '$cache_key'.

/**
* Cache duration
*
* @var int
*/
*/
private $cache_duration = 6 * MINUTE_IN_SECONDS;

Check failure on line 131 in classes/User/User.php

View workflow job for this annotation

GitHub Actions / WPRocket lint with PHP Stan. PHP 8.2 on ubuntu-latest.

Property Imagify\User\User::$cache_duration is never read, only written.

Check warning on line 131 in classes/User/User.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

classes/User/User.php#L131

Avoid unused private fields such as '$cache_duration'.

/**
* Initialisation.
*
* @var bool
*/
*/
protected $initialized = false;

/**
* Initialise the user data by fetching the api data
*
* @return void
*/
*/
public function init_user() {
if (isset($this->id)) {
return;
}

$cached_user = get_transient( $this->cache_key );

if ( $cached_user ) {
$this->set_user_properties( $cached_user );

if ( $this->initialized ) {
return;
}

Expand All @@ -162,10 +154,8 @@ public function init_user() {
return;
}

// Store user data in cache.
set_transient( $this->cache_key, $user, $this->cache_duration );

$this->set_user_properties( $user );
$this->initialized = true;
}

private function set_user_properties( $user ) {
Expand All @@ -182,34 +172,15 @@ private function set_user_properties( $user ) {
$this->is_monthly = $user->is_monthly;
}

/**
* A magic call method.
* Added this method to avoid calling init_user for every method in this class.
* The init_user method is lazy call here.
* @param $method
* @param $args
*
*/
public function __call( $method, $args ) {
if( ! $this->initialized ) {
$this->init_user();
$this->initialized = true;
}

if ( method_exists( $this, $method ) ) {
return call_user_func_array( [ $this, $method ], $args );
}

throw new BadMethodCallException( "Method {$method} does not exist");
}

/**
* Get the possible error returned when fetching user data.
*
* @return bool|WP_Error A \WP_Error object if the request to fetch the user data failed. False overwise.
* @since 1.9.9
*/
public function get_error() {
$this->init_user();

return $this->error;
}

Expand Down Expand Up @@ -295,6 +266,7 @@ public function get_percent_consumed_quota() {
* @return float|int
*/
public function get_percent_unconsumed_quota() {
$this->init_user();
return 100 - $this->get_percent_consumed_quota();
}

Expand All @@ -306,6 +278,7 @@ public function get_percent_unconsumed_quota() {
* @return bool
*/
public function is_free() {
$this->init_user();
return 1 === $this->plan_id;
}

Expand All @@ -315,6 +288,7 @@ public function is_free() {
* @return bool
*/
public function is_growth() {
$this->init_user();
return ( 16 === $this->plan_id || 18 === $this->plan_id );
}

Expand All @@ -324,6 +298,7 @@ public function is_growth() {
* @return bool
*/
public function is_infinite() {
$this->init_user();
return ( 15 === $this->plan_id || 17 === $this->plan_id );
}

Expand All @@ -349,8 +324,7 @@ public function is_over_quota() {

/**
* Get user Id
*
*/
*/
public function get_id() {
$this->init_user();

Expand Down

0 comments on commit bbeb3f9

Please sign in to comment.