PickService::getClient()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * Copyright (c) 2017 Salah Alkhwlani <[email protected]>
5
 *
6
 * For the full copyright and license information, please view
7
 * the LICENSE file that was distributed with this source code.
8
 */
9
10
namespace Yemenifree\PickServices;
11
12
use Zttp\ZttpResponse;
13
14
class PickService
15
{
16
    /** @var ApiClient */
17
    protected $client;
18
19
    /**
20
     * PickService constructor.
21
     *
22
     * @param string $token
23
     * @param bool   $sandbox
24
     */
25
    public function __construct(string $token, bool $sandbox = false)
26
    {
27
        // create client.
28
        $this->client = new ApiClient();
29
30
        // init client
31
        $this->getClient()->setSandbox($sandbox)->setToken($token);
32
    }
33
34
    /**
35
     * @return ApiClient
36
     */
37
    public function getClient(): ApiClient
38
    {
39
        return $this->client;
40
    }
41
42
    /**
43
     * create new request to api.
44
     *
45
     * @param string $service
46
     * @param string $type
47
     * @param array  ...$arg
48
     *
49
     * @throws \Exception
50
     *
51
     * @return ZttpResponse
52
     */
53
    public function request(string $service, string $type, ...$arg)
54
    {
55
        $serviceClass = $this->getClass($service);
56
57
        if (!\class_exists($serviceClass)) {
58
            throw new \Exception("class $serviceClass not exists");
59
        }
60
61
        $service = new  $serviceClass($this->getClient());
62
63
        return $service->$type(...$arg);
64
    }
65
66
    /**
67
     * Get service class.
68
     *
69
     * @param string $service
70
     *
71
     * @return string
72
     */
73
    protected function getClass(string $service): string
74
    {
75
        return 'Yemenifree\\PickServices\\Services\\' . \ucfirst($service);
76
    }
77
}
78