Passed
Push — master ( 25d1e0...c0a5de )
by Xavier
02:16
created

Controller::ldapFirst()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 2
1
<?php
2
3
/*
4
 * @author  Xavier Chopin <[email protected]>
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
namespace App\Controller;
11
12
use Psr\Container\ContainerInterface;
13
use Slim\Exception\NotFoundException;
14
use Slim\Flash\Messages;
15
use Slim\Http\Request;
16
use Slim\Http\Response;
17
use Slim\Router;
18
use Slim\Views\Twig;
19
use Awurth\SlimValidation\Validator;
20
use Symfony\Component\Yaml\Yaml;
21
use GuzzleHttp\Client;
22
23
/**
24
 * @property Twig view
25
 * @property Router router
26
 * @property Messages flash
27
 * @property Validator validator
28
 */
29
abstract class Controller
30
{
31
    /**
32
     * Slim application container.
33
     *
34
     * @var ContainerInterface
35
     */
36
    protected $container;
37
38
    /**
39
     * Parameters of the application
40
     *
41
     * @var array
42
     */
43
    private $parameters;
44
45
    /**
46
     * Guzzle HTTP client instance
47
     *
48
     * @var Client
49
     */
50
51
    protected $http;
52
53
    /**
54
     * Constructor.
55
     *
56
     * @param ContainerInterface $container
57
     */
58
    public function __construct(ContainerInterface $container)
59
    {
60
        $this->container = $container;
61
        $this->parameters = Yaml::parse(file_get_contents(__DIR__ . '../../../../app/config/parameters.yml'));
62
        $this->http = new Client(['base_uri' => $this->parameters['api']['url']]);
63
    }
64
65
    /**
66
     * Creates and return a JSON Web Token to the API by using credentials filled in parameters.yml
67
     *
68
     * @return mixed|\Psr\Http\Message\ResponseInterface
69
     */
70
    public function createJWT()
71
    {
72
        return json_decode( $this->http->request('POST', 'auth/login', [
73
            'headers' => [ 'X-Requested-With' => 'XMLHttpRequest' ],
74
            'json' => [
75
                'username' => $this->parameters['api']['username'],
76
                'password' => $this->parameters['api']['password']
77
            ]
78
        ])
79
            ->getBody()
80
            ->getContents() );
81
    }
82
83
84
    /**
85
     * Gets request parameters.
86
     *
87
     * @param Request  $request
88
     * @param string[] $params
89
     * @param string   $default
90
     *
91
     * @return string[]
92
     */
93
    public function params(Request $request, array $params, $default = null)
94
    {
95
        $data = [];
96
        foreach ($params as $param) {
97
            $data[$param] = $request->getParam($param, $default);
98
        }
99
100
        return $data;
101
    }
102
103
    /**
104
     * Redirects to a route.
105
     *
106
     * @param Request $request
107
     * @param Response $response
108
     * @param string   $route
109
     * @param array    $params
110
     *
111
     * @return Response
112
     */
113
    public function redirect(Request $request, Response $response, $route, array $params = [])
114
    {
115
        return $response->withRedirect($this->router->pathFor(
116
            $route,
117
            ['country' => $this->getCountry($request)] + $params
118
        ));
119
    }
120
121
    /**
122
     * Redirects to a url.
123
     *
124
     * @param Response $response
125
     * @param string   $url
126
     *
127
     * @return Response
128
     */
129
    public function redirectTo(Response $response, $url)
130
    {
131
        return $response->withRedirect($url);
132
    }
133
134
    /**
135
     * Writes JSON in the response body.
136
     *
137
     * @param Response $response
138
     * @param mixed    $data
139
     * @param int      $status
140
     *
141
     * @return Response
142
     */
143
    public function json(Response $response, $data, $status = 200)
144
    {
145
        return $response->withJson($data, $status);
146
    }
147
148
    /**
149
     * Writes text in the response body.
150
     *
151
     * @param Response $response
152
     * @param string   $data
153
     * @param int      $status
154
     *
155
     * @return int
156
     */
157
    public function write(Response $response, $data, $status = 200)
158
    {
159
        return $response->withStatus($status)->getBody()->write($data);
160
    }
161
162
    /**
163
     * Adds a flash message.
164
     *
165
     * @param string $name
166
     * @param string $message
167
     */
168
    public function flash($name, $message)
169
    {
170
        $this->flash->addMessage($name, $message);
171
    }
172
173
    /**
174
     * Gives the country id used by the client.
175
     *
176
     * @param Request $request
177
     *
178
     * @return string
179
     */
180
    public function getCountry(Request $request)
181
    {
182
        return $request->getAttribute('routeInfo')[2]['country'];
183
    }
184
185
    /**
186
     * Creates a new NotFoundException.
187
     *
188
     * @param Request  $request
189
     * @param Response $response
190
     *
191
     * @return NotFoundException
192
     */
193
    public function notFoundException(Request $request, Response $response)
194
    {
195
        return new NotFoundException($request, $response);
196
    }
197
198
    /**
199
     * Gets a service from the container.
200
     *
201
     * @param string $property
202
     *
203
     * @return mixed
204
     */
205
    public function __get($property)
206
    {
207
        return $this->container->get($property);
208
    }
209
210
    /**
211
     * Stops the script and prints info about a variable
212
     *
213
     * @param mixed $variable
214
     */
215
    public function debug($variable)
216
    {
217
        die('<pre>' . print_r($variable, true) . '</pre>');
0 ignored issues
show
Coding Style Compatibility introduced by
The method debug() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
218
    }
219
220
    /**
221
     * Getter function
222
     *
223
     * @return array|mixed
224
     */
225
    public function getParameters()
226
    {
227
        return $this->parameters;
228
    }
229
230
    /**
231
     * Executes a LDAP query
232
     *
233
     * @param $filter
234
     * @param array $arg
235
     * @return resource
236
     */
237
    public function searchLDAP($filter, $arg = [])
238
    {
239
        return ldap_search($this->container['ldap'], $this->container['parameters']['ldap']['base_dn'], $filter, $arg);
240
    }
241
242
    /**
243
     * Returns data from a LDAP query
244
     *
245
     * @param $filter
246
     * @param array $arg
247
     * @return mixed
248
     */
249
    public function ldap($filter, $arg = [])
250
    {
251
        return ldap_get_entries($this->container['ldap'], $this->searchLDAP($filter, $arg));
252
    }
253
254
    /**
255
     * Returns the first tuple from a LDAP query
256
     *
257
     * @param $filter
258
     * @param array $arg
259
     * @return mixed
260
     */
261
    public function ldapFirst($filter, $arg = [])
262
    {
263
        return ldap_get_entries($this->container['ldap'], $this->searchLDAP($filter, $arg))[0];
264
    }
265
}
266