getSubscriptionLastReviewNumber()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 6
ccs 3
cts 3
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Application\Traits;
6
7
use Application\Model\Product;
8
use Application\Repository\ProductRepository;
9
use Doctrine\ORM\Mapping as ORM;
10
use GraphQL\Doctrine\Attribute as API;
11
use InvalidArgumentException;
12
13
trait HasSubscriptionLastReview
14
{
15
    #[ORM\JoinColumn(onDelete: 'SET NULL')]
16
    #[ORM\ManyToOne(targetEntity: Product::class)]
17
    private ?Product $subscriptionLastReview = null;
18
19
    /**
20
     * Get last review number available through a subscription, bypassing all ACL so it also work even if review is not active yet.
21
     */
22 2
    public function getSubscriptionLastReviewNumber(): ?int
23
    {
24
        /** @var ProductRepository $productRepository */
25 2
        $productRepository = _em()->getRepository(Product::class);
26
27 2
        return $productRepository->getSubscriptionLastReviewNumber($this);
28
    }
29
30
    /**
31
     * Set last review available through a subscription.
32
     */
33
    #[API\Exclude]
34
    public function setSubscriptionLastReview(?Product $subscriptionLastReview): void
35
    {
36
        if ($subscriptionLastReview && !$subscriptionLastReview->getReviewNumber()) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $subscriptionLastReview->getReviewNumber() of type integer|null is loosely compared to false; this is ambiguous if the integer can be 0. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
37
            throw new InvalidArgumentException('The last review of a subscription must be a review, not an article');
38
        }
39
40
        $this->subscriptionLastReview = $subscriptionLastReview;
41
    }
42
}
43