Passed
Push — master ( ba21e2...5468d0 )
by Gabriel
15:15
created

TokenTrait   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 19
dl 0
loc 53
rs 10
c 1
b 0
f 1
wmc 5

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getOmnipayToken() 0 5 1
A populateFromToken() 0 4 1
A getTokenId() 0 3 1
A getName() 0 4 1
A getMaskedTokenId() 0 5 1
1
<?php
2
3
namespace ByTIC\Payments\Models\Tokens;
4
5
use ByTIC\DataObjects\Behaviors\Timestampable\TimestampableTrait;
6
use ByTIC\Omnipay\Common\Models\TokenInterface;
7
use ByTIC\Payments\Models\AbstractModels\HasCustomer\HasCustomerRecord;
8
use ByTIC\Payments\Models\AbstractModels\HasGateway\HasGatewayRecordTrait;
9
use ByTIC\Payments\Models\AbstractModels\HasPaymentMethod\HasPaymentMethodRecord;
10
use ByTIC\Payments\Models\AbstractModels\HasPurchaseParent;
11
12
/**
13
 * Trait TokenTrait
14
 * @package ByTIC\Payments\Models\Tokens
15
 *
16
 * @property int $id_method
17
 * @property string $gateway
18
 *
19
 * @property string $token_id
20
 * @property string $expiration
21
 *
22
 * @property string $modified
23
 * @property string $created
24
 *
25
 * @method TokensTrait getManager
26
 */
27
trait TokenTrait
28
{
29
    use HasPurchaseParent;
30
    use HasCustomerRecord;
31
    use HasGatewayRecordTrait;
32
    use HasPaymentMethodRecord;
33
    use TimestampableTrait;
34
35
    /**
36
     * @var string
37
     */
38
    protected static $createTimestamps = ['created'];
39
40
    /**
41
     * @var string
42
     */
43
    protected static $updateTimestamps = ['modified'];
44
45
    public function getName(): string
46
    {
47
        return 'Token '
48
            . $this->getMaskedTokenId();
49
    }
50
51
    public function getMaskedTokenId(): string
52
    {
53
        return substr($this->token_id, 0, 4)
54
            . '***'
55
            . substr($this->token_id, -4);
56
    }
57
58
    public function getTokenId(): string
59
    {
60
        return $this->token_id;
61
    }
62
63
    /**
64
     * @param TokenInterface $token
65
     */
66
    public function populateFromToken(TokenInterface $token)
67
    {
68
        $this->token_id = $token->getId();
69
        $this->expiration = $token->getExpirationDate();
70
    }
71
72
    /**
73
     * @return \ByTIC\Omnipay\Common\Models\Token
74
     */
75
    public function getOmnipayToken(): \ByTIC\Omnipay\Common\Models\Token
76
    {
77
        return new \ByTIC\Omnipay\Common\Models\Token([
78
            'id' => $this->token_id,
79
            'expiration_date' => $this->expiration,
80
        ]);
81
    }
82
}
83