Order::hasSubscription()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 11
ccs 5
cts 5
cp 1
rs 10
c 0
b 0
f 0
cc 3
nc 3
nop 0
crap 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Application\Model;
6
7
use Application\Enum\OrderStatus;
8
use Application\Enum\PaymentMethod;
9
use Application\Repository\OrderRepository;
10
use Application\Traits\HasAddress;
11
use Application\Traits\HasAutomaticBalance;
12
use Application\Traits\HasBalanceInterface;
13
use Doctrine\Common\Collections\ArrayCollection;
14
use Doctrine\Common\Collections\Collection;
15
use Doctrine\ORM\Mapping as ORM;
16
use Ecodev\Felix\Model\Traits\HasInternalRemarks;
17
use GraphQL\Doctrine\Attribute as API;
18
use Money\Money;
19
20
/**
21
 * An order made by a users.
22
 */
23
#[ORM\Table('`order`')]
24
#[ORM\Entity(OrderRepository::class)]
25
class Order extends AbstractModel implements HasBalanceInterface
26
{
27
    use HasAddress;
28
    use HasAutomaticBalance;
29
    use HasInternalRemarks;
30
31
    #[ORM\Column(type: 'enum', options: ['default' => OrderStatus::Pending])]
32
    private OrderStatus $status = OrderStatus::Pending;
33
34
    /**
35
     * @var Collection<int, OrderLine>
36
     */
37
    #[ORM\OneToMany(targetEntity: OrderLine::class, mappedBy: 'order')]
38
    private Collection $orderLines;
39
40
    #[ORM\Column(type: 'enum')]
41
    private PaymentMethod $paymentMethod;
42
43
    /**
44
     * @param OrderStatus $status status for new order
45
     */
46 24
    public function __construct(OrderStatus $status = OrderStatus::Pending)
47
    {
48 24
        $this->status = $status;
49 24
        $this->orderLines = new ArrayCollection();
50 24
        $this->balanceCHF = Money::CHF(0);
51 24
        $this->balanceEUR = Money::EUR(0);
52
    }
53
54
    /**
55
     * Notify when a order line is added
56
     * This should only be called by OrderLine::setOrder().
57
     */
58 17
    public function orderLineAdded(OrderLine $orderLine): void
59
    {
60 17
        $this->orderLines->add($orderLine);
61
    }
62
63
    /**
64
     * Notify when a order line is removed
65
     * This should only be called by OrderLine::setOrder().
66
     */
67 1
    public function orderLineRemoved(OrderLine $orderLine): void
68
    {
69 1
        $this->orderLines->removeElement($orderLine);
70
    }
71
72 20
    public function getOrderLines(): Collection
73
    {
74 20
        return $this->orderLines;
75
    }
76
77 7
    public function getStatus(): OrderStatus
78
    {
79 7
        return $this->status;
80
    }
81
82 6
    public function setStatus(OrderStatus $status): void
83
    {
84
        // If we change from non-confirmed to confirmed, then give temporary access (until explicit import of users)
85 6
        if ($this->status !== OrderStatus::Validated && $status === OrderStatus::Validated) {
86
            /** @var OrderLine $orderLine */
87 6
            foreach ($this->getOrderLines() as $orderLine) {
88 4
                $orderLine->maybeGiveTemporaryAccess();
89
            }
90
        }
91
92 6
        $this->status = $status;
93
    }
94
95 14
    public function getPaymentMethod(): PaymentMethod
96
    {
97 14
        return $this->paymentMethod;
98
    }
99
100 20
    public function setPaymentMethod(PaymentMethod $paymentMethod): void
101
    {
102 20
        $this->paymentMethod = $paymentMethod;
103
    }
104
105
    /**
106
     * Return whether there is at least one subscription in the order.
107
     */
108 2
    #[API\Exclude]
109
    public function hasSubscription(): bool
110
    {
111
        /** @var OrderLine $line */
112 2
        foreach ($this->getOrderLines() as $line) {
113 2
            if ($line->getSubscription()) {
114 1
                return true;
115
            }
116
        }
117
118 1
        return false;
119
    }
120
}
121