Passed
Push — develop ( 9e5508...855c67 )
by Jimmy
03:28 queued 11s
created

HasClickUp::resolveEncrypter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 2
c 0
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Spinen\ClickUp\Concerns;
4
5
use Illuminate\Container\Container;
6
use Illuminate\Contracts\Container\BindingResolutionException;
7
use Illuminate\Contracts\Encryption\Encrypter;
8
use Spinen\ClickUp\Api\Client as ClickUp;
9
use Spinen\ClickUp\Exceptions\NoClientException;
10
use Spinen\ClickUp\Support\Builder;
11
12
/**
13
 * Trait HasClickUp
14
 *
15
 * @package Spinen\ClickUp
16
 *
17
 * @property ClickUp $clickup
18
 * @property string $clickup_token
19
 */
20
trait HasClickUp
21
{
22
    /**
23
     * ClickUp Builder instance
24
     *
25
     * @var Builder
26
     */
27
    protected $builder = null;
28
29
    /**
30
     * Return cached version of the ClickUp Builder for the user
31
     *
32
     * @return Builder
33
     * @throws BindingResolutionException
34
     */
35 4
    public function clickup()
36
    {
37 4
        if (is_null($this->builder)) {
38 4
            $this->builder = Container::getInstance()
39 4
                                      ->make(Builder::class)
40 4
                                      ->setClient(
41 4
                                          Container::getInstance()
42 4
                                                   ->make(ClickUp::class)
43 4
                                                   ->setToken($this->clickup_token)
44
                                      );
45
        }
46
47 4
        return $this->builder;
48
    }
49
50
    /**
51
     * Accessor for ClickUp Client.
52
     *
53
     * @return ClickUp
54
     * @throws BindingResolutionException
55
     * @throws NoClientException
56
     */
57 1
    public function getClickupAttribute()
58
    {
59 1
        return $this->clickup()
60 1
                    ->getClient();
61
    }
62
63
    /**
64
     * Accessor for ClickUpToken.
65
     *
66
     * @return string
67
     */
68 2
    public function getClickupTokenAttribute()
69
    {
70 2
        return is_null($this->attributes['clickup_token'] ?? null)
71 1
            ? null
72 1
            : $this->resolveEncrypter()
73 2
                   ->decrypt($this->attributes['clickup_token']);
74
    }
75
76
    /**
77
     * Make sure that the clickup_token is fillable & protected
78
     */
79 1
    public function initializeHasClickUp()
80
    {
81 1
        $this->fillable[] = 'clickup_token';
0 ignored issues
show
Bug Best Practice introduced by
The property fillable does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
82 1
        $this->hidden[] = 'clickup';
0 ignored issues
show
Bug Best Practice introduced by
The property hidden does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
83 1
        $this->hidden[] = 'clickup_token';
84
    }
85
86
    /**
87
     * Resolve the encrypter from the IoC
88
     *
89
     * We are staying away from the Crypt facade, so that we can support PHP 7.4 with Laravel 5.x
90
     *
91
     * @return Encrypter
92
     */
93 3
    protected function resolveEncrypter()
94
    {
95 3
        return Container::getInstance()
96 3
                        ->make(Encrypter::class);
97
    }
98
99
    /**
100
     * Mutator for ClickUpToken.
101
     *
102
     * @param string $clickup_token
103
     */
104 3
    public function setClickupTokenAttribute($clickup_token)
105
    {
106
        // If setting the password & already have a client, then empty the client to use new password in client
107 3
        if (!is_null($this->builder)) {
108 1
            $this->builder = null;
109
        }
110
111 3
        $this->attributes['clickup_token'] = is_null($clickup_token)
0 ignored issues
show
introduced by
The condition is_null($clickup_token) is always false.
Loading history...
Bug Best Practice introduced by
The property attributes does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
112 1
            ? null
113 2
            : $this->resolveEncrypter()
114 2
                   ->encrypt($clickup_token);
115
    }
116
}
117