Skip to content
This repository has been archived by the owner on Jun 7, 2022. It is now read-only.

Commit

Permalink
PSR4-ify the Reports data store classes.
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffstieler committed Aug 12, 2019
1 parent 7514460 commit df9698f
Show file tree
Hide file tree
Showing 35 changed files with 260 additions and 198 deletions.
13 changes: 9 additions & 4 deletions src/API/Leaderboards.php
Expand Up @@ -11,6 +11,11 @@

defined( 'ABSPATH' ) || exit;

use \Automattic\WooCommerce\Admin\API\Reports\Categories\DataStore as CategoriesDataStore;
use \Automattic\WooCommerce\Admin\API\Reports\Coupons\DataStore as CouponsDataStore;
use \Automattic\WooCommerce\Admin\API\Reports\Customers\DataStore as CustomersDataStore;
use \Automattic\WooCommerce\Admin\API\Reports\Products\DataStore as ProductsDataStore;

/**
* Leaderboards controller.
*
Expand Down Expand Up @@ -73,7 +78,7 @@ public function register_routes() {
* @param string $persisted_query URL query string.
*/
public function get_coupons_leaderboard( $per_page, $after, $before, $persisted_query ) {
$coupons_data_store = new \WC_Admin_Reports_Coupons_Data_Store();
$coupons_data_store = new CouponsDataStore();
$coupons_data = $per_page > 0 ? $coupons_data_store->get_data(
array(
'orderby' => 'orders_count',
Expand Down Expand Up @@ -139,7 +144,7 @@ public function get_coupons_leaderboard( $per_page, $after, $before, $persisted_
* @param string $persisted_query URL query string.
*/
public function get_categories_leaderboard( $per_page, $after, $before, $persisted_query ) {
$categories_data_store = new \WC_Admin_Reports_Categories_Data_Store();
$categories_data_store = new CategoriesDataStore();
$categories_data = $per_page > 0 ? $categories_data_store->get_data(
array(
'orderby' => 'items_sold',
Expand Down Expand Up @@ -205,7 +210,7 @@ public function get_categories_leaderboard( $per_page, $after, $before, $persist
* @param string $persisted_query URL query string.
*/
public function get_customers_leaderboard( $per_page, $after, $before, $persisted_query ) {
$customers_data_store = new \WC_Admin_Reports_Customers_Data_Store();
$customers_data_store = new CustomersDataStore();
$customers_data = $per_page > 0 ? $customers_data_store->get_data(
array(
'orderby' => 'total_spend',
Expand Down Expand Up @@ -269,7 +274,7 @@ public function get_customers_leaderboard( $per_page, $after, $before, $persiste
* @param string $persisted_query URL query string.
*/
public function get_products_leaderboard( $per_page, $after, $before, $persisted_query ) {
$products_data_store = new \WC_Admin_Reports_Products_Data_Store();
$products_data_store = new ProductsDataStore();
$products_data = $per_page > 0 ? $products_data_store->get_data(
array(
'orderby' => 'items_sold',
Expand Down
4 changes: 2 additions & 2 deletions src/API/OnboardingProfile.php
Expand Up @@ -236,7 +236,7 @@ public static function get_profile_properties() {
'sanitize_callback' => 'wp_parse_slug_list',
'validate_callback' => 'rest_validate_request_arg',
'items' => array(
'enum' => array_keys( \WC_Admin_Onboarding::get_allowed_industries() ),
'enum' => array_keys( WC_Admin_Onboarding::get_allowed_industries() ),
'type' => 'string',
),
),
Expand All @@ -248,7 +248,7 @@ public static function get_profile_properties() {
'sanitize_callback' => 'wp_parse_slug_list',
'validate_callback' => 'rest_validate_request_arg',
'items' => array(
'enum' => array_keys( \WC_Admin_Onboarding::get_allowed_product_types() ),
'enum' => array_keys( WC_Admin_Onboarding::get_allowed_product_types() ),
'type' => 'string',
),
),
Expand Down
Expand Up @@ -5,13 +5,14 @@
* @package WooCommerce Admin/Classes
*/

defined( 'ABSPATH' ) || exit;
namespace Automattic\WooCommerce\Admin\API\Reports\Categories;

defined( 'ABSPATH' ) || exit;

/**
* WC_Admin_Reports_Categories_Data_Store.
*/
class WC_Admin_Reports_Categories_Data_Store extends WC_Admin_Reports_Data_Store implements WC_Admin_Reports_Data_Store_Interface {
class DataStore extends \Automattic\WooCommerce\Admin\API\Reports\DataStore implements \WC_Admin_Reports_Data_Store_Interface {

/**
* Table used to get the data.
Expand Down Expand Up @@ -207,7 +208,7 @@ protected function page_records( $data, $page_no, $items_per_page ) {
*/
protected function include_extended_info( &$categories_data, $query_args ) {
foreach ( $categories_data as $key => $category_data ) {
$extended_info = new ArrayObject();
$extended_info = new \ArrayObject();
if ( $query_args['extended_info'] ) {
$extended_info['name'] = get_the_category_by_ID( $category_data['category_id'] );
}
Expand All @@ -232,8 +233,8 @@ public function get_data( $query_args ) {
'page' => 1,
'order' => 'DESC',
'orderby' => 'date',
'before' => WC_Admin_Reports_Interval::default_before(),
'after' => WC_Admin_Reports_Interval::default_after(),
'before' => \WC_Admin_Reports_Interval::default_before(),
'after' => \WC_Admin_Reports_Interval::default_after(),
'fields' => '*',
'categories' => array(),
'extended_info' => false,
Expand Down Expand Up @@ -295,7 +296,7 @@ public function get_data( $query_args ) {
); // WPCS: cache ok, DB call ok, unprepared SQL ok.

if ( null === $categories_data ) {
return new WP_Error( 'woocommerce_reports_categories_result_failed', __( 'Sorry, fetching revenue data failed.', 'woocommerce-admin' ), array( 'status' => 500 ) );
return new \WP_Error( 'woocommerce_reports_categories_result_failed', __( 'Sorry, fetching revenue data failed.', 'woocommerce-admin' ), array( 'status' => 500 ) );
}

$record_count = count( $categories_data );
Expand Down
Expand Up @@ -5,12 +5,14 @@
* @package WooCommerce Admin/Classes
*/

namespace Automattic\WooCommerce\Admin\API\Reports\Coupons;

defined( 'ABSPATH' ) || exit;

/**
* WC_Admin_Reports_Coupons_Data_Store.
*/
class WC_Admin_Reports_Coupons_Data_Store extends WC_Admin_Reports_Data_Store implements WC_Admin_Reports_Data_Store_Interface {
class DataStore extends \Automattic\WooCommerce\Admin\API\Reports\DataStore implements \WC_Admin_Reports_Data_Store_Interface {

/**
* Table used to get the data.
Expand Down Expand Up @@ -170,33 +172,33 @@ protected function normalize_order_by( $order_by ) {
*/
protected function include_extended_info( &$coupon_data, $query_args ) {
foreach ( $coupon_data as $idx => $coupon_datum ) {
$extended_info = new ArrayObject();
$extended_info = new \ArrayObject();
if ( $query_args['extended_info'] ) {
$coupon_id = $coupon_datum['coupon_id'];
$coupon = new WC_Coupon( $coupon_id );
$coupon = new \WC_Coupon( $coupon_id );

$gmt_timzone = new DateTimeZone( 'UTC' );
$gmt_timzone = new \DateTimeZone( 'UTC' );

$date_expires = $coupon->get_date_expires();
if ( null === $date_expires ) {
$date_expires = '';
$date_expires_gmt = '';
} else {
$date_expires = $date_expires->format( WC_Admin_Reports_Interval::$iso_datetime_format );
$date_expires_gmt = new DateTime( $date_expires );
$date_expires = $date_expires->format( \WC_Admin_Reports_Interval::$iso_datetime_format );
$date_expires_gmt = new \DateTime( $date_expires );
$date_expires_gmt->setTimezone( $gmt_timzone );
$date_expires_gmt = $date_expires_gmt->format( WC_Admin_Reports_Interval::$iso_datetime_format );
$date_expires_gmt = $date_expires_gmt->format( \WC_Admin_Reports_Interval::$iso_datetime_format );
}

$date_created = $coupon->get_date_created();
if ( null === $date_created ) {
$date_created = '';
$date_created_gmt = '';
} else {
$date_created = $date_created->format( WC_Admin_Reports_Interval::$iso_datetime_format );
$date_created_gmt = new DateTime( $date_created );
$date_created = $date_created->format( \WC_Admin_Reports_Interval::$iso_datetime_format );
$date_created_gmt = new \DateTime( $date_created );
$date_created_gmt->setTimezone( $gmt_timzone );
$date_created_gmt = $date_created_gmt->format( WC_Admin_Reports_Interval::$iso_datetime_format );
$date_created_gmt = $date_created_gmt->format( \WC_Admin_Reports_Interval::$iso_datetime_format );
}

$extended_info = array(
Expand Down Expand Up @@ -229,8 +231,8 @@ public function get_data( $query_args ) {
'page' => 1,
'order' => 'DESC',
'orderby' => 'coupon_id',
'before' => WC_Admin_Reports_Interval::default_before(),
'after' => WC_Admin_Reports_Interval::default_after(),
'before' => \WC_Admin_Reports_Interval::default_before(),
'after' => \WC_Admin_Reports_Interval::default_after(),
'fields' => '*',
'coupons' => array(),
'extended_info' => false,
Expand Down Expand Up @@ -385,7 +387,7 @@ public static function sync_order_coupons( $order_id ) {
'order_id' => $order_id,
'coupon_id' => $coupon_id,
'discount_amount' => $coupon_item->get_discount(),
'date_created' => $order->get_date_created( 'edit' )->date( WC_Admin_Reports_Interval::$sql_datetime_format ),
'date_created' => $order->get_date_created( 'edit' )->date( \WC_Admin_Reports_Interval::$sql_datetime_format ),
),
array(
'%d',
Expand Down
Expand Up @@ -5,13 +5,14 @@
* @package WooCommerce Admin/Classes
*/

defined( 'ABSPATH' ) || exit;
namespace Automattic\WooCommerce\Admin\API\Reports\Coupons\Stats;

defined( 'ABSPATH' ) || exit;

/**
* WC_Reports_Coupons_Stats_Data_Store.
*/
class WC_Admin_Reports_Coupons_Stats_Data_Store extends WC_Admin_Reports_Coupons_Data_Store implements WC_Admin_Reports_Data_Store_Interface {
class DataStore extends \Automattic\WooCommerce\Admin\API\Reports\Coupons\DataStore implements \WC_Admin_Reports_Data_Store_Interface {

/**
* Mapping columns to data type to return correct response types.
Expand Down Expand Up @@ -102,8 +103,8 @@ public function get_data( $query_args ) {
'page' => 1,
'order' => 'DESC',
'orderby' => 'date',
'before' => WC_Admin_Reports_Interval::default_before(),
'after' => WC_Admin_Reports_Interval::default_after(),
'before' => \WC_Admin_Reports_Interval::default_before(),
'after' => \WC_Admin_Reports_Interval::default_after(),
'fields' => '*',
'interval' => 'week',
'coupons' => array(),
Expand Down Expand Up @@ -142,7 +143,7 @@ public function get_data( $query_args ) {
); // WPCS: cache ok, DB call ok, unprepared SQL ok.

$db_interval_count = count( $db_intervals );
$expected_interval_count = WC_Admin_Reports_Interval::intervals_between( $query_args['after'], $query_args['before'], $query_args['interval'] );
$expected_interval_count = \WC_Admin_Reports_Interval::intervals_between( $query_args['after'], $query_args['before'], $query_args['interval'] );
$total_pages = (int) ceil( $expected_interval_count / $intervals_query['per_page'] );
if ( $query_args['page'] < 1 || $query_args['page'] > $total_pages ) {
return $data;
Expand All @@ -164,7 +165,7 @@ public function get_data( $query_args ) {
if ( null === $totals ) {
return $data;
}
$segmenter = new WC_Admin_Reports_Coupons_Stats_Segmenting( $query_args, $this->report_columns );
$segmenter = new \WC_Admin_Reports_Coupons_Stats_Segmenting( $query_args, $this->report_columns );
$totals[0]['segments'] = $segmenter->get_totals_segments( $totals_query, $table_name );
$totals = (object) $this->cast_numbers( $totals[0] );

Expand Down Expand Up @@ -207,7 +208,7 @@ public function get_data( $query_args ) {
'page_no' => (int) $query_args['page'],
);

if ( WC_Admin_Reports_Interval::intervals_missing( $expected_interval_count, $db_interval_count, $intervals_query['per_page'], $query_args['page'], $query_args['order'], $query_args['orderby'], count( $intervals ) ) ) {
if ( \WC_Admin_Reports_Interval::intervals_missing( $expected_interval_count, $db_interval_count, $intervals_query['per_page'], $query_args['page'], $query_args['order'], $query_args['orderby'], count( $intervals ) ) ) {
$this->fill_in_missing_intervals( $db_intervals, $query_args['adj_after'], $query_args['adj_before'], $query_args['interval'], $data );
$this->sort_intervals( $data, $query_args['orderby'], $query_args['order'] );
$this->remove_extra_records( $data, $query_args['page'], $intervals_query['per_page'], $db_interval_count, $expected_interval_count, $query_args['orderby'], $query_args['order'] );
Expand Down
Expand Up @@ -5,12 +5,14 @@
* @package WooCommerce Admin/Classes
*/

namespace Automattic\WooCommerce\Admin\API\Reports\Customers;

defined( 'ABSPATH' ) || exit;

/**
* WC_Admin_Reports_Customers_Data_Store.
*/
class WC_Admin_Reports_Customers_Data_Store extends WC_Admin_Reports_Data_Store implements WC_Admin_Reports_Data_Store_Interface {
class DataStore extends \Automattic\WooCommerce\Admin\API\Reports\DataStore implements \WC_Admin_Reports_Data_Store_Interface {

/**
* Table used to get the data.
Expand Down Expand Up @@ -169,14 +171,14 @@ protected function get_time_period_sql_params( $query_args, $table_name ) {
$column_name = $param_info['column'];

if ( ! empty( $query_args[ $before_arg ] ) ) {
$datetime = new DateTime( $query_args[ $before_arg ] );
$datetime_str = $datetime->format( WC_Admin_Reports_Interval::$sql_datetime_format );
$datetime = new \DateTime( $query_args[ $before_arg ] );
$datetime_str = $datetime->format( \WC_Admin_Reports_Interval::$sql_datetime_format );
$subclauses[] = "{$column_name} <= '$datetime_str'";
}

if ( ! empty( $query_args[ $after_arg ] ) ) {
$datetime = new DateTime( $query_args[ $after_arg ] );
$datetime_str = $datetime->format( WC_Admin_Reports_Interval::$sql_datetime_format );
$datetime = new \DateTime( $query_args[ $after_arg ] );
$datetime_str = $datetime->format( \WC_Admin_Reports_Interval::$sql_datetime_format );
$subclauses[] = "{$column_name} >= '$datetime_str'";
}

Expand Down Expand Up @@ -347,8 +349,8 @@ public function get_data( $query_args ) {
'page' => 1,
'order' => 'DESC',
'orderby' => 'date_registered',
'order_before' => WC_Admin_Reports_Interval::default_before(),
'order_after' => WC_Admin_Reports_Interval::default_after(),
'order_before' => \WC_Admin_Reports_Interval::default_before(),
'order_after' => \WC_Admin_Reports_Interval::default_after(),
'fields' => '*',
);
$query_args = wp_parse_args( $query_args, $defaults );
Expand Down Expand Up @@ -516,10 +518,10 @@ public static function get_or_create_customer_from_order( $order ) {
// Add registered customer data.
if ( 0 !== $order->get_user_id() ) {
$user_id = $order->get_user_id();
$customer = new WC_Customer( $user_id );
$customer = new \WC_Customer( $user_id );
$data['user_id'] = $user_id;
$data['username'] = $customer->get_username( 'edit' );
$data['date_registered'] = $customer->get_date_created( 'edit' ) ? $customer->get_date_created( 'edit' )->date( WC_Admin_Reports_Interval::$sql_datetime_format ) : null;
$data['date_registered'] = $customer->get_date_created( 'edit' ) ? $customer->get_date_created( 'edit' )->date( \WC_Admin_Reports_Interval::$sql_datetime_format ) : null;
$format[] = '%d';
$format[] = '%s';
$format[] = '%s';
Expand Down Expand Up @@ -611,7 +613,7 @@ public static function get_oldest_orders( $customer_id ) {
public static function update_registered_customer( $user_id ) {
global $wpdb;

$customer = new WC_Customer( $user_id );
$customer = new \WC_Customer( $user_id );

if ( ! self::is_valid_customer( $user_id ) ) {
return false;
Expand All @@ -638,7 +640,7 @@ public static function update_registered_customer( $user_id ) {
'state' => $customer->get_billing_state( 'edit' ),
'postcode' => $customer->get_billing_postcode( 'edit' ),
'country' => $customer->get_billing_country( 'edit' ),
'date_registered' => $customer->get_date_created( 'edit' )->date( WC_Admin_Reports_Interval::$sql_datetime_format ),
'date_registered' => $customer->get_date_created( 'edit' )->date( \WC_Admin_Reports_Interval::$sql_datetime_format ),
'date_last_active' => $last_active ? date( 'Y-m-d H:i:s', $last_active ) : null,
);
$format = array(
Expand Down Expand Up @@ -680,7 +682,7 @@ public static function update_registered_customer( $user_id ) {
* @return bool
*/
protected static function is_valid_customer( $user_id ) {
$customer = new WC_Customer( $user_id );
$customer = new \WC_Customer( $user_id );

if ( absint( $customer->get_id() ) !== absint( $user_id ) ) {
return false;
Expand Down
Expand Up @@ -5,12 +5,14 @@
* @package WooCommerce Admin/Classes
*/

namespace Automattic\WooCommerce\Admin\API\Reports\Customers\Stats;

defined( 'ABSPATH' ) || exit;

/**
* WC_Admin_Reports_Customers_Stats_Data_Store.
*/
class WC_Admin_Reports_Customers_Stats_Data_Store extends WC_Admin_Reports_Customers_Data_Store implements WC_Admin_Reports_Data_Store_Interface {
class DataStore extends \Automattic\WooCommerce\Admin\API\Reports\Customers\DataStore implements \WC_Admin_Reports_Data_Store_Interface {
/**
* Mapping columns to data type to return correct response types.
*
Expand Down

0 comments on commit df9698f

Please sign in to comment.