v1.0.0.0
StocketBase
- <?php
-
- declare(strict_types=1);
-
- require_once __DIR__ . '/database.php';
- require_once __DIR__ . '/digital-files.php';
-
- final class ProductStats
- {
- public static function buyersCount(int $productId): int
- {
- try {
- $usersDb = Database::users();
- $placeholders = implode(',', array_fill(0, count(DigitalFiles::PAID_ORDER_STATUSES), '?'));
-
- $orderStatement = $usersDb->prepare(
- "SELECT COUNT(DISTINCT CASE WHEN o.user_account_id IS NOT NULL THEN CONCAT('u', o.user_account_id) ELSE CONCAT('g', LOWER(o.guest_email)) END)
- FROM orders o JOIN order_items oi ON oi.order_id = o.id
- WHERE oi.product_id = ? AND o.status IN ({$placeholders})"
- );
- $orderStatement->execute(array_merge([$productId], DigitalFiles::PAID_ORDER_STATUSES));
- $buyers = (int) $orderStatement->fetchColumn();
-
- $subscriptionStatement = $usersDb->prepare('SELECT COUNT(DISTINCT user_account_id) FROM subscriptions WHERE product_id = :id');
- $subscriptionStatement->execute(['id' => $productId]);
-
- return $buyers + (int) $subscriptionStatement->fetchColumn();
- } catch (PDOException $exception) {
- return 0;
- }
- }
-
- public static function downloadsCount(int $productId): int
- {
- try {
- $statement = Database::site()->prepare('SELECT COALESCE(SUM(download_count), 0) FROM product_files WHERE product_id = :id');
- $statement->execute(['id' => $productId]);
-
- return (int) $statement->fetchColumn();
- } catch (PDOException $exception) {
- return 0;
- }
- }
- }
-