Test Failed
Push — develop ( 1fb310...514ddb )
by Paul
09:40
created

Controller   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 20
eloc 47
c 1
b 0
f 1
dl 0
loc 89
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A onCreatedReview() 0 10 3
A maybeEarnPoints() 0 11 5
B filterFieldAfter() 0 35 10
A onApprovedReview() 0 4 1
A earnPoints() 0 10 1
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Integrations\WPLoyalty;
4
5
use GeminiLabs\SiteReviews\Controllers\AbstractController;
6
use GeminiLabs\SiteReviews\Database\ReviewManager;
7
use GeminiLabs\SiteReviews\Helpers\Cast;
8
use GeminiLabs\SiteReviews\Modules\Html\ReviewForm;
9
use GeminiLabs\SiteReviews\Review;
10
use Wlr\App\Helpers\EarnCampaign;
0 ignored issues
show
Bug introduced by
The type Wlr\App\Helpers\EarnCampaign was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
use Wlr\App\Helpers\Woocommerce;
0 ignored issues
show
Bug introduced by
The type Wlr\App\Helpers\Woocommerce was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
use Wlr\App\Premium\Helpers\ProductReview;
0 ignored issues
show
Bug introduced by
The type Wlr\App\Premium\Helpers\ProductReview was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
use Wlr\App\Premium\Helpers\Referral;
0 ignored issues
show
Bug introduced by
The type Wlr\App\Premium\Helpers\Referral was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
14
15
class Controller extends AbstractController
16
{
17
    /**
18
     * @filter site-reviews/review-form/fields/visible
19
     */
20
    public function filterFieldAfter(array $fields, ReviewForm $form): array
21
    {
22
        $assignedPostId = Cast::toInt($form->args->assigned_posts);
23
        if (empty($assignedPostId)) {
24
            return $fields;
25
        }
26
        if (!$product = Woocommerce::getInstance()->getProduct($assignedPostId)) {
27
            return $fields;
28
        }
29
        if (!is_user_logged_in() || Woocommerce::getInstance()->isBannedUser()) {
30
            return $fields;
31
        }
32
        if (apply_filters('wlr_hide_product_review_message', false, [])) {
33
            return $fields;
34
        }
35
        foreach ($fields as $field) {
36
            if ('content' !== $field->original_name) {
37
                continue;
38
            }
39
            $rewardsList = EarnCampaign::getInstance()->getActionEarning(['product_review'], [
40
                'is_calculate_based' => 'product',
41
                'product' => $product,
42
                'product_id' => $assignedPostId,
43
                'user_email' => Woocommerce::getInstance()->get_login_user_email(),
44
            ]);
45
            $messages = [$field->after];
46
            foreach ($rewardsList as $action => $rewards) {
47
                foreach ($rewards as $key => $reward) {
48
                    $messages[] = $reward['messages'] ?? '';
49
                }
50
            }
51
            $field->after = implode('<br/>', array_filter($messages));
52
            break;
53
        }
54
        return $fields;
55
    }
56
57
    /**
58
     * @action site-reviews/review/approved
59
     */
60
    public function onApprovedReview(Review $review): void
61
    {
62
        $review = glsr(ReviewManager::class)->get($review->ID); // get a fresh copy of the review
63
        $this->maybeEarnPoints($review);
64
    }
65
66
    /**
67
     * @action site-reviews/review/created
68
     */
69
    public function onCreatedReview(Review $review): void
70
    {
71
        $review = glsr(ReviewManager::class)->get($review->ID); // get a fresh copy of the review
72
        if (!is_user_logged_in()) {
73
            return;
74
        }
75
        if (!$review->is_approved) {
76
            return;
77
        }
78
        $this->maybeEarnPoints($review);
79
    }
80
81
    protected function earnPoints(string $customerEmail, int $productId): void
82
    {
83
        $data = [
84
            'is_calculate_based' => 'product',
85
            'product' => Woocommerce::getInstance()->getProduct($productId),
86
            'product_id' => $productId,
87
            'user_email' => $customerEmail,
88
        ];
89
        (new ProductReview())->applyEarnProductReview($data);
90
        (new Referral())->doReferralCheck($data);
91
    }
92
93
    protected function maybeEarnPoints(Review $review): void
94
    {
95
        if (!$user = $review->user()) {
96
            return;
97
        }
98
        if (Woocommerce::getInstance()->isBannedUser($user->user_email)) {
99
            return;
100
        }
101
        foreach ($review->assigned_posts as $postId) {
102
            if (Woocommerce::getInstance()->getProduct((int) $postId)) {
103
                $this->earnPoints($user->user_email, (int) $postId);
104
            }
105
        }
106
    }
107
}
108