AbstractProductStrategy::checkProductIntegrity()   A
last analyzed

Complexity

Conditions 5
Paths 3

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 14.6179

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 5
eloc 10
c 2
b 0
f 0
nc 3
nop 1
dl 0
loc 18
ccs 3
cts 11
cp 0.2727
crap 14.6179
rs 9.6111
1
<?php
2
3
namespace Terox\SubscriptionBundle\Strategy;
4
5
use Monolog\Logger;
6
use Terox\SubscriptionBundle\Exception\ProductExpiredException;
7
use Terox\SubscriptionBundle\Exception\ProductIntegrityException;
8
use Terox\SubscriptionBundle\Exception\ProductQuoteExceededException;
9
use Terox\SubscriptionBundle\Model\ProductInterface;
10
use Terox\SubscriptionBundle\Repository\ProductRepositoryInterface;
11
use Terox\SubscriptionBundle\Repository\SubscriptionRepositoryInterface;
12
13
abstract class AbstractProductStrategy implements ProductStrategyInterface
14
{
15
    /**
16
     * @var ProductRepositoryInterface
17
     */
18
    private $productRepository;
19
20
    /**
21
     * @var SubscriptionRepositoryInterface
22
     */
23
    private $subscriptionRepository;
24
25
    /**
26
     * @var Logger
27
     */
28
    private $logger;
29
30
    /**
31
     * Constructor.
32
     *
33
     * @param ProductRepositoryInterface      $productRepository
34
     * @param SubscriptionRepositoryInterface $subscriptionRepository
35
     * @param Logger                          $logger
36
     */
37 21
    public function __construct(
38
        ProductRepositoryInterface $productRepository,
39
        SubscriptionRepositoryInterface $subscriptionRepository,
40
        Logger $logger
41
    )
42
    {
43 21
        $this->productRepository      = $productRepository;
44 21
        $this->subscriptionRepository = $subscriptionRepository;
45 21
        $this->logger                 = $logger;
46 21
    }
47
48
    /**
49
     * @return ProductRepositoryInterface
50
     */
51 2
    protected function getProductRepository()
52
    {
53 2
        return $this->productRepository;
54
    }
55
56
    /**
57
     * @return SubscriptionRepositoryInterface
58
     */
59
    protected function getSubscriptionRepository()
60
    {
61
        return $this->subscriptionRepository;
62
    }
63
64
    /**
65
     * @return Logger
66
     */
67 2
    protected function getLogger()
68
    {
69 2
        return $this->logger;
70
    }
71
72
    /**
73
     * Check the product model integrity.
74
     *
75
     * @param ProductInterface $product
76
     *
77
     * @throws ProductIntegrityException
78
     */
79 7
    final public function checkProductIntegrity(ProductInterface $product)
80
    {
81 7
        if ($product->isDefault() && null !== $product->getQuota()) {
82
83
            throw new ProductIntegrityException(sprintf(
84
                'The product "%s" is a default product with a quota (%s). Default products can not have a quote value.',
85
                $product->getName(),
86
                $product->getQuota()
87
            ));
88
89
        }
90
91 7
        if ($product->isDefault() && null !== $product->getExpirationDate()) {
92
93
            throw new ProductIntegrityException(sprintf(
94
                'The product "%s" is a default product with expiration date (%s). Default products can not have a expiration date.',
95
                $product->getName(),
96
                $product->getExpirationDate()->format('Y-m-d H:i:s')
97
            ));
98
99
        }
100 7
    }
101
102
    /**
103
     * Check product expiration.
104
     *
105
     * @param ProductInterface $product
106
     *
107
     * @throws ProductExpiredException
108
     */
109 7
    public function checkExpiration(ProductInterface $product)
110
    {
111 7
        $expirationDate = $product->getExpirationDate();
112
113 7
        if (null === $expirationDate || new \DateTime() <= $expirationDate) {
114 6
            return;
115
        }
116
117 1
        throw new ProductExpiredException(sprintf(
118 1
            'The product "%s" has been expired at %s.',
119 1
            $product->getName(),
120 1
            $expirationDate->format('Y-m-d H:i:s')
121
        ));
122
    }
123
124
    /**
125
     * Check product quote.
126
     *
127
     * @param ProductInterface $product
128
     *
129
     * @throws ProductQuoteExceededException
130
     */
131 6
    public function checkQuote(ProductInterface $product)
132
    {
133
        // Unlimited quote
134 6
        if (null === $product->getQuota()) {
0 ignored issues
show
introduced by
The condition null === $product->getQuota() is always false.
Loading history...
135 4
            return;
136
        }
137
138
        // Calculate the current quote
139 2
        $currentQuote = $this->subscriptionRepository->getNumberOfSubscriptionsByProducts($product);
140
141 2
        if ($currentQuote < $product->getQuota()) {
142 1
            return;
143
        }
144
145 1
        throw new ProductQuoteExceededException(sprintf(
146 1
            'The product "%s" quota is %s. This is exceeded. Increase the quota.',
147 1
            $product->getName(),
148 1
            $product->getQuota()
149
        ));
150
    }
151
}
152