Completed
Pull Request — master (#2)
by Jimmy
04:23
created

HasClickUp::clickup()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 8
c 1
b 0
f 0
dl 0
loc 13
ccs 9
cts 9
cp 1
rs 10
cc 2
nc 2
nop 0
crap 2
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 2
            : $this->resolveEncrypter()->decrypt($this->attributes['clickup_token']);
73
    }
74
75
    /**
76
     * Make sure that the clickup_token is fillable & protected
77
     */
78 1
    public function initializeHasClickUp()
79
    {
80 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...
81 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...
82 1
        $this->hidden[] = 'clickup_token';
83
    }
84
85
    /**
86
     * Resolve the encrypter from the IoC
87
     *
88
     * We are staying away from the Crypt facade, so that we can support PHP 7.4 with Laravel 5.x
89
     *
90
     * @return Encrypter
91
     */
92 3
    protected function resolveEncrypter()
93
    {
94 3
        return Container::getInstance()
95 3
                        ->make(Encrypter::class);
96
    }
97
98
    /**
99
     * Mutator for ClickUpToken.
100
     *
101
     * @param string $clickup_token
102
     */
103 3
    public function setClickupTokenAttribute($clickup_token)
104
    {
105
        // If setting the password & already have a client, then empty the client to use new password in client
106 3
        if (!is_null($this->builder)) {
107 1
            $this->builder = null;
108
        }
109
110 3
        $this->attributes['clickup_token'] = is_null($clickup_token)
0 ignored issues
show
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...
introduced by
The condition is_null($clickup_token) is always false.
Loading history...
111 1
            ? null
112 2
            : $this->resolveEncrypter()
113 2
                   ->encrypt($clickup_token);
114
    }
115
}
116