HasClient::setClient()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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