Completed
Push — master ( ee13c0...668d14 )
by Derek Stephen
02:12
created

Init::controller()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

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