Completed
Push — master ( 52b305...ed76d4 )
by Guillermo A.
01:49
created

Client::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 2
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Guillermoandrae\Highrise\Client;
4
5
use BadMethodCallException;
6
use Guillermoandrae\Highrise\Http\GuzzleAdapter;
7
use Guillermoandrae\Highrise\Http\AdapterAwareTrait;
8
use Guillermoandrae\Highrise\Http\AdapterInterface;
9
use Guillermoandrae\Highrise\Http\CredentialsAwareTrait;
10
use Guillermoandrae\Highrise\Resources;
11
use Guillermoandrae\Highrise\Resources\ResourceFactory;
12
use Guillermoandrae\Highrise\Resources\ResourceInterface;
13
14
/**
15
 * Highrise API Client class.
16
 *
17
 * @method Resources\Account account()
18
 * @method Resources\Cases cases()
19
 * @method Resources\Deals deals()
20
 * @method Resources\People people()
21
 * @method Resources\Tasks tasks()
22
 * @method Resources\Users users()
23
 *
24
 * @author Guillermo A. Fisher <[email protected]>
25
 */
26
final class Client implements ClientInterface
27
{
28
    use AdapterAwareTrait, CredentialsAwareTrait;
29
30
    /**
31
     * The default HTTP adapter.
32
     *
33
     * @var AdapterInterface
34
     */
35
    private $defaultAdapter;
36
37
    /**
38
     * Client constructor.
39
     *
40
     * @param string $subdomain The account subdomain.
41
     * @param string $token The authentication token.
42
     */
43 1
    public function __construct(string $subdomain = '', string $token = '')
44
    {
45 1
        $this->setSubdomain($subdomain);
46 1
        $this->setToken($token);
47 1
    }
48
49
    /**
50
     * Invoked when non-existent methods are called. Can be used for returning
51
     * resources.
52
     *
53
     * @param string $name The method name.
54
     * @param array $arguments The method arguments.
55
     * @return mixed
56
     */
57 1
    public function __call($name, $arguments)
58
    {
59
        try {
60 1
            return $this->resource($name);
61 1
        } catch (\Exception $ex) {
62 1
            throw new BadMethodCallException(
63 1
                sprintf('The %s method does not exist.', $name)
64
            );
65
        }
66
    }
67
68 1
    public function resource(string $name): ResourceInterface
69
    {
70 1
        if (!$client = $this->adapter) {
0 ignored issues
show
Unused Code introduced by
The assignment to $client is dead and can be removed.
Loading history...
71 1
            $this->setAdapter($this->getDefaultAdapter());
72
        }
73 1
        return ResourceFactory::factory($name, $this->getAdapter());
74
    }
75
76 1
    public function getDefaultAdapter(): AdapterInterface
77
    {
78 1
        if (!$this->defaultAdapter) {
79 1
            $this->defaultAdapter = new GuzzleAdapter($this->getSubdomain(), $this->getToken());
80
        }
81 1
        return $this->defaultAdapter;
82
    }
83
}
84