Passed
Push — master ( f4c671...6bd095 )
by Derek Stephen
07:57
created

Init::pdoCheck()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 2
nc 2
nop 2
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 2
rs 10
c 1
b 0
f 1
1
<?php
2
3
namespace Bone\Controller;
4
5
use Barnacle\Container;
6
use Bone\Db\DbProviderInterface;
7
use Bone\I18n\I18nAwareInterface;
8
use Bone\Controller\Controller;
9
use Bone\View\ViewEngine;
10
use Bone\Log\LoggerAwareInterface;
11
use Bone\Server\SessionAwareInterface;
12
use Bone\Server\SiteConfig;
13
use Bone\Server\SiteConfigAwareInterface;
14
use Bone\View\ViewAwareInterface;
15
use Del\SessionManager;
16
use Laminas\I18n\Translator\Translator;
17
use PDO;
18
use Psr\Log\LoggerInterface;
19
20
class Init
21
{
22
    /**
23
     * @param Controller $controller
24
     * @param Container $container
25 1
     * @return Controller
26
     */
27 1
    public static function controller(Controller $controller, Container $container): Controller
28 1
    {
29 1
        self::i18nCheck($controller, $container);
30 1
        self::viewCheck($controller, $container);
31 1
        self::pdoCheck($controller, $container);
32
        self::siteConfigCheck($controller, $container);
33 1
        self::sessionCheck($controller, $container);
34
        self::loggerCheck($controller, $container);
35
36
        return $controller;
37
    }
38
39
    /**
40 1
     * @param \Bone\Controller\Controller $controller
41
     * @param Container $container
42 1
     */
43 1
    private static function pdoCheck(Controller $controller, Container $container): void
44
    {
45 1
        if ($controller instanceof DbProviderInterface) {
46
            $controller->setDb($container->get(PDO::class));
47
        }
48
    }
49
50
    /**
51 1
     * @param \Bone\Controller\Controller $controller
52
     * @param Container $container
53 1
     */
54 1
    private static function i18nCheck(Controller $controller, Container $container): void
55
    {
56 1
        if ($controller instanceof I18nAwareInterface) {
0 ignored issues
show
introduced by
$controller is always a sub-type of Bone\I18n\I18nAwareInterface.
Loading history...
57
            $controller->setTranslator($container->get(Translator::class));
58
        }
59
    }
60
61
    /**
62 1
     * @param \Bone\Controller\Controller $controller
63
     * @param Container $container
64 1
     */
65 1
    private static function viewCheck(Controller $controller, Container $container): void
66
    {
67 1
        if ($controller instanceof ViewAwareInterface) {
0 ignored issues
show
introduced by
$controller is always a sub-type of Bone\View\ViewAwareInterface.
Loading history...
68
            $controller->setView($container->get(ViewEngine::class));
69
        }
70
    }
71
72
    /**
73 1
     * @param \Bone\Controller\Controller $controller
74
     * @param Container $container
75 1
     */
76 1
    private static function siteConfigCheck(Controller $controller, Container $container): void
77
    {
78 1
        if ($controller instanceof SiteConfigAwareInterface) {
0 ignored issues
show
introduced by
$controller is always a sub-type of Bone\Server\SiteConfigAwareInterface.
Loading history...
79
            $controller->setSiteConfig($container->get(SiteConfig::class));
80
        }
81
    }
82
83
    /**
84 1
     * @param \Bone\Controller\Controller $controller
85
     * @param Container $container
86 1
     */
87 1
    private static function sessionCheck(Controller $controller, Container $container): void
88 1
    {
89 1
        if ($controller instanceof SessionAwareInterface) {
90 1
            $controller->setSession($container->get(SessionManager::class));
91
        }
92 1
    }
93
94
    /**
95
     * @param \Bone\Controller\Controller $controller
96
     * @param Container $container
97
     */
98
    private static function loggerCheck(Controller $controller, Container $container): void
99
    {
100
        if ($controller instanceof LoggerAwareInterface) {
101
            $channel = $controller->getChannel();
102
            $loggers = $container->get(LoggerInterface::class);
103
            $logger = in_array($channel, $loggers) ? $loggers['channel'] : $loggers['default'];
104
            $controller->setLogger($logger);
105
        }
106
    }
107
}