Passed
Push — main ( 2475c8...d45680 )
by Yaroslav
02:49
created

UsesPauseCollection   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 166
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 50
c 1
b 0
f 0
dl 0
loc 166
ccs 63
cts 63
cp 1
rs 10
wmc 23

12 Methods

Rating   Name   Duplication   Size   Complexity  
A notPaused() 0 3 1
A scopeNotPaused() 0 14 2
A paused() 0 8 4
A scopePaused() 0 9 2
A unpause() 0 13 2
A pauseResumesAtTimestamp() 0 7 3
A pauseResumesAt() 0 7 2
A pauseBehaviorVoid() 0 3 1
A pauseBehaviorKeepAsDraft() 0 3 1
A syncStripePauseCollection() 0 8 1
A pause() 0 17 3
A pauseBehaviorMarkUncollectible() 0 3 1
1
<?php
2
3
namespace CashierSubscriptionPause\Eloquent;
4
5
use Carbon\Carbon;
6
use Illuminate\Database\Eloquent\Builder;
7
use LogicException;
8
9
/**
10
 * @property array|null $pause_collection
11
 * @extends WithPauseCollection
12
 */
13
trait UsesPauseCollection
14
{
15
    /**
16
     * @inerhitDoc
17
     */
18 2
    public function pause(string $behavior, ?Carbon $resumesAt = null): static
19
    {
20 2
        if ($this->canceled()) {
0 ignored issues
show
Bug introduced by
It seems like canceled() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

20
        if ($this->/** @scrutinizer ignore-call */ canceled()) {
Loading history...
21 1
            throw new LogicException('Unable to pause subscription that is canceled.');
22
        }
23
24 1
        $payload = [ 'behavior' => $behavior ];
25 1
        if ($resumesAt) {
26 1
            $payload['resumes_at'] = $resumesAt->timestamp;
27
        }
28 1
        $stripeSubscription = $this->updateStripeSubscription([ 'pause_collection' => $payload, ]);
0 ignored issues
show
Bug introduced by
It seems like updateStripeSubscription() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

28
        /** @scrutinizer ignore-call */ 
29
        $stripeSubscription = $this->updateStripeSubscription([ 'pause_collection' => $payload, ]);
Loading history...
29
30 1
        $this->fill([
0 ignored issues
show
Bug introduced by
It seems like fill() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

30
        $this->/** @scrutinizer ignore-call */ 
31
               fill([
Loading history...
31 1
            'pause_collection' => $stripeSubscription->pause_collection,
32 1
        ])->save();
33
34 1
        return $this;
35
    }
36
37
    /**
38
     * @inerhitDoc
39
     */
40 1
    public function pauseBehaviorMarkUncollectible(?Carbon $resumesAt = null): static
41
    {
42 1
        return $this->pause(WithPauseCollection::BEHAVIOR_MARK_UNCOLLECTIBLE, $resumesAt);
43
    }
44
45
    /**
46
     * @inerhitDoc
47
     */
48 2
    public function pauseBehaviorKeepAsDraft(?Carbon $resumesAt = null): static
49
    {
50 2
        return $this->pause(WithPauseCollection::BEHAVIOR_KEEP_AS_DRAFT, $resumesAt);
51
    }
52
53
    /**
54
     * @inerhitDoc
55
     */
56 1
    public function pauseBehaviorVoid(?Carbon $resumesAt = null): static
57
    {
58 1
        return $this->pause(WithPauseCollection::BEHAVIOR_VOID, $resumesAt);
59
    }
60
61
    /**
62
     * @inerhitDoc
63
     */
64 2
    public function unpause(): static
65
    {
66 2
        if (!$this->paused()) {
67 1
            throw new LogicException('Unable to unpause subscription that is not paused.');
68
        }
69
70 1
        $stripeSubscription = $this->updateStripeSubscription([ 'pause_collection' => '', ]);
71
72 1
        $this->fill([
73 1
            'pause_collection' => $stripeSubscription->pause_collection,
74 1
        ])->save();
75
76 1
        return $this;
77
    }
78
79
    /**
80
     * @inerhitDoc
81
     */
82 1
    public function syncStripePauseCollection(): static
83
    {
84 1
        $subscription = $this->asStripeSubscription();
0 ignored issues
show
Bug introduced by
It seems like asStripeSubscription() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

84
        /** @scrutinizer ignore-call */ 
85
        $subscription = $this->asStripeSubscription();
Loading history...
85
86 1
        $this->pause_collection = $subscription->pause_collection?->toArray();
87 1
        $this->save();
0 ignored issues
show
Bug introduced by
It seems like save() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

87
        $this->/** @scrutinizer ignore-call */ 
88
               save();
Loading history...
88
89 1
        return $this;
90
    }
91
92
    /**
93
     * @inerhitDoc
94
     */
95 3
    public function paused(?string $behavior = null): bool
96
    {
97 3
        $hasBehavior = is_array($this->pause_collection) && !empty($this->pause_collection['behavior']);
98 3
        if ($behavior) {
99 2
            return $hasBehavior && $this->pause_collection['behavior'] === $behavior;
100
        }
101
102 3
        return $hasBehavior;
103
    }
104
105
    /**
106
     * Filter query by pause_collection behavior.
107
     *
108
     * @param Builder $query
109
     * @param string|null $behavior
110
     *
111
     * @return Builder
112
     */
113 1
    public function scopePaused($query, ?string $behavior = null): Builder
114
    {
115 1
        if (!$behavior) {
116 1
            return $query->whereNotNull('pause_collection')
117 1
                         ->where('pause_collection->behavior', '!=', '')
118 1
                         ->whereNotNull('pause_collection->behavior');
119
        }
120
121 1
        return $query->where('pause_collection->behavior', '=', $behavior);
122
    }
123
124
    /**
125
     * @inerhitDoc
126
     */
127 1
    public function notPaused(?string $behavior = null): bool
128
    {
129 1
        return !$this->paused($behavior);
130
    }
131
132
    /**
133
     * Filter query by not paused payment collection.
134
     *
135
     * @param Builder $query
136
     * @param string|null $behavior
137
     *
138
     * @return Builder
139
     */
140 1
    public function scopeNotPaused($query, ?string $behavior = null): Builder
141
    {
142 1
        if (!$behavior) {
143 1
            return $query->where(function ($query) {
144 1
                $query->whereNull('pause_collection')
145 1
                      ->orWhere('pause_collection->behavior', '=', '')
146 1
                      ->orWhereNull('pause_collection->behavior');
147 1
            });
148
        }
149
150 1
        return $query->where(function ($query) use ($behavior) {
151 1
            $query->where('pause_collection->behavior', '!=', $behavior)
152 1
                  ->orWhereNull('pause_collection')
153 1
                  ->orWhereNull('pause_collection->behavior');
154 1
        });
155
    }
156
157
    /**
158
     * @inerhitDoc
159
     */
160 2
    public function pauseResumesAtTimestamp(?string $behavior = null): ?string
161
    {
162 2
        if (!$this->paused($behavior) || empty($this->pause_collection['resumes_at'])) {
163 2
            return null;
164
        }
165
166 1
        return $this->pause_collection['resumes_at'];
167
    }
168
169
    /**
170
     * @inerhitDoc
171
     */
172 2
    public function pauseResumesAt(?string $behavior = null): ?Carbon
173
    {
174 2
        if ($timestamp = $this->pauseResumesAtTimestamp($behavior)) {
175 1
            return Carbon::createFromTimestamp($timestamp);
176
        }
177
178 2
        return null;
179
    }
180
}
181