Passed
Push — master ( 710aff...9afc87 )
by Diogo Oliveira de
01:38
created

DependencyManager::loadDependencies()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 46

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 1
nop 0
dl 0
loc 46
rs 9.1781
c 0
b 0
f 0
1
<?php
2
3
namespace SeedApp;
4
5
use Monolog\Logger;
6
use Monolog\Handler\StreamHandler;
7
use Monolog\Registry;
8
use SlimX\Models\Error;
9
use SlimX\Exceptions\ErrorCodeException;
10
11
class DependencyManager
12
{
13
    protected $container;
14
15
    public function __construct(\Slim\App $app)
16
    {
17
        $this->container = $app->getContainer();
18
    }
19
20
    public function loadDependencies()
21
    {
22
        $this->container['log'] = function ($container) {
23
            $conf = $container->get('settings')['logger'];
24
            $log = new Logger($conf['name']);
25
            $log->pushHandler(new StreamHandler(
26
                __DIR__ . '/../../logs/api-' .
27
                (new \DateTime())->format('Ymd') . '.log',
28
                $conf['level'] ?? Logger::DEBUG
29
            ));
30
            try {
31
                Registry::addLogger($log, 'log');
32
            } catch (\InvalidArgumentException $e) {
33
                // Log already exists. Let it go!
34
            }
35
36
            return $log;
37
        };
38
39
        $this->container['error'] = function ($container) {
0 ignored issues
show
Unused Code introduced by
The parameter $container 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

39
        $this->container['error'] = function (/** @scrutinizer ignore-unused */ $container) {

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...
40
            $error = new Error();
41
            $error->setCodeList([
42
                1000 => [
43
                    'status' => 406,
44
                    'message' => 'API version is mandatory',
45
                ],
46
            ]);
47
48
            return $error;
49
        };
50
51
        $this->container['errorHandler'] = function ($container) {
52
            return function ($request, $response, $exception) use ($container) {
53
                if ($exception instanceof ErrorCodeException) {
54
                    $container->get('log')->info('Error code ' . $exception->getCode());
55
                    return $container->get('error')->handle($response, $exception->getCode());
56
                }
57
            };
58
        };
59
60
        $this->container['testDbConnection'] = function ($container) {
61
            $dbTest = $container->get('settings')['dbtest'];
62
            return new \PDO(
63
                "mysql:host={$dbTest['host']};dbname={$dbTest['dbname']}",
64
                $dbTest['user'],
65
                $dbTest['pass']
66
            );
67
        };
68
    }
69
}
70