Completed
Push — develop ( b200db...5ab519 )
by Adolfo
04:19
created

Subscription   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 0
Metric Value
wmc 12
lcom 0
cbo 4
dl 0
loc 83
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A make() 0 12 2
A scopeCurrent() 0 9 1
A scopeUnfinished() 0 8 1
A getDaysLeft() 0 8 2
A isPerpetual() 0 4 1
A getElapsedDays() 0 4 1
A getExpirationDate() 0 4 1
A getStartDate() 0 4 1
A subscriber() 0 4 1
A plan() 0 4 1
1
<?php
2
3
namespace Sagitarius29\LaravelSubscriptions\Entities;
4
5
use Carbon\Carbon;
6
use Illuminate\Database\Eloquent\Model;
7
use Illuminate\Database\Eloquent\Builder;
8
use Illuminate\Database\Eloquent\Relations\BelongsTo;
9
use Sagitarius29\LaravelSubscriptions\Contracts\PlanContract;
10
use Sagitarius29\LaravelSubscriptions\Contracts\SubscriptionContact;
11
use Sagitarius29\LaravelSubscriptions\Exceptions\SubscriptionErrorException;
12
13
class Subscription extends Model implements SubscriptionContact
14
{
15
    protected $table = 'subscriptions';
16
17
    protected $fillable = [
18
        'plan_id', 'start_at', 'end_at',
19
    ];
20
21
    protected $dates = [
22
        'start_at', 'end_at',
23
    ];
24
25
    public static function make(PlanContract $plan, Carbon $start_at, Carbon $end_at = null): Model
26
    {
27
        if (! $plan instanceof Model) {
28
            throw new SubscriptionErrorException('$plan must be '.Model::class);
29
        }
30
31
        return new self([
32
            'plan_id' => $plan->id,
33
            'start_at' => $start_at,
34
            'end_at' => $end_at,
35
        ]);
36
    }
37
38
    public function scopeCurrent(Builder $q)
39
    {
40
        $today = now();
41
42
        return $q->where('start_at', '<=', $today)
43
            ->where(function ($query) use ($today) {
44
                $query->where('end_at', '>=', $today)->orWhereNull('end_at');
45
            });
46
    }
47
48
    public function scopeUnfinished(Builder $q)
49
    {
50
        $today = now();
51
52
        return $q->where(function ($query) use ($today) {
53
            $query->where('end_at', '>=', $today)->orWhereNull('end_at');
54
        });
55
    }
56
57
    public function getDaysLeft(): ?int
58
    {
59
        if ($this->isPerpetual()) {
60
            return null;
61
        }
62
63
        return now()->diffInDays($this->end_at);
0 ignored issues
show
Documentation introduced by
The property end_at does not exist on object<Sagitarius29\Lara...\Entities\Subscription>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
64
    }
65
66
    public function isPerpetual(): bool
67
    {
68
        return $this->end_at == null;
0 ignored issues
show
Documentation introduced by
The property end_at does not exist on object<Sagitarius29\Lara...\Entities\Subscription>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
69
    }
70
71
    public function getElapsedDays(): int
72
    {
73
        return now()->diffInDays($this->start_at);
0 ignored issues
show
Documentation introduced by
The property start_at does not exist on object<Sagitarius29\Lara...\Entities\Subscription>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
74
    }
75
76
    public function getExpirationDate(): ?Carbon
77
    {
78
        return $this->end_at;
0 ignored issues
show
Documentation introduced by
The property end_at does not exist on object<Sagitarius29\Lara...\Entities\Subscription>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
79
    }
80
81
    public function getStartDate(): Carbon
82
    {
83
        return $this->start_at;
0 ignored issues
show
Documentation introduced by
The property start_at does not exist on object<Sagitarius29\Lara...\Entities\Subscription>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
84
    }
85
86
    public function subscriber()
87
    {
88
        return $this->morphTo();
89
    }
90
91
    public function plan(): BelongsTo
92
    {
93
        return $this->belongsTo(config('subscriptions.entities.plan'));
94
    }
95
}
96