Issues (12)

src/Concerns/HasClickUp.php (4 issues)

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
     * @throws BindingResolutionException
67
     * @return string|null
68
     */
69 2
    public function getClickupTokenAttribute()
70
    {
71 2
        if (!is_null($this->attributes['clickup_token'])) {
72 1
            return $this->resolveEncrypter()
73 1
                        ->decrypt($this->attributes['clickup_token']);
74
        }
75
76 1
        return null;
77
    }
78
79
    /**
80
     * Make sure that the clickup_token is fillable & protected
81
     */
82 1
    public function initializeHasClickUp()
83
    {
84 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...
85 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...
86 1
        $this->hidden[] = 'clickup_token';
87 1
    }
88
89
    /**
90
     * Resolve the encrypter from the IoC
91
     *
92
     * We are staying away from the Crypt facade, so that we can support PHP 7.4 with Laravel 5.x
93
     *
94
     * @return Encrypter
95
     * @throws BindingResolutionException
96
     */
97 3
    protected function resolveEncrypter()
98
    {
99 3
        return Container::getInstance()
100 3
                        ->make(Encrypter::class);
101
    }
102
103
    /**
104
     * Mutator for ClickUpToken.
105
     *
106
     * @param string $clickup_token
107
     * @throws BindingResolutionException
108
     */
109 3
    public function setClickupTokenAttribute($clickup_token)
110
    {
111
        // If setting the password & already have a client, then empty the client to use new password in client
112 3
        if (!is_null($this->builder)) {
113 1
            $this->builder = null;
114
        }
115
116 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...
The condition is_null($clickup_token) is always false.
Loading history...
117 1
            ? null
118 2
            : $this->resolveEncrypter()
119 2
                   ->encrypt($clickup_token);
120 3
    }
121
}
122