HasClient::getClient()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
c 1
b 0
f 0
dl 0
loc 11
ccs 6
cts 6
cp 1
rs 10
cc 4
nc 4
nop 0
crap 4
1
<?php
2
3
namespace Spinen\ClickUp\Concerns;
4
5
use Spinen\ClickUp\Api\Client;
6
use Spinen\ClickUp\Exceptions\NoClientException;
7
use Spinen\ClickUp\Support\Model;
8
9
trait HasClient
10
{
11
    /**
12
     * Client instance
13
     *
14
     * @var Client
15
     */
16
    protected $client;
17
18
    /**
19
     * Get the Client instance
20
     *
21
     * If there is no client assigned on the model, but it has a parent, then try to get the parent's client
22
     *
23
     * @return Client
24
     * @throws NoClientException
25
     */
26 121
    public function getClient(): Client
27
    {
28 121
        if (!$this->client && $this->parentModel) {
29 1
            $this->client = $this->parentModel->getClient();
30
        }
31
32 121
        if ($this->client) {
33 120
            return $this->client;
34
        }
35
36 1
        throw new NoClientException();
37
    }
38
39
    /**
40
     * Set the client instance
41
     *
42
     * @param Client $client
43
     *
44
     * @return $this
45
     */
46 204
    public function setClient(?Client $client): self
47
    {
48 204
        $this->client = $client;
49
50 204
        return $this;
51
    }
52
}
53