Completed
Push — master ( 229a32...055b70 )
by Artem
02:23
created

ExpireAccountTrait   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 51
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 0
dl 51
loc 51
ccs 10
cts 10
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A expireAccountAt() 8 8 3
A expireAccountIn() 8 8 2
A canAccountBeExpired() 4 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
//----------------------------------------------------------------------
4
//
5
//  Copyright (C) 2017 Artem Rodygin
6
//
7
//  You should have received a copy of the MIT License along with
8
//  this file. If not, see <http://opensource.org/licenses/MIT>.
9
//
10
//----------------------------------------------------------------------
11
12
namespace Pignus\Model;
13
14
use Doctrine\ORM\Mapping as ORM;
15
16
/**
17
 * Trait for "account expiration" feature.
18
 */
19 View Code Duplication
trait ExpireAccountTrait
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
20
{
21
    /**
22
     * @var int Unix Epoch timestamp when the account expires.
23
     *
24
     * @ORM\Column(name="account_expires_at", type="integer", nullable=true)
25
     */
26
    protected $accountExpiresAt;
27
28
    /**
29
     * Makes account to expire at specified moment of time (NULL for no expiration).
30
     *
31
     * @param \DateTime $time
32
     *
33
     * @return self
34
     */
35 1
    public function expireAccountAt(\DateTime $time = null)
36
    {
37 1
        if ($this->canAccountBeExpired()) {
38 1
            $this->accountExpiresAt = $time === null ? null : $time->getTimestamp();
39
        }
40
41 1
        return $this;
42
    }
43
44
    /**
45
     * Makes account to expire in specified period of time.
46
     *
47
     * @param \DateInterval $interval
48
     *
49
     * @return self
50
     */
51 1
    public function expireAccountIn(\DateInterval $interval)
52
    {
53 1
        if ($this->canAccountBeExpired()) {
54 1
            $this->accountExpiresAt = date_create()->add($interval)->getTimestamp();
55
        }
56
57 1
        return $this;
58
    }
59
60
    /**
61
     * Specifies whether the "account expiration" feature is available for this user.
62
     *
63
     * @return bool
64
     */
65 2
    protected function canAccountBeExpired(): bool
66
    {
67 2
        return true;
68
    }
69
}
70