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

TokenTrait::getName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
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