SubscriptionEndLastStrategy::createSubscription()   B
last analyzed

Complexity

Conditions 10
Paths 25

Size

Total Lines 45
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 10.0125

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 10
eloc 20
c 2
b 0
f 0
nc 25
nop 2
dl 0
loc 45
ccs 19
cts 20
cp 0.95
crap 10.0125
rs 7.6666

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Terox\SubscriptionBundle\Strategy;
4
5
use Terox\SubscriptionBundle\Exception\PermanentSubscriptionException;
6
use Terox\SubscriptionBundle\Model\ProductInterface;
7
use Terox\SubscriptionBundle\Model\SubscriptionInterface;
8
9
/**
10
 * End Last Subscription Strategy.
11
 *
12
 * Starts a new subscription at the end of the latest if there isn't any permanent subscription with the current
13
 * product.
14
 */
15
class SubscriptionEndLastStrategy extends AbstractSubscriptionStrategy
16
{
17
    /**
18
     * {@inheritdoc}
19
     */
20 12
    public function createSubscription(ProductInterface $product, array $subscriptions = [])
21
    {
22 12
        if (empty($subscriptions)) {
23 4
            return $this->create($this->createCurrentDate(), $product);
24
        }
25
26 8
        $startDate = null;
27 8
        foreach ($subscriptions as $subscription) {
28
29
            // Subscription is permanent, don't continue
30 8
            if (null === $subscription->getEndDate()) {
31 4
                $startDate = null;
32 4
                break;
33
            }
34
35
            // Catch the subscription with higher end date
36 5
            if (null === $startDate || $startDate < $subscription->getEndDate()) {
37 5
                $startDate = $subscription->getEndDate();
38
            }
39
        }
40
41
        // It's a permanent subscription
42 8
        if (null === $startDate) {
0 ignored issues
show
introduced by
The condition null === $startDate is always true.
Loading history...
43
44 4
            if (count($subscriptions) > 1) {
45 2
                throw new PermanentSubscriptionException(
46 2
                    'More than one subscription per product is not allowed when there is a permanent subscription 
47
                    enabled. Maybe you are mixing different strategies?'
48
                );
49
            }
50
51 2
            return $subscriptions[0];
52
        }
53
54
        // Check if subscription is expired
55 4
        if (time() > $startDate->getTimestamp()) {
56 1
            $startDate = $this->createCurrentDate();
57
        }
58
59
        // Date should use the \DateTimeImmutable (a little fix)
60 4
        if (!$startDate instanceof \DateTimeImmutable) {
61
            $startDate = (new \DateTimeImmutable())->setTimestamp($startDate->getTimestamp());
62
        }
63
64 4
        return $this->create($startDate, $product);
65
    }
66
67
    /**
68
     * Create subscription.
69
     *
70
     * @param \DateTimeImmutable $startDate
71
     * @param ProductInterface   $product
72
     *
73
     * @return SubscriptionInterface
74
     */
75 8
    private function create($startDate, $product)
76
    {
77 8
        $endDate = null !== $product->getDuration() ?
78 8
            $startDate->modify(sprintf('+%s seconds', $product->getDuration())) : null;
0 ignored issues
show
Bug introduced by
$product->getDuration() of type DateInterval is incompatible with the type string expected by parameter $args of sprintf(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

78
            $startDate->modify(sprintf('+%s seconds', /** @scrutinizer ignore-type */ $product->getDuration())) : null;
Loading history...
79
80
        // Create the new subscription
81 8
        $subscription = $this->createSubscriptionInstance();
82 8
        $subscription->setProduct($product);
83 8
        $subscription->setStartDate($startDate);
84 8
        $subscription->setEndDate($endDate);
85 8
        $subscription->setAutoRenewal($product->isAutoRenewal());
86
87 8
        return $subscription;
88
    }
89
}
90