@@ -7,125 +7,125 @@ |
||
7 | 7 | |
8 | 8 | class ChuckAPIService |
9 | 9 | { |
10 | - /** |
|
11 | - * If user has not its own parameters set, value will be 'Chuck'. |
|
12 | - * |
|
13 | - * @var string |
|
14 | - */ |
|
15 | - private $firstName; |
|
16 | - |
|
17 | - /** |
|
18 | - * If user has not its own parameters set, value will be 'Norris'. |
|
19 | - * |
|
20 | - * @var string |
|
21 | - */ |
|
22 | - private $lastName; |
|
23 | - |
|
24 | - /** |
|
25 | - * After n seconds, cancel. |
|
26 | - * |
|
27 | - * @var int |
|
28 | - */ |
|
29 | - private $timeout; |
|
30 | - |
|
31 | - /** |
|
32 | - * @param string $firstName |
|
33 | - * @param string $lastName |
|
34 | - * @param int $timeout |
|
35 | - */ |
|
36 | - public function __construct($firstName = 'Chuck', $lastName = 'Norris', $timeout = 2) |
|
37 | - { |
|
38 | - $this |
|
39 | - ->setFirstName($firstName) |
|
40 | - ->setLastName($lastName) |
|
41 | - ->setTimeout($timeout) |
|
42 | - ; |
|
43 | - } |
|
44 | - |
|
45 | - /** |
|
46 | - * @param string $firstName |
|
47 | - * |
|
48 | - * @return $this |
|
49 | - */ |
|
50 | - public function setFirstName($firstName) |
|
51 | - { |
|
52 | - $this->firstName = $firstName; |
|
53 | - |
|
54 | - return $this; |
|
55 | - } |
|
56 | - |
|
57 | - /** |
|
58 | - * @param string $lastName |
|
59 | - * |
|
60 | - * @return $this |
|
61 | - */ |
|
62 | - public function setLastName($lastName) |
|
63 | - { |
|
64 | - $this->lastName = $lastName; |
|
65 | - |
|
66 | - return $this; |
|
67 | - } |
|
68 | - |
|
69 | - /** |
|
70 | - * @param int $timeout |
|
71 | - * |
|
72 | - * @return $this |
|
73 | - */ |
|
74 | - public function setTimeout($timeout) |
|
75 | - { |
|
76 | - $this->timeout = $timeout; |
|
77 | - |
|
78 | - return $this; |
|
79 | - } |
|
80 | - |
|
81 | - /** |
|
82 | - * Get fact from Internet Chuck Norris Database. |
|
83 | - * |
|
84 | - * @return string |
|
85 | - */ |
|
86 | - public function getFact() |
|
87 | - { |
|
88 | - try { |
|
89 | - $response = $this->getResponse(); |
|
90 | - } catch (ConnectException $e) { |
|
91 | - return ''; |
|
92 | - } |
|
93 | - |
|
94 | - //if status is not 200 then return false |
|
95 | - if ($response->getStatusCode() == 200) { |
|
96 | - $datas = json_decode($response->getBody()); |
|
97 | - |
|
98 | - return html_entity_decode(stripslashes($datas->value->joke)); |
|
99 | - } |
|
100 | - } |
|
101 | - |
|
102 | - /** |
|
103 | - * @return \Psr\Http\Message\ResponseInterface |
|
104 | - */ |
|
105 | - private function getResponse() |
|
106 | - { |
|
107 | - $client = $this->getClient(); |
|
108 | - |
|
109 | - |
|
110 | - $response = $client->get('random', [ |
|
111 | - 'query' => [ |
|
112 | - 'firstName' => $this->firstName, |
|
113 | - 'lastName' => $this->lastName, |
|
114 | - ], |
|
115 | - ]); |
|
116 | - |
|
117 | - |
|
118 | - return $response; |
|
119 | - } |
|
120 | - |
|
121 | - /** |
|
122 | - * @return Client |
|
123 | - */ |
|
124 | - private function getClient() |
|
125 | - { |
|
126 | - return new Client([ |
|
127 | - 'base_uri' => 'http://api.icndb.com/jokes/', |
|
128 | - 'timeout' => $this->timeout, |
|
129 | - ]); |
|
130 | - } |
|
10 | + /** |
|
11 | + * If user has not its own parameters set, value will be 'Chuck'. |
|
12 | + * |
|
13 | + * @var string |
|
14 | + */ |
|
15 | + private $firstName; |
|
16 | + |
|
17 | + /** |
|
18 | + * If user has not its own parameters set, value will be 'Norris'. |
|
19 | + * |
|
20 | + * @var string |
|
21 | + */ |
|
22 | + private $lastName; |
|
23 | + |
|
24 | + /** |
|
25 | + * After n seconds, cancel. |
|
26 | + * |
|
27 | + * @var int |
|
28 | + */ |
|
29 | + private $timeout; |
|
30 | + |
|
31 | + /** |
|
32 | + * @param string $firstName |
|
33 | + * @param string $lastName |
|
34 | + * @param int $timeout |
|
35 | + */ |
|
36 | + public function __construct($firstName = 'Chuck', $lastName = 'Norris', $timeout = 2) |
|
37 | + { |
|
38 | + $this |
|
39 | + ->setFirstName($firstName) |
|
40 | + ->setLastName($lastName) |
|
41 | + ->setTimeout($timeout) |
|
42 | + ; |
|
43 | + } |
|
44 | + |
|
45 | + /** |
|
46 | + * @param string $firstName |
|
47 | + * |
|
48 | + * @return $this |
|
49 | + */ |
|
50 | + public function setFirstName($firstName) |
|
51 | + { |
|
52 | + $this->firstName = $firstName; |
|
53 | + |
|
54 | + return $this; |
|
55 | + } |
|
56 | + |
|
57 | + /** |
|
58 | + * @param string $lastName |
|
59 | + * |
|
60 | + * @return $this |
|
61 | + */ |
|
62 | + public function setLastName($lastName) |
|
63 | + { |
|
64 | + $this->lastName = $lastName; |
|
65 | + |
|
66 | + return $this; |
|
67 | + } |
|
68 | + |
|
69 | + /** |
|
70 | + * @param int $timeout |
|
71 | + * |
|
72 | + * @return $this |
|
73 | + */ |
|
74 | + public function setTimeout($timeout) |
|
75 | + { |
|
76 | + $this->timeout = $timeout; |
|
77 | + |
|
78 | + return $this; |
|
79 | + } |
|
80 | + |
|
81 | + /** |
|
82 | + * Get fact from Internet Chuck Norris Database. |
|
83 | + * |
|
84 | + * @return string |
|
85 | + */ |
|
86 | + public function getFact() |
|
87 | + { |
|
88 | + try { |
|
89 | + $response = $this->getResponse(); |
|
90 | + } catch (ConnectException $e) { |
|
91 | + return ''; |
|
92 | + } |
|
93 | + |
|
94 | + //if status is not 200 then return false |
|
95 | + if ($response->getStatusCode() == 200) { |
|
96 | + $datas = json_decode($response->getBody()); |
|
97 | + |
|
98 | + return html_entity_decode(stripslashes($datas->value->joke)); |
|
99 | + } |
|
100 | + } |
|
101 | + |
|
102 | + /** |
|
103 | + * @return \Psr\Http\Message\ResponseInterface |
|
104 | + */ |
|
105 | + private function getResponse() |
|
106 | + { |
|
107 | + $client = $this->getClient(); |
|
108 | + |
|
109 | + |
|
110 | + $response = $client->get('random', [ |
|
111 | + 'query' => [ |
|
112 | + 'firstName' => $this->firstName, |
|
113 | + 'lastName' => $this->lastName, |
|
114 | + ], |
|
115 | + ]); |
|
116 | + |
|
117 | + |
|
118 | + return $response; |
|
119 | + } |
|
120 | + |
|
121 | + /** |
|
122 | + * @return Client |
|
123 | + */ |
|
124 | + private function getClient() |
|
125 | + { |
|
126 | + return new Client([ |
|
127 | + 'base_uri' => 'http://api.icndb.com/jokes/', |
|
128 | + 'timeout' => $this->timeout, |
|
129 | + ]); |
|
130 | + } |
|
131 | 131 | } |