Completed
Push — master ( fb03b4...646b2a )
by smiley
02:45
created

TinyCurlEndpoint::checkPhoneNo()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 9
rs 9.6666
cc 2
eloc 5
nc 2
nop 1
1
<?php
2
/**
3
 * Class TinyCurlEndpoint
4
 *
5
 * @filesource   TinyCurlEndpoint.php
6
 * @created      02.04.2016
7
 * @package      chillerlan\Threema\Endpoint
8
 * @author       Smiley <[email protected]>
9
 * @copyright    2016 Smiley
10
 * @license      MIT
11
 */
12
13
namespace chillerlan\Threema\Endpoint;
14
15
use chillerlan\Threema\GatewayOptions;
16
use chillerlan\TinyCurl\{
17
	Request, Response\ResponseInterface, URL
18
};
19
20
/**
21
 *
22
 */
23
class TinyCurlEndpoint extends EndpointAbstract{
24
25
	/**
26
	 * @var \chillerlan\TinyCurl\Request
27
	 */
28
	private $request;
29
30
	/**
31
	 * TinyCurlEndpoint constructor.
32
	 *
33
	 * @param \chillerlan\Threema\GatewayOptions $gatewayOptions
34
	 * @param \chillerlan\TinyCurl\Request       $request
35
	 */
36
	public function __construct(GatewayOptions $gatewayOptions, Request $request){
37
		parent::__construct($gatewayOptions);
38
39
		$this->request = $request;
40
	}
41
42
	/**
43
	 * @param string $endpoint
44
	 * @param array  $params
45
	 * @param array  $body
46
	 *
47
	 * @return \chillerlan\TinyCurl\Response\ResponseInterface
48
	 * @throws \chillerlan\Threema\Endpoint\EndpointException
49
	 */
50
	private function getResponse(string $endpoint, array $params = [], array $body = []):ResponseInterface{
51
		$endpoint = self::API_BASE.$endpoint;
52
		$params   = array_merge($params, [
53
			'from'   => getenv('THREEMA_GATEWAY_ID'),
54
			'secret' => getenv('THREEMA_GATEWAY_SECRET')
55
		]);
56
57
		$url = !empty($body)
58
			? new URL($endpoint, $params, 'POST', $body)
59
			: new URL($endpoint, $params);
60
61
		$response = $this->request->fetch($url);
62
63
		if($response->info->http_code === 200){
64
			return $response;
65
		}
66
		// @codeCoverageIgnoreStart
67
		elseif(array_key_exists($response->info->http_code, self::API_ERRORS)){
68
			throw new EndpointException('gateway error: '.self::API_ERRORS[$response->info->http_code]);
69
		}
70
71
		throw new EndpointException('unknown error: "compiles on my machine."');
72
		// @codeCoverageIgnoreEnd
73
	}
74
75
	/**
76
	 * @inheritdoc
77
	 */
78
	public function checkCredits():int{
79
		return intval($this->getResponse('/credits')->body->content);
1 ignored issue
show
Bug introduced by
Accessing body on the interface chillerlan\TinyCurl\Response\ResponseInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
80
	}
81
82
	/**
83
	 * @inheritdoc
84
	 */
85
	public function checkCapabilities(string $threemaID):array{
86
		$response = $this->getResponse('/capabilities/'.$this->checkThreemaID($threemaID))->body->content;
1 ignored issue
show
Bug introduced by
Accessing body on the interface chillerlan\TinyCurl\Response\ResponseInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
87
		$response = !empty($response) ? explode(',', $response) : [];
88
89
		sort($response);
90
91
		return $response;
92
	}
93
94
	/**
95
	 * @inheritdoc
96
	 */
97
	public function getIdByPhone(string $phoneno):string{
98
		return $this->getResponse('/lookup/phone/'.$this->checkPhoneNo($phoneno))->body->content;
1 ignored issue
show
Bug introduced by
Accessing body on the interface chillerlan\TinyCurl\Response\ResponseInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
99
	}
100
101
	/**
102
	 * @inheritdoc
103
	 */
104
	public function getIdByPhoneHash(string $phonenoHash):string{
105
		return $this->getResponse('/lookup/phone_hash/'.$this->checkHash($phonenoHash))->body->content;
1 ignored issue
show
Bug introduced by
Accessing body on the interface chillerlan\TinyCurl\Response\ResponseInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
106
	}
107
108
	/**
109
	 * @inheritdoc
110
	 */
111
	public function getIdByEmail(string $email):string{
112
		return $this->getResponse('/lookup/email/'.$this->checkEmail($email))->body->content;
1 ignored issue
show
Bug introduced by
Accessing body on the interface chillerlan\TinyCurl\Response\ResponseInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
113
	}
114
115
	/**
116
	 * @inheritdoc
117
	 */
118
	public function getIdByEmailHash(string $emailHash):string{
119
		return $this->getResponse('/lookup/email_hash/'.$this->checkHash($emailHash))->body->content;
1 ignored issue
show
Bug introduced by
Accessing body on the interface chillerlan\TinyCurl\Response\ResponseInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
120
	}
121
122
	/**
123
	 * @inheritdoc
124
	 */
125
	public function getPublicKey(string $threemaID):string{
126
		return $this->checkHash($this->getResponse('/pubkeys/'.$this->checkThreemaID($threemaID))->body->content);
1 ignored issue
show
Bug introduced by
Accessing body on the interface chillerlan\TinyCurl\Response\ResponseInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
127
	}
128
129
	/**
130
	 * @inheritdoc
131
	 */
132
	public function sendSimple(string $to, string $message):string{
133
		// TODO: Implement sendSimple() method.
134
	}
135
136
	/**
137
	 * @inheritdoc
138
	 */
139
	public function sendE2E(string $threemaID, string $box, string $nonce):string{
140
		// TODO: Implement sendE2E() method.
141
	}
142
143
	/**
144
	 * @inheritdoc
145
	 */
146
	public function upload(string $blob):string{
147
		// TODO: Implement upload() method.
148
	}
149
150
	/**
151
	 * @inheritdoc
152
	 */
153
	public function download(string $blobID){
154
		// TODO: Implement download() method.
155
	}
156
157
}
158