ProductDefaultStrategy::getDefaultProduct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.032

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 2
b 0
f 0
nc 2
nop 0
dl 0
loc 9
ccs 4
cts 5
cp 0.8
crap 2.032
rs 10
1
<?php
2
3
namespace Terox\SubscriptionBundle\Strategy;
4
5
use Terox\SubscriptionBundle\Exception\ProductDefaultNotFoundException;
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
11
class ProductDefaultStrategy extends AbstractProductStrategy
12
{
13
    /**
14
     * {@inheritdoc}
15
     */
16 7
    public function getFinalProduct(ProductInterface $product)
17
    {
18
        try {
19
20 7
            $this->checkProductIntegrity($product);
21 7
            $this->checkExpiration($product);
22 6
            $this->checkQuote($product);
23
24 5
            return $product;
25
26 2
        } catch (ProductIntegrityException $exception) {
27
28
            $this->getLogger()->error('Product integrity: {message}', [
29
                'message' => $exception->getMessage()
30
            ]);
31
32 2
        } catch (ProductExpiredException $exception) {
33
34 1
            $this->getLogger()->error('Product is expired: {message}', [
35 1
                'message' => $exception->getMessage()
36
            ]);
37
38 1
        } catch (ProductQuoteExceededException $exception) {
39
40 1
            $this->getLogger()->error('Product quota is exceeded: {message}', [
41 1
                'message' => $exception->getMessage()
42
            ]);
43
44
        }
45
46 2
        return $this->getDefaultProduct();
47
    }
48
49
    /**
50
     * Get default product in case of that current product is not valid.
51
     *
52
     * @return ProductInterface
53
     *
54
     * @throws ProductDefaultNotFoundException
55
     */
56 2
    private function getDefaultProduct()
57
    {
58 2
        $defaultProduct = $this->getProductRepository()->findDefault();
59
60 2
        if (null !== $defaultProduct) {
61 2
            return $defaultProduct;
62
        }
63
64
        throw new ProductDefaultNotFoundException('Default product was not found into the product repository');
65
    }
66
}
67