Passed
Push — master ( c05460...30c9c2 )
by Seth
01:04
created

index.php (30 issues)

1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 15 and the first side effect is on line 3.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
3
require_once __DIR__ . '/vendor/autoload.php';
4
5
use Battis\SharedLogs\Database\Bindings\DevicesBinding;
6
use Battis\SharedLogs\Database\Bindings\EntriesBinding;
7
use Battis\SharedLogs\Database\Bindings\LogsBinding;
8
use Battis\SharedLogs\Database\Bindings\UsersBinding;
9
use Battis\SharedLogs\Objects\User;
10
use Slim\App;
11
use Slim\Handlers\Strategies\RequestResponseArgs;
12
use Slim\Http\Request;
13
use Slim\Http\Response;
14
15
define('id_PATTERN', '/{id:[0-9]+}');
16
17
$config = json_decode(file_get_contents('config.json'), true);
18
$app = new App(['settings' => $config]);
19
20
/* register dependencies */
21
$container = $app->getContainer();
22
23
/*
24
 * show errors
25
 * TODO Handle database errors more transparently
26
 * FIXME disable in production!
27
 */
28
$container['settings']['displayErrorDetails'] = true;
29
30
/* database with PDO */
31
$container['pdo'] = function ($c) {
32
    $settings = $c['settings']['database'];
33
    $pdo = new PDO($settings['dsn'], $settings['user'], $settings['password']);
34
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
35
    $pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
36
    return $pdo;
37
};
38
39
/* placeholders as separate arguments */
40
$container['foundHandler'] = function () {
41
    return new RequestResponseArgs();
42
};
43
44
/* prepare bindings */
45
$container['devices'] = function ($c) {
46
    return new DevicesBinding($c->pdo);
47
};
48
$container['logs'] = function ($c) {
49
    return new LogsBinding($c->pdo);
50
};
51
$container['entries'] = function ($c) {
52
    return new EntriesBinding($c->pdo);
53
};
54
$container['users'] = function ($c) {
55
    return new UsersBinding($c->pdo);
56
};
57
58
$container['cors'] = function ($c) {
59
    return [
60
        'allow-origin' => (empty($c['settings']['cors']['allow-origin'])
61
            ? ($_SERVER['HTTPS'] ? 'https://' : 'http://') . $_SERVER['SERVER_NAME']
62
            : $c['settings']['cors']['allow-origin']
63
        ),
64
        'allow-headers' => (empty($c['settings']['cors']['allow-headers'])
65
            ? 'X-Requested-With, Content-Type, Accept, Origin, Authorization'
66
            : $c['settings']['cors']['allow-headers']
67
        ),
68
        'allow-methods' => (empty($c['settings']['cors']['allow-methods'])
69
            ? 'GET, POST, PUT, DELETE, OPTIONS'
70
            : $c['settings']['cors']['allow-headers']
71
        )
72
    ];
73
};
74
75
$apiPrefix = $container['settings']['api']['prefix'];
76
77
/* "lazy CORS" */
78
$app->options($apiPrefix . '/{routes:.+}', function ($request, $response, $args) {
0 ignored issues
show
The parameter $args is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

78
$app->options($apiPrefix . '/{routes:.+}', function ($request, $response, /** @scrutinizer ignore-unused */ $args) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
79
    return $response;
80
});
81
82
$app->add(function (Request $req, Response $res, callable $next) {
83
    $response = $next($req, $res);
84
    return $response
85
        ->withHeader('Access-Control-Allow-Origin', $this->cors['allow-origin'])
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $this seems to be never defined.
Loading history...
86
        ->withHeader('Access-Control-Allow-Headers', $this->cors['allow-headers'])
87
        ->withHeader('Access-Control-Allow-Methods', $this->cors['allow-methods']);
88
});
89
90
function callWithNonEmptyParams(callable $method, ...$params)
91
{
92
    return $method(...array_filter($params, function ($param) {
93
        return !empty($param);
94
    }));
95
}
96
97
/*
98
 * define routes
99
 */
100
$app->group($apiPrefix . '/devices', function () {
101
    $this->post('', function (Request $request, Response $response) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $this seems to be never defined.
Loading history...
102
        return $response->withJson(callWithNonEmptyParams([$this->devices, 'create'], $request->getParsedBody(), $request->getParams()));
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $this seems to be never defined.
Loading history...
103
    });
104
    $this->get('', function (Request $request, Response $response) {
105
        return $response->withJson(callWithNonEmptyParams([$this->devices, 'all'], $request->getParams()));
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $this seems to be never defined.
Loading history...
106
    });
107
    $this->get(id_PATTERN, function (Request $request, Response $response, $id) {
108
        return $response->withJson(callWithNonEmptyParams([$this->devices, 'get'], $id, $request->getParams()));
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $this seems to be never defined.
Loading history...
109
    });
110
    $this->put(id_PATTERN, function (Request $request, Response $response, $id) {
111
        return $response->withJson(callWithNonEmptyParams([$this->devices, 'update'], $id, $request->getParams()));
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $this seems to be never defined.
Loading history...
112
    });
113
    $this->delete(id_PATTERN, function (Request $request, Response $response, $id) {
114
        return $response->withJson(callWithNonEmptyParams([$this->devices, 'delete'], $id, $request->getParams()));
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $this seems to be never defined.
Loading history...
115
    });
116
    $this->get(id_PATTERN . '/logs', function (Request $request, Response $response, $id) {
117
        return $response->withJson(callWithNonEmptyParams([$this->logs, 'listByDevice'], $id, $request->getParams()));
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $this seems to be never defined.
Loading history...
118
    });
119
});
120
$app->group($apiPrefix . '/logs', function () {
121
    $this->post('', function (Request $request, Response $response) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $this seems to be never defined.
Loading history...
122
        return $response->withJson(callWithNonEmptyParams([$this->logs, 'create'], $request->getParsedBody(), $request->getParams()));
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $this seems to be never defined.
Loading history...
123
    });
124
    $this->get('', function (Request $request, Response $response) {
125
        return $response->withJson(callWithNonEmptyParams([$this->logs, 'all'], $request->getParams()));
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $this seems to be never defined.
Loading history...
126
    });
127
    $this->get(id_PATTERN, function (Request $request, Response $response, $id) {
128
        return $response->withJson(callWithNonEmptyParams([$this->logs, 'get'], $id, $request->getParams()));
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $this seems to be never defined.
Loading history...
129
    });
130
    $this->put(id_PATTERN, function (Request $request, Response $response, $id) {
131
        return $response->withJson(callWithNonEmptyParams([$this->logs, 'update'], $id, $request->getParams()));
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $this seems to be never defined.
Loading history...
132
    });
133
    $this->delete(id_PATTERN, function (Request $request, Response $response, $id) {
134
        return $response->withJson(callWithNonEmptyParams([$this->logs, 'delete'], $id, $request->getParams()));
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $this seems to be never defined.
Loading history...
135
    });
136
    $this->get(id_PATTERN . '/entries', function (Request $request, Response $response, $id) {
137
        return $response->withJson(callWithNonEmptyParams([$this->entries, 'listByLog'], $id, $request->getParams()));
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $this seems to be never defined.
Loading history...
138
    });
139
});
140
$app->group($apiPrefix . '/entries', function () {
141
    $this->post('', function (Request $request, Response $response) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $this seems to be never defined.
Loading history...
142
        return $response->withJson(callWithNonEmptyParams([$this->entries, 'create'], $request->getParsedBody(), $request->getParams()));
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $this seems to be never defined.
Loading history...
143
    });
144
    $this->get(id_PATTERN, function (Request $request, Response $response, $id) {
145
        return $response->withJson(callWithNonEmptyParams([$this->entries, 'get'], $id, $request->getParams()));
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $this seems to be never defined.
Loading history...
146
    });
147
    $this->put(id_PATTERN, function (Request $request, Response $response, $id) {
148
        return $response->withJson(callWithNonEmptyParams([$this->entries, 'update'], $id, $request->getParams()));
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $this seems to be never defined.
Loading history...
149
    });
150
    $this->delete(id_PATTERN, function (Request $request, Response $response, $id) {
151
        return $response->withJson(callWithNonEmptyParams([$this->entries, 'delete'], $id, $request->getParams()));
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $this seems to be never defined.
Loading history...
152
    });
153
});
154
$app->group($apiPrefix . '/users', function () {
155
    $this->post('', function (Request $request, Response $response) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $this seems to be never defined.
Loading history...
156
        return $response->withJson(callWithNonEmptyParams([$this->users, 'create'], $request->getParsedBody(), $request->getParams()));
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $this seems to be never defined.
Loading history...
157
    });
158
    $this->get('', function (Request $request, Response $response) {
159
        return $response->withJson(callWithNonEmptyParams([$this->users, 'all'], $request->getParams()));
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $this seems to be never defined.
Loading history...
160
    });
161
    $this->get(id_PATTERN, function (Request $request, Response $response, $id) {
162
        return $response->withJson(callWithNonEmptyParams([$this->users, 'get'], $id, $request->getParams()));
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $this seems to be never defined.
Loading history...
163
    });
164
    $this->get('/{screen_name:\w{' . User::SCREEN_NAME_MINIMUM_LENGTH . ',}}', function (Request $request, Response $response, $screen_name) {
165
        return $response->withJson(callWithNonEmptyParams([$this->users, 'lookupByScreenName'], $screen_name, $request->getParams()));
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $this seems to be never defined.
Loading history...
166
    });
167
    $this->put(id_PATTERN, function (Request $request, Response $response, $id) {
168
        return $response->withJson(callWithNonEmptyParams([$this->users, 'update'], $id, $request->getParams()));
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $this seems to be never defined.
Loading history...
169
    });
170
    $this->delete(id_PATTERN, function (Request $request, Response $response, $id) {
171
        return $response->withJson(callWithNonEmptyParams([$this->users, 'delete'], $id, $request->getParams()));
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $this seems to be never defined.
Loading history...
172
    });
173
});
174
175
/* finish lazy CORS */
176
$app->map(['GET', 'POST', 'PUT', 'DELETE'], $apiPrefix . '/{routes:.+}', function ($req, $res) {
177
    $handler = $this->notFoundHandler;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $this seems to be never defined.
Loading history...
178
    return $handler($req, $res);
179
});
180
181
$app->run();
182